void function output

I have a program to output the phonetic version of a string input, however I cannot seem to get it to output in one line, it currently is outputting one character of the string at a time on each line. I feel like I am missing something simple, but cannot figure it out. Looking for ideas on how to output the string phonetically at one time.

#include <iostream>
#include <string>

using namespace std;

void GetString(string&);
char letter;

int main()
{
string Phonetic;
bool keepOn = true;
bool firstTime = true;

cout << "Enter a word or statement to be converted to the phonetic alphabet: " << endl;

while (keepOn)
{
cin.get(letter);
if (firstTime)
{
cout << endl;
cout << "The Phonetic version of your string is: " << endl;
firstTime = false;
}
if (int(letter) == 10 || int(letter) == 13)
keepOn = false;
else
{
GetString(Phonetic);
Phonetic += ' ';
}
cout << Phonetic << endl;
}
cin.get();
cin.get();
return 0;
}
void GetString(string& Phonetic)
{
switch (letter)
{
case'a':
case'A': Phonetic += "Alpha";
break;
case'b':
case'B': Phonetic += "Bravo";
break;
case'c':
case'C': Phonetic += "Charlie";
break;
case'd':
case'D': Phonetic += "Delta";
break;
case'e':
case'E': Phonetic += "Echo";
break;
case'f':
case'F': Phonetic += "Foxtrot";
break;
case'g':
case'G': Phonetic += "Golf";
break;
case'h':
case'H': Phonetic += "Hotel";
break;
case'i':
case'I': Phonetic += "India";
break;
case'j':
case'J': Phonetic += "Juliet";
break;
case'k':
case'K': Phonetic += "Kilo";
break;
case'l':
case'L': Phonetic += "Lima";
break;
case'm':
case'M': Phonetic += "Mike";
break;
case'n':
case'N': Phonetic += "November";
break;
case'o':
case'O': Phonetic += "Oscar";
break;
case'p':
case'P': Phonetic += "Papa";
break;
case'q':
case'Q': Phonetic += "Quebec";
break;
case'r':
case'R': Phonetic += "Romeo";
break;
case's':
case'S': Phonetic += "Sierra";
break;
case't':
case'T': Phonetic += "Tango";
break;
case'u':
case'U': Phonetic += "Uniform";
break;
case'v':
case'V': Phonetic += "Victor";
break;
case'w':
case'W': Phonetic += "Whiskey";
break;
case'x':
case'X': Phonetic += "X-ray";
break;
case'y':
case'Y': Phonetic += "Yankee";
break;
case'z':
case'Z': Phonetic += "Zulu";
break;
}
return;
}

There’s a number of issue in your code.
Some of them:

1) Declare your variables only when you need them.
Declaring a variable when you don’t have a good value to initialize it at, makes your code harder to read and introduces subtle bugs.
Declaring all the variables in a bunch at the beginning of functions is a bad habit.

2) Do not use “using namespace std;”.
Another bad habit. Leave it to the experts.

3) Make “letter” local.

4) You don’t need “keepOn”.
Your loop condition is “ std::cin.get( letter ) && '\n' != letter ”.

5) “firstTime” is supposed to be used before giving the answer, i.e. after the loop - anyway, outside it.

6) In the ANSI table there’s a bunch of unwanted characters between the 'Z' and the 'a'.
You need to improve a lot your ‘if’ condition to ensure you’re dealing only with the the English alphabet.

You could consider std::library functions like std::isspace(), std::isalnum() and so on.
More details here:
http://www.cplusplus.com/reference/cctype/
https://en.cppreference.com/w/cpp/header/cctype

Otherwise I think the logic should be:
if     letter is less than 'A'
    or 'z' is less than letter
    or (     'Z' is less than letter
         and letter is less than 'a' )



7) do use the tags [ code] [/ code] to get better answers.


EDIT
What’s bad in lastchance’s code here?
http://www.cplusplus.com/forum/beginner/268907/
Last edited on
Topic archived. No new replies allowed.