![]() |
|
#1
|
|||
|
|||
Convert sentence to morse codeThis code is to convert a sentence into Morse code. The run log always terminates with error:
Quote:
TIA for your help!!! CPP / C++ / C Code:
Last edited by LuciWiz : 02-May-2006 at 07:51.
Reason: Edited title; Previous title was "somebody please tell me what I'm doing wrong..."
|
|
#2
|
||||
|
||||
Re: somebody please tell me what I'm doing wrong...The only terminal conditions I see in your loops are a check for '.' and a check for ' '. If your string ends in anything else (which I expect is the case, since newline is likely the last thing read in), then you will keep read characters past the end of the string, hence the index counter i will cause string::at() to throw an exception.
Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#3
|
|||
|
|||
Re: somebody please tell me what I'm doing wrong...(Edit) I've worked it out while writing this post - but am still interested in feedback on my solution)
OK, I'm still having a brain freeze on this. Help me break it down. If I get a word from an input stream: CPP / C++ / C Code:
And enter: Quote:
Now, if I was assigning "Hi" to a C-style string (character array), there would actually be three characters assigned, 'H', 'i', '\0'. But since I'm using a C++ string, there are only the two characters in the string, as evidenced by using CPP / C++ / C Code:
Quote:
So having gotten that out of the way, and using input "Hi" as an example, using loop repetition condition CPP / C++ / C Code:
The problem in my code is that CPP / C++ / C Code:
The complete (revised) code of that section is: CPP / C++ / C Code:
I still have a little work to do, but that's basically working. Is that the "best" solution given the low level of programming that I'm on? |
|
#4
|
||||
|
||||
Re: somebody please tell me what I'm doing wrong...Instead of reading one word at a time, read the entire sentence with getline(). This will allow you to process whitespace (TABs and SPACEs) more accurately.
Also, why stop when you get a '.'? A period is a valid Morse Code character. IMO, you should process all valid characters and either ignore or flag all invalid characters. __________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#5
|
||||
|
||||
Re: somebody please tell me what I'm doing wrong...It might help to consider that std::string is an STL container (of chars). This means that you can use iterators to traverse the string. It is similar to what you've got already, except that each time you call string::at() bounds-checking is performed (some might say unnecessarily, since you know the length of the string).
CPP / C++ / C Code:
Matthew __________________
I was born not knowing and have only had a little time to change that here and there. -- Richard P. Feynman Boris Podolsky: James! How's the rat business? James Moreland: Well, actually it's mostly students I'm experimenting on now. Kurt Gödel: My God, the mazes must be enormous. |
|
#6
|
|||
|
|||
Re: somebody please tell me what I'm doing wrong...Quote:
Walt, that was the assignment! I guess the authors decided that having beginners deal with 26 contiguous characters was a heck of a lot easier than having to deal with some 52 or 53 non-contiguous ones, not counting the special characters. |
|
#7
|
||||
|
||||
Re: somebody please tell me what I'm doing wrong...Quote:
__________________
Cow: You're a lawyer too? Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase! |
|
#8
|
|||
|
|||
Re: somebody please tell me what I'm doing wrong...Quote:
|
|
#9
|
|||
|
|||
Re: somebody please tell me what I'm doing wrong...Morse.h
CPP / C++ / C Code:
Morse.cpp CPP / C++ / C Code:
Output: Code:
...I used an underscore for spaces, since in Morse code, a space is simply a pause in transmission of characters. This is a rather quick and dirty hack, but should give you an idea of at least one alternative that flexes C++ standard library features a bit more along the lines of what Matthew noted previously. By changing the input string: Code:
...obviously a more robust class implementation would handle unknown characters better and offer much better interfaces for conversion to and from Morse code, etc. :davis: |
Recent GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Here it is again! 35% - 40% off For Life! | my-e-space | Web Hosting Advertisements & Offers | 0 | 20-Apr-2006 14:48 |
| User defined headers | davis | Miscellaneous Programming Forum | 6 | 16-Feb-2006 18:40 |
| convert for to while loop | zodisign | C Programming Language | 1 | 14-Aug-2005 06:38 |
| Problem with int mixed with char,... | leitz | CPP / C++ Forum | 17 | 07-Dec-2004 20:56 |
| Morse Code for C | sibyl03 | C Programming Language | 1 | 13-Oct-2004 00:00 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The