Input Problem

Hi there,

I'm learning C++ and I've only recently started, and learning by online tutorials, videos etc. I'm currently trying to learn how to use a Switch case.

I've got a simple question of "what is your favourite colour?".

Answers being 1) Red, 2) Blue etc. The first 4 cases work. I am now trying to get the final 5) Other to work. I want to be able to ask the user for the "Other" colour they prefer, and then for it then to come up with "your favourite colour is 'Whatever the input was'".

I've got the same code what's used for the rest of my cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
switch (A){
case 1: 
        cout << "Your favourite colour is Red!" << endl;
	break ;
				
case 2:	
       cout << "Your favourite colour is Blue!";
       break ;

case 3: 
	cout << "Your favourite colour is Green!";
	break ;
				
case 4: cout << "Your favourite colour is Yellow!";
	break;

case 5: 
        cout << "Another colour? Which colour is your favourite?";
	cin >> B;
	cout << " Your favourite colour is " << B << endl;
	break;
	}
}


But for some reason, I probably am making a stupid mistake here, but the response I get from the console application when the 5th case is used, is..

Another colour? Which colour is your favourite? Your favourite colour is 0.
Press any key to continue.

Am I not able to gain input twice, or am I using wrong data types?

Like I said I'm a beginner so any advice/help would be useful.

Thanks in advance.

Brad.
What is "B"? You'll need to show us more context to be able to say what the problem is with any certainty. Are you by chance using getline() to read in "A"? That's a common issue when mixing getline() with the stream extraction operator.
I used B as a separate variable to hold the user input. For the first part of the program I have this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

	int A;
	int B;

int main(){
	cout << "What's your favourite colour?" << endl;
	cout << "1) Red" << endl;
	cout << "2) Blue" << endl;
	cout << "3) Green" << endl;
	cout << "4) Yellow" << endl;
	cout << "5) Other" << endl;
	cout << "Enter a Choice:\n";
	cin >> A;


So it uses the input to become variable A, that bit works fine.

I tried to do the same with the B variable, but is that wrong? It's not letting me input anything after asking what the "other" is.
Wow I really don't know, I guess only switch statements only work with certain types. I tried to call a function for grabbing the users input and it ignores the getline(cin, mystr); . Only with certain types
Last edited on
String b at the top of the program?

And where would the "getline(cin,b); go?
String b at the top of the program?

No, just before you use it (i.e. before getline).

And where would the "getline(cin,b); go?

Wherever you want to read a line from the user.
Arthar check my post again, I edited it with what I have tried and I was wrong I suppose. Only certain types are accepted in switch statements not strings I guess.
No, you were right. He is trying to read an int when he actually wants a string.
So basically:
1
2
3
4
5
6
7
8
9
10
#include <string>
[...]
case 5: 
{
  cout << "Another colour? Which colour is your favourite? ";
  string b;
  getline(cin,b);
  cout << " Your favourite colour is " << b << endl;
  break;
}
closed account (z05DSL3A)
Only certain types are accepted in switch statements not strings I guess.

Only integer types.
Brad I guess you will have to just declare a void function for initializing your string that you want to output. But I don't even know if that will work, also you can add the default case to your switch its like "else" in a if & else statement.

Forgive me about my terminology of stuff I'm still learning.
Last edited on
No, you were right. He is trying to read an int when he actually wants a string.
So basically:
1
2
3
4
5
6
7
8
9
10
#include <string>
[...]
case 5: 
{
  cout << "Another colour? Which colour is your favourite? ";
  string b;
  getline(cin,b);
  cout << " Your favourite colour is " << b << endl;
  break;
}



Tried this, copied and pasted it in but it still isn't working for me? I've added the #include <string> at top which allowed it, but it's still just rushing straight through without letting the user input?
You can't use getline() in switch statements. Try to call a function, and use a default case for your switch.

Heres a small template you just need to create your function and call it here, return your string and cout it. Maybe it will work idk, only way I could think of bypassing something this strict.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (A){
		case 1:
			cout << "Your favorite colour is Red";
			break;
		case 2:
			cout << "Your favorite colour is Blue";
			break;
		case 3:
			cout << "Your favorite colour is Green";
			break;
		case 4:
			cout << "Your favorite colour is Yellow";
			break;
		default:
			cout << "What is your favorite colour then? ";
                        //for example cin >> mycolor(string mystr);
                        break;
	}


Last edited on
I think I'll just leave it and carry on the tutorials instead of getting hung up on it.

Thanks for the help anyway. :).

Much appreciated.
Brad.
Tried this, copied and pasted it in but it still isn't working for me? I've added the #include <string> at top which allowed it, but it's still just rushing straight through without letting the user input?


Ah yes, you're right. However, that's completely unrelated to switch and rather boils down to the fact that standard C++ iostreams suck. The more immediate reason is that the line break from hitting return after typing in 5 is still in the input buffer and so getline reads in an empty string. cin.ignore(); fixes the problem (at least in this case).
closed account (z05DSL3A)
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
#include <iostream>
#include <string>

using namespace std;

int   main()   
{
    cout << "What's your favourite colour?" << endl;
	cout << "1) Red" << endl;
	cout << "2) Blue" << endl;
	cout << "3) Green" << endl;
	cout << "4) Yellow" << endl;
	cout << "5) Other" << endl;
	cout << "Enter a Choice:\n";
	int A;
    cin >> A;      // This leaves 'junk' in the stream
    cin.ignore();  // <--- Read up on this

    switch (A)
    {
    case 1: 
        cout << "Your favourite colour is Red!" << endl;
        break ;

    case 2:	
        cout << "Your favourite colour is Blue!";
        break ;

    case 3: 
        cout << "Your favourite colour is Green!";
        break ;

    case 4: cout << "Your favourite colour is Yellow!";
        break;

    case 5: 
        cout << "Another colour? Which colour is your favourite?";
        string B;
        cin >> B;
        cout << " Your favourite colour is " << B << endl;
        break;
    }

    return   0;
} 


Edit: A bit slow on typing
Last edited on
Thanks Grey
Topic archived. No new replies allowed.