cin.get() not working as it is supposed to

In the following function definition, the compiler is not pausing to take the user input 'c'. However the compiler is pausing for taking the customer's name.

void Ticket::populateTckt()
{
char c;
//Customer's name
cout<<"Enter the customer's name:";
getline(cin,custName,'\n');
cin.ignore();
//Choice of sport
X:
cout<<"Choose the customer's choice of Sport:\n"
<<"A: Skiing\n"
<<"B: Snowboarding\n"
<<"C: Tubing\n"
<<"D: Ice Skating"<<endl;
cin.get(c);
cin.ignore();
switch(toupper(c))
{
case 'A': sportChoice="Skiing"; break;
case 'B': sportChoice="Snowboarding";break;
case 'C': sportChoice="Tubing";break;
case 'D': sportChoice="Ice Skating";break;
default:
{
cout<<"Invalid input!"<<endl;
goto X;
}
}
//payment amount
if(c=='a' || c=='A' || c=='b' || c=='B')
paymentAmt=75.0;
else if(c=='c' || c=='C')
paymentAmt=50.0;
else
paymentAmt=60.0;
}

Thank you in advance and sorry if I made a very silly mistake.
It is doing exactly what it is supposed to do. Remember that your user will always press ENTER after the input. Don't forget about that.

Also, get rid of the goto and use a simple loop like you are supposed to.

Hope this helps.
Thank you for your time Duoas but I still am not able to figure out what the problem is. I've removed the goto statement and kept a while loop but its going to infinite loop.

void Ticket::populateTckt()
{
char c=0;
//Customer's name
cout<<"Enter the customer's name:";
getline(cin,custName,'\n');

cin.ignore();
//Choice of sport
while(c!='q' || c!='Q')
{
cout<<"Choose the customer's choice of Sport:\n"
<<"A: Skiing\n"
<<"B: Snowboarding\n"
<<"C: Tubing\n"
<<"D: Ice Skating\n"
<<"Q: Quit"
<<endl;
cin.get(c);
cin.ignore();
switch(toupper(c))
{
case 'A': sportChoice="Skiing"; break;
case 'B': sportChoice="Snowboarding";break;
case 'C': sportChoice="Tubing";break;
case 'D': sportChoice="Ice Skating";break;
case 'Q': break;
default:
cout<<"Invalid input!"<<endl;
}
}
//payment amount
if(c=='a' || c=='A' || c=='b' || c=='B')
paymentAmt=75.0;
else if(c=='c' || c=='C')
paymentAmt=50.0;
else if(c=='d' || c=='D')
paymentAmt=60.0;
else
paymentAmt=00.0;
}

I'm very new to c++, so I apologize if I made any silly mistakes. I appreciate any help. Thanks in advance.
For looping while loop can be used in this way alos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
do
{
	bool b_ch = true;
	cout<<"Choose the customer's choice of Sport:\n"
		<<"A: Skiing\n"
		<<"B: Snowboarding\n"
		<<"C: Tubing\n"
		<<"D: Ice Skating"<<endl;
		cin.get(c);
		cin.ignore();
		cout<<"\n Please press Enter after you enter the character . ";
		switch(toupper(c))
		{
			case 'A': sportChoice="Skiing"; break;
			case 'B': sportChoice="Snowboarding";break;
			case 'C': sportChoice="Tubing";break;
			case 'D': sportChoice="Ice Skating";break;
			default: 
					{
						cout<<"Invalid input!"<<endl;
						b_ch = false;
					}
		}
}while( ch == false)
"kept a while loop but its going to infinite loop."
that`s becouse your condition " c!='q' || c!='Q' " will allways be true.
i`ll give some examples to try to show you how " || " works.
we take 2 variabiles int a,b;

a || b is true when :
1. a is true (a!=0)
2. b is true (b!=0)
3. a is true and b is true

a || b is only false when neither a nor b is true. (a==0), (b==0)

in your case: if c!='q' is false (that means c=='q') than automatically c!='Q' must be true becouse c cannot have 2 different values at the same time.
and viceversa.
your while loop would work the way you want it to if you change the condition to c!='q' && c!='Q'
for an example we will owrk with the variabiles from above (a and b)

a && b is false when:
1. a is false (a==0)
2. b is false (b==0)
3. a is false and b is false

a && b is only true when a is true (a!=0) and b is true (b!=0) in the same time

i think you know that allready but explained it just in case.
your while loop would also work the way you want it to if you change the condition to !(c=='q' || c=='Q')
and i think that may be what you where aktually thinkimg about when you wrote the condition

hope this helped ( i`m not very experienced in c++ myself )
Last edited on
Thank you very much @someone else, I dint realize my while condition is wrong until I read your explanation. Thanks a lot for taking time to explain this to me with an example.
thank you @bluecoder, I literally used your loop except that I declared bool b_ch outside the loop and only initialized b_ch to true inside the loop and replaced ch with b_ch in the while condition. You saved me a lot of time.
Topic archived. No new replies allowed.