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:
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:
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:
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:
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]
}