C-string not accepting new input within switch case?

Hi, so I've spent hours pouring over different forums and documentation and I cannot for the life of me figure out why during case 'D' it doesn't accept new input for the c-string userString.

There are other functions present in my code but they all work as intended so aren't included here, I left the whole main function in just for context as to what's happening. Option D is supposed to let the user input a new string, and then continue as normal (looping). But instead all it does is display the text for a new entry and then keep looping the menu without allowing any new input to even be typed.

Any hints? Should I have the string input in its own function? (I have tried this, but can't figure out how to return a char...)

EDIT: Also, I am supposed to be using c-strings specifically and passing pointers, if that makes any difference.

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
  int main()
{
	const int SIZE = 31;
	char userString[SIZE];

	cout << "Enter a string (up to 30 characters): ";
	cin.getline(userString, SIZE);

	cout << "You entered: ";
	unsigned int cnt = 0;
	while (userString[cnt] != '\0')
	{
		cout << *(userString + cnt);
		cnt++;
	}
	cout << endl;

	bool cont = true;
	char choice;

	while (cont != false)
	{
		cout << "\nWhat would you like to do?\n-------------------------\n";
		cout << "A) Count the number of vowels.\n";
		cout << "B) Count the number of consonants.\n";
		cout << "C) Count both vowels and consonants.\n";
		cout << "D) Enter another string.\n";
		cout << "E) Exit program.\n";

		cin >> choice;
		choice = toupper(choice);

		int numVowels = vowelCounter(userString);
		int numConson = consonCounter(userString);

		switch (choice)
		{
		case 'A':
			cout << "\nYou selected A.\n";
			cout << "The number of vowels in " << userString << " is " << numVowels << endl;
			break;

		case 'B':
			cout << "\nYou selected B.\n";
			cout << "The number of consonants in " << userString << " is " << numConson << endl;
			break;

		case 'C':
			cout << "\nYou selected C.\n";
			cout << "The number of vowels in " << userString << " is " << numVowels 
				<< " and the number of consonants is " << numConson << endl;
			break;

		case 'D':
			cout << "\nYou selected D.\n";
			cin.getline(userString, SIZE);
			break;

		case 'E':
			cout << "\nYou selected E, goodbye!\n";
			cont = false;
			break;

		default:
			cout << "\n**Please make a valid selection.**\n";
		}
	}
	return 0;
}
Last edited on
Salem, thank you so much. I feel really dumb. I knew about that too. I guess I just psyched myself out. Program works perfectly as intended now!
Registered users can post here. Sign in or register to post.