Enum in combination with switch - help

I'm trying to write a simple program that uses user input to call different cases of a switch, which print different elements of an enum. As far as I could tell I'd have to prompt the user for an int in order to plug this into the switch, but my code just stops right after I input a value! If I manually plug in an int into the switch and delete the cin statement it works, but if I prompt the user to input an integer it skips the switch. Why?

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
#include <iostream>
#include <string>
using namespace std;

enum Flowers {
	daisy,
	daffodil,
	sunflower
};

int main () {
	cout << "Daisy = 0, Daffodil = 1, Sunflower = 2: ";
	int b;
	cin >> b;

	switch (4) {
		case daisy:
			cout << "ITS DAISY!!" << endl;
			break;
		case daffodil:
			cout << "ITS DAFFODIL!!" << endl;
			break;
		case sunflower:
			cout << "ITS A SUNFLOWER!!" << endl;
			break;
		default:
			cout << "No flower :(" << endl;
		}

		string y;
		getline(cin, y);
		return 0;
	}
Last edited on
it would be ideal if we could keep a level of professionalism on the forum. There is no need to embed swearing in your code.

That said, switch(4) is nonsense. If it compiles, it won't work at all.
you mean
switch(b)

you also have a typo on daffodil @ 20.

not necessary but I usually put a max in all enums. It lets you know how many you have, which is sometimes useful for a number of reasons.
Last edited on
I overlooked the swearing, my bad! Edited out. I did mean switch(b). Doesn't work. But switch(4) outputs the default.
it worked just fine for me with switch(b) and fixed typo. If it isnt working, something else is going on with how you compiled it or something; the code itself is no longer the issue. What is happening with those changes for you? Try a rebuild / full build to ensure it makes an up to date executable.

switch 4 will check to see which case 4 falls into. it falls into default, always... you would think the compiler would flag that as a likely error (switch on a constant is just going to do the same thing each go).
Last edited on
I figured out that by removing the getline down at the bottom the program works. Any idea what this might be doing? The way I understand it, getline waits for the user to input something and then stores that into a variable. Why would that interfere?

EDIT: Found that the program had actually been working. Usually I use the getline command to pause the debugger so I can see what I printed. For some reason here it does not do the same thing, and I have to use a breakpoint instead to see what I printed.
Last edited on
The way you have it the getline would've basically done nothing since the newline is left in the input buffer after the cin >> b. So it would've just read the newline and went right on to the return 0.

You say it "doesn't work". What does that mean?
And post your current code that "doesn't work".
Last edited on
The code works. I suppose I don't really understand the rudimentary function of getline. Does it not do anything because cin outputs a new line char? Therefore it doesn't wait?
Post the code that doesn't work so we can see why.

As for getline, cin doesn't "output" anything at all, ever. It's an input function. It's you that entered the newline since you had to hit the enter key after entering a digit. The cin >> b only read the digit, leaving the newline still in the input buffer. The getline then read from the input buffer up to the first newline, and since there was already a newline in the buffer it returns immediately not allowing you to enter anything.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
using namespace std;

int main() {
    cout << "Enter b: ";
    int b;
    cin >> b;
    cin.ignore(1e9, '\n');  // skip up to a billion chars, stopping after reading a newline

    cout << "Hit Enter to continue... ";
    string line;
    getline(cin, line);  // now this should wait.
}


Try it with and without the ignore().

Technically, the ignore should be:
 
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

That makes it ignore up to an infinite number of chars until it reads a newline. The max streamsize value is treated specially for this purpose. (You need to include <limits> for numeric_limits.)
Last edited on
Great thank you! Also it seems I didn't have to include <limits> for numeric_limits!
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
#include <iostream>
#include <string>
using namespace std;

enum Flowers {
	daisy,
	daffodil,
	sunflower
};

int main () {
	cout << "Enter daisy, daffodil, or sunflower: ";
	int b;
	cin >> b;
	cin.ignore(numeric_limits<streamsize>::max(), '\n');
	switch (b) {
		case daisy:
			cout << "ITS A DAISY!!" << endl;
			break;
		case daffodil:
			cout << "ITS A DAFFODIL!!" << endl;
			break;
		case sunflower:
			cout << "ITS A SUNFLOWER!!" << endl;
			break;
		default:
			cout << "No flower :(" << endl;
		}

	
		string y;
		getline(cin, y);
		return 0;
	}
Also it seems I didn't have to include <limits> for numeric_limits!

For your code to be correct, you do in fact need to include <limits>, whether or not your specific compiler complains about it. My compiler won't compile your code until I include it (since that is where is is declared in the standard).

I'm assuming that what you meant by your code not "working" is that the console disappeared right away if you used the cin >> b (due to the extraneous newline), but if you commented out the cin >> b and hardcoded a value for b then it would seem to work since the getline would hold the console open.

Ultimately this is down to the stupidity of your IDE. A decent IDE will hold the console open for you automatically so you don't need to add garbage at the end just to hold it open. Adding that kind of crap just makes the program annoying to people who run console programs directly from the terminal (and therefore don't need anything "held open").
Last edited on
Topic archived. No new replies allowed.