Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,921 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,990 people online right now. Registration is fast and FREE... Join Now!




C-string assignment help

 
Reply to this topicStart new topic

C-string assignment help, Need some help.

jingoria
21 Jan, 2008 - 08:13 PM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 41


My Contributions
Here is the prompt for the program that I have to write:
We are given a Shakespeare prose:
Blow, blow, thou winter wind,
Thou art not so unkind
As man's ingratitude;
Thy tooth is not so keen
Because thou art not seen,
Although thy breath be rude.
Heigh-ho! sing heigh-ho! unto the green holly:

I have to convert the prose code into morse code using this table.

Character Code Character Code Character Code
A .- N -. 1 .----
B -... O --- 2 ..---
C -.-. P .--. 3 ...--
D -.. Q --.- 4 ....-
E . R .-. 5 .....
F ..-. S ... 6 -....
G --. T - 7 --...
H .... U ..- 8 ---..
I .. V ...- 9 ----.
J .--- W .-- 0 -----
K -.- X -..-
L .-.. Y -.--
M -- Z --..

I am just starting my work and have several confusion.
Here is my code so far (just starting).

#include<iostream>
#include<cstring>
#include <string>
#include <fstream>
using namespace std;

int main (int argc, char *argv[])
{
ifstream inStream;
ofstream outStream;

inStream.open("Shakespeare");
if (inStream.fail( ))
{
cout<<"Opening file failed."<<endl;
}

2 Questions.
1) I was thinking to use the getline function to get one line at a time and then convert each character in to morse code but I am not sure how to do that. I am not sure how would I get program to read only one line at a time (not sure if array works)?
2) I don't really know how to use the functions argc and *argv[] in my program and how does it work?

Please help me out.

thanks.

This post has been edited by jingoria: 21 Jan, 2008 - 08:16 PM
User is offlineProfile CardPM
+Quote Post

GWatt
RE: C-string Assignment Help
21 Jan, 2008 - 08:20 PM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,358



Thanked: 31 times
Dream Kudos: 500
My Contributions
You can use a map, which essentially an array indexed by anything you want. You can index, say, "s" to "..."

Initialize you map like so:
CODE

#include <map>
#include <string>

using namespace std;

map morse(char, string);
morse['s'] = "...";


After you initialize all of you alphabet, iterate through all of the characters in the message and add appropriate morse to a new string and give that as the output.

This post has been edited by GWatt: 21 Jan, 2008 - 08:21 PM
User is online!Profile CardPM
+Quote Post

VernonDozier
RE: C-string Assignment Help
21 Jan, 2008 - 08:32 PM
Post #3

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

CODE

int main (int argc, char* argv[])


works like this. Let's say your program is called "myprogram.exe" and you wanted the user to specify a file to open when that program ran called "Shakespeare.txt". You could type this in at the command line:


myprogram Shakespeare.txt


This would run "myprogram.exe" and initialize the parameters as such:

int argc = 2 // this is because you typed in two things ("myprogram" and "Shakespeare.txt")
argv[0] = "myprogram"
argv[1] = "Shakespeare.txt"


argv is an array of type char* with 2 elements (since argc = 2).

If you wanted to use "argv" later in your program, you could do something like this:

CODE

ifstream ins;
ins.open (argv[1]);


That's the equivalent of this:

CODE

istream ins;
ins.open ("Shakespeare.txt");


User is offlineProfile CardPM
+Quote Post

nirvanarupali
RE: C-string Assignment Help
21 Jan, 2008 - 09:53 PM
Post #4

D.I.C Stomach
Group Icon

Joined: 1 Aug, 2007
Posts: 995



Thanked: 4 times
Dream Kudos: 375
My Contributions
As Gwatt says, MAP container is good for this problem...

Here is the example, to start with:
CODE
#include <iostream>
#include <map>
#include <string>
using namespace std;

int main()
{
    // create map / associative array
    
    typedef map<string, string> MapString;

    MapString PairWords;      // create empty container

    // insert some elements
    PairWords["Blow, blow, thou winter wind"] = "A .- N -. 1 .---- ";
    PairWords["Thou art not so unkind"]       = "B -... O --- 2 ..--- ";
    PairWords["As man's ingratitude"]         = "C -.-. P .--. 3 ...--";
    PairWords["Thy tooth is not so keen"]     = "D -.. Q --.- 4 ....-";
    

    // print all elements
    MapString::iterator pos;
    
    for (pos = PairWords.begin(); pos != PairWords.end(); ++pos)
    {
        cout << " " << pos->first << "\t"
             << " -------- " << pos->second << endl;
    }
    cout << endl;
    
    return 0;
    
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month