| Author |
RE: c++ array phonetic alphabet program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
|
Xunxen wrote:
why not just use getline(null,cin)?
then you would have to press return to continue.
it isnt working either.
I did find this from something Jim posted earlier in another of my threads I seemed to have missed (Thanks Jim!)
cout << "Press any key to continue..." << endl;
cin.ignore(1,'\x10');
while(!(cin.get() > 0)) {}
Now I just need to figure out how to get it to skip over spaces and continue as well as stop saying AAlpha and BBravo and so on.

Edited by vegeta_man111 on 10-03-11 04:09 |
|
| Author |
RE: c++ array phonetic alphabet program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
updated! fixed the letter problem. Putchar makes it print the char so i was literally printing the uppercase letter then printing the phonetic right after it.
// Chapter X Assignment X
// Corey Hartshorn
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main ()
{
string phonetic[] = { "Alpha", "Bravo", "Charlie", "Delta", "Echo",
"Foxtrot", "Golf", "Hotel", "India", "Juliet",
"Kilo", "Lima", "Mike", "November", "Oscar",
"Papa", "Quebec", "Romeo", "Sierra", "Tango",
"Uniform", "Victor", "Whiskey", "X-ray",
"Yankee", "Zulu" };
string word;
char letter;
cout <<"Input a word or sentence to be output in phonetic form:" << endl;
cin >> word;
for(int x = 0; x < word.length(); x++)
{
letter = word.at(x); // gets each letter by itself
if (isalpha(letter)) // checks to make sure an alphabetical symbol
{
cout << phonetic[(toupper(letter)) - 65] << endl;
} // outputs phonetic alphabet based on alphabetical value
else if (letter == " ");
{
cout << "*SPACE*" << endl;
word = word.substr(x, word.length());
x = 0;
}
else
{
cout << letter << " is not a letter" << endl;
break; // declares not a variable and ends the 'for' loop
}
}
cout << "Press the ENTER key to continue..." << endl;
cin.ignore(1,'\x10');
while(!(cin.get() > 0)) {} // pauses at the end
return 0;
}
started coding a little bit of the else if statement to determine if letter is a space so i can skip over it, but realized i cant compare pointers to integers, which im assuming is meaning i cant compare " " (space) to a character.
 |
|
| Author |
RE: c++ array phonetic alphabet program |
GTADarkDude
Member

Posts: 142
Location: The Netherlands
Joined: 23.02.08 Rank: God |
|
letter is a character while " " is a character array, so indeed, you can't compare them like this. You need single quotes if you want to compare to the space character: ' '. Furthermore, word will never contain any spaces. The >> operator reads until the first whitespace it encounters. (Space, newline, tab..) So when you enter "Hey there!", word contains "Hey", and " there!" remains in the input buffer. This is also why you need to call ignore() first, before calling get().
Use getline() if you want to enter a full sentence.
... |
|
| Author |
RE: c++ array phonetic alphabet program |
Xunxen
Member

Posts: 30
Location:
Joined: 06.03.11 Rank: HBH Guru |
|
try using ' ' instead of " ". double quotes usually mean a string, while single quotes are a char.
you could also do this:
if(!(letter>='A'&&letter<='Z')){
continue;
}
assuming you convert everything to uppercase before hand, this will skip that iteration of the loop if the character is not a capital letter.
just make sure you use getline() if you want spaces.
Edited by Xunxen on 10-03-11 14:24 |
|
| Author |
RE: c++ array phonetic alphabet program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
it seems that when I use getline() that my pause now needs you to enter twice to exit at the end, but nonetheless it works. Updated code:
// Chapter X Assignment X
// Corey Hartshorn
#include <iostream>
#include <string>
#include <ctype.h>
using namespace std;
int main ()
{
string phonetic[] = { "Alpha", "Bravo", "Charlie", "Delta", "Echo",
"Foxtrot", "Golf", "Hotel", "India", "Juliet",
"Kilo", "Lima", "Mike", "November", "Oscar",
"Papa", "Quebec", "Romeo", "Sierra", "Tango",
"Uniform", "Victor", "Whiskey", "X-ray",
"Yankee", "Zulu" };
string word;
char letter;
cout <<"Input a word or sentence to be output in phonetic form:" << endl;
getline(cin, word);
for(int x = 0; x < word.length(); x++)
{
letter = word.at(x); // gets each letter by itself
if (isalpha(letter)) // checks to make sure an alphabetical symbol
{
cout << phonetic[(toupper(letter)) - 65] << endl;
} // outputs phonetic alphabet based on alphabetical value
else if (letter == ' ')
{
cout << "*SPACE*" << endl; // places a space wherever one is
word = word.substr(x, word.length());
x = 0;
}
else
{
cout << letter << " is not a letter" << endl;
break; // declares not a variable and ends the 'for' loop
}
}
cout << "Press the ENTER key to continue..." << endl;
cin.ignore(1,'\x10');
while(!(cin.get() > 0)) {} // pauses at the end
return 0;
}
 |
|
| Author |
RE: c++ array phonetic alphabet program |
Xunxen
Member

Posts: 30
Location:
Joined: 06.03.11 Rank: HBH Guru |
|
instead of doing this:
word=word.substring(x, word.length());
x=0;
wouldn't it be better to just leave the string as it is? that way you have less overhead from the substring method.
|
|
| Author |
RE: c++ array phonetic alphabet program |
vegeta_man111
Member
Posts: 104
Location: Ohio
Joined: 20.07.05 Rank: Apprentice |
|
fixed, thanks. anyone know why i still have to press enter twice at the end to close it? Maybe if I changed to a loop where I could keep inputting new strings until I type # or some symbol like it.
 |
|
| Author |
RE: c++ array phonetic alphabet program |
GTADarkDude
Member

Posts: 142
Location: The Netherlands
Joined: 23.02.08 Rank: God |
|
You can safely remove your cin.ignore() now. getline() discards the newline symbol at the end, so in this case it also empties your input buffer. And hey, surprise, you only have to press enter once now... ;-)
... |
|
| Author |
RE: c++ array phonetic alphabet program |
random
Member
Posts: 6
Location: N-H
Joined: 21.01.08 Rank: Newbie |
|
Why use while-loop for pause? Why not:
cin.sync();
cin.get();
|
|
| Author |
RE: c++ array phonetic alphabet program |
j4m32
Member
Posts: 81
Location:
Joined: 01.05.10 Rank: God |
|
I'd given him a bit of a crap C++ conversion from what I've used in C.
I honestly didn't know about cin.sync();, sorry about that...
Jim, |
|
| Author |
RE: c++ array phonetic alphabet program |
random
Member
Posts: 6
Location: N-H
Joined: 21.01.08 Rank: Newbie |
|
Don't worry about it;)
Just thought it might come in handy.
Edit:
oh yeah, I don't know what you wanted to do with:
cin.ignore();
(I only read the last page) but what cin.sync() effectively does is discard all unread characters in buffer.
Edited by random on 12-03-11 00:10 |
|
| Author |
RE: c++ array phonetic alphabet program |
Xunxen
Member

Posts: 30
Location:
Joined: 06.03.11 Rank: HBH Guru |
|
this whole project seems like it'd be easier with an STL map, then you could just dump the the current character in the string into the map as a key and get the phonetic character back.
cout << phonetic[toupper(word.at(x))] << endl;
seems much neater than
letter=word.at(x);
cout << phonetic[(toupper(letter)) - 65] << endl;
(I feel like that last line needs an explicit type cast, but that could just be me.)
although a map would probably defeat the purpose of the project in the first place.
also, you could use mod 26 to get the integer value of the letter (after converting to upper),but 'A' would be 13, so your phonetic array would have to start with "Alpha" in the 13th index, then "November" would be in the 0th index. it could be fun just to see how much you could confuse your teacher with that...
|
|