Switch Statements - Output Help!

Hello - This is my first post (I'm a total beginner!) but need some help. I've been working on an assignment to take a user's input string and output the corresponding IACO words. So like "no" becomes "November Oscar".

I've got it working.... mostly.

What happens is that the file compiles, runs and asks the user to enter a word. You do so and it displays the IACO word for the first letter. You press enter and it displays the second one. You press enter and it displays the third one and so on. Sounds ok, but the line spacing is strange - sometimes single spaced and sometimes with a blank line between.

The output currently is :

------------------------
Please enter a word: Tiger <return>
Tango <return>
India <return>

Golf <return>

Echo <return>

Romeo <return>

------------------------

What I was aiming for it to do is to display all of the words that match the letters in the string at once, but I'm not sure how to do that. I've tinkered with the code and just get errors!

I wanted:
------------------------
Please enter a word: Tiger <return>
Tango
India
Golf
Echo
Romeo

------------------------

Can someone point me to where in the following code that I:

- can address the line spacing in the output if I were to leave it as is and have the user press enter to display the IACO words one at a time

- can change it so that the IACO words for the entire string display at once?

Thanks!
M

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//Declarations
	string inString;
	char letter;
	int len, ipos;

	//User Prompt
	cout << "Please enter a word: ";

	// Read User Input 
	cin >> inString;

	//Output
	cout << "The IACO version is: " << endl << endl;
	len = inString.length();
	ipos = 0;

	//Switch logic to display ICAO alphabet based on character letter
	while (ipos <= len - 1)
	{
		letter = inString.at(ipos);
		if (isalpha(letter) == 0)
			cout << "Will skip character " << letter << endl;
		else
		{
			switch (letter)
			{
			case 'a':
			case 'A': cout << "Alpha";
				break;
			case 'b':
			case 'B': cout << "Bravo";
				break;
			case 'c':
			case 'C': cout << "Charlie";
				break;
			case 'd':
			case 'D': cout << "Delta";
				break;
			case 'e':
			case 'E': cout << "Echo";
				break;
			case 'f':
			case 'F': cout << "Foxtrot";
				break;
			case 'g':
			case 'G': cout << "Golf";
				break;
			case 'h':
			case 'H': cout << "Hotel";
				break;
			case 'i':
			case 'I': cout << "India";
				break;
			case 'j':
			case 'J': cout << "Juliet";
				break;
			case 'k':
			case 'K': cout << "Kilo";
				break;
			case 'l':
			case 'L': cout << "Lima";
				break;
			case 'm':
			case 'M': cout << "Mike";
				break;
			case 'n':
			case 'N': cout << "November";
				break;
			case 'o':
			case 'O': cout << "Oscar";
				break;
			case 'p':
			case 'P': cout << "Papa";
				break;
			case 'q':
			case 'Q': cout << "Quebec";
				break;
			case 'r':
			case 'R': cout << "Romeo";
				break;
			case 's':
			case 'S': cout << "Sierra";
				break;
			case 't':
			case 'T': cout << "Tango";
				break;
			case 'u':
			case 'U': cout << "Uniform";
				break;
			case 'v':
			case 'V': cout << "Victor";
				break;
			case 'w':
			case 'W': cout << "Whiskey";
				break;
			case 'x':
			case 'X': cout << "X-ray";
				break;
			case 'y':
			case 'Y': cout << "Yankee";
				break;
			case 'z':
			case 'Z': cout << "Zulu";
				break;
			}

			ipos = ipos + 1;
			void displayChar(char letter);

		}

		cin.get();
		cin.get();
	}
	return 0;
}
Last edited on
The reason you have to press enter twice for each letter is because you call cin.get() twice.
Aha! Thanks!
I removed one of the two cin.get() 's and now get :

Please enter a word: Tiger <return>
TangoIndia (appear immediately with no space between)
Golf <return>
Echo <return>
Romeo <return>

Any guess as to why it's treating the first letter/word differently than the rest of the string? It was doing that before as well with the spacing?
It's because the >> operator reads only the next word from the input stream so when you input the word and press enter the newline character is left untouched. cin.get() reads the next character from the input stream. The first time there is already a newline character in the buffer so there is no point in waiting for more input so it returns right away.

What you could do is that after you have used operator >> make sure to remove the newline character using cin.get(), cin.ignore(), or similar.
Perfect!

Thank you so much! That worked.
Topic archived. No new replies allowed.