Reading characters from a string?

This is a discussion on Reading characters from a string? within the C++ Programming forums, part of the General Programming Boards category; Okay, I feel really stupid, but I cannot seem to get this down pat. Last assigment I turned in incomplete ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    Reading characters from a string?

    Okay, I feel really stupid, but I cannot seem to get this down pat. Last assigment I turned in incomplete because of the same issue, but for whatever reason, I cannot seem to figure out how to read one character at at time and then output the conversion to a string.

    This is what this program is supposed to do. The user is supposed to enter a word and the program is supposed to send out the Phonetic. EG: Type in Rose and you should get: Romeo Oscar Sierra Echo as the output. But everytime I run this program, I get 'invalid data' outputting endlessly.

    Here is my code. Can you guys help?

    Code:
    
    
    
    
    #include <iostream>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    void getPhonetic(char&, string&, string&);
    int main()
    {
    char readChar;
    string insertString;
    string outputString;
    
    getPhonetic(readChar, insertString, outputString);
    
    cin.ignore();
    cin.get();
    return 1;
    }
    
    
    void getPhonetic(char& readChar, string& insertString, string& outputString)
    {
    
    cout << "Please type in a word to convert: \n";
    cin >> insertString;
    
    cin.get(readChar);
    readChar = tolower(readChar);
    while (readChar != '/n')
     
    
    
    
    
    	 switch(readChar)
    {
    
    case 'a': cout << "Alpha";
    		  break;
    case 'b': cout << "Bravo";
    		  break;
    case 'c': cout << "Charlie";
    		  break;
    case 'd': cout << "Delta";
    		  break;
    case 'e': cout << "Echo";
    		  break;
    case 'f': cout << "Foxtrot";
    		  break;
    case 'g': cout << "Golf";
    		  break;
    case 'h': cout << "Hotel";
    		  break;
    case 'i': cout << "India";
    		  break;
    case 'j': cout << "Juliet";
    		  break;
    case 'k': cout << "Kilo";
    		  break;
    case 'l': cout << "Lima";
    		  break;
    case 'm': cout << "Mike";
    		  break;
    case 'n': cout << "November";
    		  break;
    case 'o': cout << "Oscar";
    		  break;
    case 'p': cout << "Papa";
    		  break;
    case 'q': cout << "Quebec";
    		  break;
    case 'r': cout << "Romeo";
    		  break;
    case 's': cout << "Sierra";
    		  break;
    case 't': cout << "Tango";
    		  break;
    case 'u': cout << "Uniform";
    		  break;
    case 'v': cout << "Victor";
    		  break;
    case 'w': cout << "Whiskey";
    		  break;
    case 'x': cout << "X=ray";
    		  break;
    case 'y': cout << "Yankee";
    		  break;
    case 'z': cout << "Zulu";
    		  break;
    default:  cout << "Invalid symbol";
    		  break;
    }
    outputString = outputString + " " + readChar;
    readChar++;
    cout << outputString;
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    And would it be easier to use "int", except I'm not sure how...OMG! I am SO stuck! LOL!

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I don't suppose this might help any?
    Code:
    void getPhonetic(char& readChar, string& insertString, string& outputString)
    {
       cout << "Please type in a word to convert: \n";
       cin >> insertString;
    
       for ( int i = 0; insertString[i]; ++i )
       {
          readChar = tolower(insertString[i]);
          switch ( readChar )
          {
          case 'a': outputString += "Alpha ";          break;
          case 'b': outputString += "Bravo ";          break;
          case 'c': outputString += "Charlie ";        break;
          case 'd': outputString += "Delta ";          break;
          case 'e': outputString += "Echo ";           break;
          case 'f': outputString += "Foxtrot ";        break;
          case 'g': outputString += "Golf ";           break;
          case 'h': outputString += "Hotel ";          break;
          case 'i': outputString += "India ";          break;
          case 'j': outputString += "Juliet ";         break;
          case 'k': outputString += "Kilo ";           break;
          case 'l': outputString += "Lima ";           break;
          case 'm': outputString += "Mike ";           break;
          case 'n': outputString += "November ";       break;
          case 'o': outputString += "Oscar ";          break;
          case 'p': outputString += "Papa ";           break;
          case 'q': outputString += "Quebec ";         break;
          case 'r': outputString += "Romeo ";          break;
          case 's': outputString += "Sierra ";         break;
          case 't': outputString += "Tango ";          break;
          case 'u': outputString += "Uniform ";        break;
          case 'v': outputString += "Victor ";         break;
          case 'w': outputString += "Whiskey ";        break;
          case 'x': outputString += "X=ray ";          break;
          case 'y': outputString += "Yankee ";         break;
          case 'z': outputString += "Zulu ";           break;
          default:  cout << "Invalid symbol"; break;
          }
       }
       cout << outputString;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    Thank you! Thank you again! And even THANKS SO MUCH!!!

    Obviously, I was on the right track with the "int" usage, but could you explain to me how I can use "int i" when it's not even declared anywhere? I actually WANT to learn this...

    I actually wouldn't mind the full explanation, since this has given me so much trouble.

    Thanks yet again!
    Rose

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    but could you explain to me how I can use "int i" when it's not even declared anywhere?
    'int i' is a declaration.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Code:
    cout << "Please type in a word to convert: \n";
    cin >> insertString;
    cin >> is programmed to read data until it encounters a whitespace character(space, tab, newline), and then it stops reading data. The very next line in your code is:
    Code:
    cin.get(readChar);
    That says to read the next char and put it into the char variable readChar. Since cin>> stops reading when it encounters a whitespace character, the next char will be a whitespace character, and therfore readChar will be a whitespace character. You asked the user to type in 'a word', so it is likely that the user typed in a word and then hit enter. When the user hits enter, that inserts a '\n' character into the input, like this:

    dog\n

    So cin>> reads in 'dog' and then stops at the whitespace character '\n'. Next, cin.get(readChar) puts the '\n' character in the variable readChar.

    Thereafter, you do this:
    Code:
    readChar = tolower(readChar);
    which because readChar is not an uppercase character does nothing to readChar.

    Then comes your while loop:
    Code:
    while (readChar != '/n')
    Since readChar = '\n' it does not equal '/n', and the while loop executes. In the switch() none of the cases are a '\n' character so the default case is executed:
    Code:
    default:  cout << "Invalid symbol";
    		  break;
    }
    which outputs "Invalid symbol". Then, you add the '\n' character(which remember is a carriage return) to outputString:
    Code:
    outputString = outputString + " " + readChar;
    Then you increment readChar:
    Code:
    readChar++;
    which gives readChar the next highest value in an ascii table of character codes. If you look at an ascii table, a carriage return character is represented by the numeric code 10. Computers are dumb; they can't store characters--they can only store numbers. So to store a character, a computer stores it's numeric ascii code.

    As an exercise, look at an ascii table in the back of your C++ book or online and post what the numeric code is for the letter 'a'. That integer gets stored in a char variable when you do this:
    Code:
    char ch = 'a';
    However, when you try to display the char variable ch, instead of displaying the numeric code stored in the variable, C++ automatically converts the numeric code to the letter 'a' and displays that instead.

    Anyway, readChar++ results in a numeric code of 11 being stored in readChar, which my ascii table says is the numeric code for a vertical line feed character. In any case, your while loop continues forever because incrementing readChar++ never gives it a numeric code that is equal to the numeric code for '/n'(which doesn't exist).

    The main problem with your code is this line:
    cin.get(readChar);
    That says to get a char from cin--where cin is the input from the keyboard. What you want to do is get a char from the word you read into your string variable:
    Code:
    void getPhonetic(char& readChar, string& insertString, string& outputString)
    {
    
    cout << "Please type in a word to convert: \n";
    cin >> insertString;
    You can access the first char in the string using insertString[0], the second character using insertString[1], etc. So a for loop is a good way to step through every character in the string using insertString[i]:
    Code:
    for(int i = 0; i < insertString.length(); i++)
    {
    
         //do something with insertString[i]
    
    }
    Last edited by 7stud; 04-06-2006 at 12:13 AM.

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    Thanks 7Stud...that is VERY helpful. And I actually understood it. Yay!

    Thanks again! You taught me something today!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21