Error with switch statement

So I'm writing this code that takes a phrase and translates it into Pig Latin and will do that until the user decides to stop. Im using a switch statement for the choice between (T) for translate and (Q) for quit. My default and (Q) switches work great, but when I use my (T) option it just prints out the cout line then goes back to the menu. Any ideas why or how to fix?
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
#include<iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
using namespace std;


// Prototype for the function
void printLatinWord(char *);

int main()
{
	// Declare variables
	char phrase[20];
	char *token = NULL;
	char *token2 = NULL;
	char option;

	while (1)
	{
		fflush(stdin); // Used to clear input
		cout << "Luke's Pig Latin Translator\nPress (T) to translate a phrase into piglatin.\nPress (Q) to quit.\nChoice: ";
		cin >> option;

		switch (option)
		{

		case 'T':

			// Prompt the user for input
			cout << "Enter a phrase to be translated into piglatin: ";

			// Read the input from the user
			cin.getline(phrase, 200);

			// Extract the first token
			token = strtok_s(phrase, " ", &token2);

			// While there are tokens in "string"
			while (token != NULL)
			{
				// Call the print latin word function
				printLatinWord(token);

				// Insert a blank according to the phrase
				cout << " ";

				// Get the next token
				token = strtok_s(NULL, " ", &token2);
			}
			cout << endl;
			break;

		case 'Q':
			cout << "Thank you! Now quitting..." << endl;
			system("pause");
			exit(1);

		default:
			cout << "That is not a valid option. Please enter either (T) or (Q)" << endl;
			break;

		}
	}
	return 0;
}

// Prints the pig latin word
void printLatinWord(char *letter)
{
	// sepearate the first letter
	char firstletter = *letter;
	// print the rest of the word, then the first letter, then ay
	cout << ++letter << firstletter << "ay";

}
> char phrase[20];
...
> cin.getline(phrase, 200);
Lying about your buffer sizes won't do you any good.

> fflush(stdin); // Used to clear input
a) mixing C and C++ is bad
b) fflush() isn't defined for input streams anyway. Don't expect this to work wherever you go.

cout has a compatible c++ flush. cin has other tools that do what you want, like clear or ignore.
Ok I fixed the buffer size and removed the flush. My (T) case still won't execute properly.
You need to call ignore on cin. When you did cin >> option, the char was consumed, but the new line character was left. When you called cin.getline(), you consumed the new line character. The ignore function (look it up) will clean out the new line character for you.
Thank you!
Topic archived. No new replies allowed.