What am I doing wrong?

Ok, the program itself works fine. Compiles fine, runs fine. But I need it to make the char choice uppercase, and I need it to delay or wait for user input before going back to the menu. I'm thinking it may have something to do with not formatting the case statements correctly, because a classmate did almost the exact same thing as me, but he had his various case statement outputs in seperate functions. What am I doing wrong?

EDIT: How do I exit the program in the middle of it? For instance, if I wanted case 'Q' to end the program (which I do), what code would i need to do so?

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
#include <iostream>
#include <windows.h>

using namespace std;

char menu(char);
//void doValue(char);

int main()
{
	char choice = ' ';
	menu(choice);
	//doValue(choice);
	do
	{
		//cout << "Enter a choice" << endl;
		//cin >> value;
		choice = (toupper(choice));
	
		switch (menu(choice))
		{
			case 'F': 
				cout << "You choice is F! \n";
				cin.get();
				system("CLS");
			break;
			case 'S': cout << "Congratulations, you chose S!\n";
				cin.get();
				system("CLS");
			break;
			case 'T': cout << "T is your choice? Here, have a cookie.\n";
				cin.get();
				system("CLS");
			break;
			case 'O': cout << "You chose O. La-de-da.\n";
				cin.get();
				system("CLS");
			break;
			//case 'Q': cout<< "[Q]uit\n";
			default: cout << "Not an option. Try again.\n";
			cin.get();
			system("CLS");
		}
	} while ((choice != 'Q')&&(choice != 'q'));
	return 0;
}

char menu(char choice)
{
	cout<<"[F]irst Choice\n";
	cout<<"[S]econd Choice\n";
	cout<<"[T]hird Choice\n";
	cout<<"F[o]urth Choice \n";
	cout<<"[Q]uit\n";
	cout << "Enter a choice" << endl;
	cin >> choice;
	choice = (toupper(choice));
	//cout<<"out of loop";
	return choice;
}

/*void doValue(char choice)
{
	
}*/
Last edited on
cin.get() is not pausing the program because you are always using cin >> before it. cin >> leaves the whitespace character from pressing Enter in the input stream, so when cin.get() is called it immediately takes this whitespace character, essentially making it seem like it does nothing. To fix this, either put cin.ignore() before cin.get(), or put cin.get() twice. cin.ignore() followed by cin.get() would be better, in case there is no whitespace character for the first cin.get() to absorb.

That was probably a terrible explanation, but hopefully you'll understand it.

The following isn't really a problem, but it is something you should be aware of. In your menu function, there is no reason to both pass choice in as a parameter AND return it at the end of the function. You should either not pass it in at all and instead declare a new variable in the function that you then return, or you could pass it in by reference like this:
 
void menu (char &choice)

This way the value of your choice variable in main will be changed to whatever the choice variable in menu is changed to, thus getting rid of the need to return anything. It's up to you to choose which way you would rather do this.

As for how to end the program when the user presses Q, what you're doing right now looks good, but if you ever want to exit the program you can just use return 0; or exit();, which I believe requires something to be included but I don't remember what.
Last edited on
Thank you for your help! I don't have a C++ compiler here, but I'll make the changes as soon as I can access one.

EDIT: The code did work, thank you for your help. I was also able to iron out other kinks that had shown up after I had fixed that one, one of which was the menu showing up twice upon starting the program, caused by line 12, I believe.
Last edited on
Topic archived. No new replies allowed.