Switch statement

I am using a switch statement to show what word corresponds with a letter. It isn't working properly though because the output shows the letter again, and then the word on the next line. What am I doing wrong??

include <iostream>

using namespace std;


int main ()
{
char letter;

cout << "Please enter your letter:";
cin >> letter;

cout << "The corresponding word is: " << letter << endl;

switch (letter)
{
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 << "Unifor";
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";
}

return 0;
}
cout << "The corresponding word is: " << letter << endl;
That's why,

You're sending the command to cout to output the letter, then end the line before it gets to your switch statement. what you want is:
cout << "The corresponding word is: ";
cout << "The corresponding word is: " << letter << endl; <--- you're printing the letter here
Since you did include the lower case letters too i would use a toupper on this program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cctype>

using namespace std;

int main( )
{
    char letter;

    cout << "Please enter your letter:";
    cin >> letter;

    cout << endl;

    letter = static_cast<char>( toupper( letter ) );

    cout << "The corresponding word is: ";
Topic archived. No new replies allowed.