C++ if statement help

Before I get in to this I'd just like to say, please be mindful of technical explanations. I'm quite new to the language of C++ and my knowledge is fairly limited. Also, please bear in mind that I can't replace the if statement with a switch or anything like that as I am required to include this statement.

I'm currently studying and having to learn C++ for one of my units. The problem I'm having is with the if statement.

What I want to achieve is that when a user inputs either a, b or c, the program outputs a corresponding value (50, 150 or 100), but the problem is that no matter what, the program seems to output the value of the final condition.

So, for example, if I input a (value set to 50), but the final condition is c, the program outputs c's value over a, regardless of the users input.

I've attempted a couple of variations as below:

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
  oid buy_tickets()
{
   system("cls");
     cout<<"\nPlease choose the type of ticket you would like to purchase:";

     cout<<"\n\n\t\t\t a - Day Ticket";
     cout<<"\n\n\t\t\t b - Weekend (camping)";
     cout<<"\n\n\t\t\t c - Weekend (non-camping)";
     cin.get();
    cin.ignore();

     if (cin=="a"){
        cout<<a;
        cin.ignore();
        cin.get();
       }
        
     else if (cin=="b") {
        cout<<b;
        cin.ignore();
        cin.get();
     }
     
     else {
        cout<<c;
        cin.ignore();
        cin.get();
    }
    
        
}



And

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
void buy_tickets()
{
	system("cls");
	cout<<"\nPlease choose the type of ticket you would like to purchase:";
     cout<<"\n\n\t\t\t a - Day Ticket";
     cout<<"\n\n\t\t\t b - Weekend (camping)";
     cout<<"\n\n\t\t\t c - Weekend (non-camping)";
     cin.get();
	 cin.ignore();
     if (a==50){
     	cout<<a;
     	cin.ignore();
     	cin.get();
		 }
     	
     else if (b==150) {
     	cout<<b;
     	cin.ignore();
     	cin.get();
     }
     
     else if (c==100) {
     	cout<<c;
     	cin.ignore();
     	cin.get();
	 }
	 
	 else {
	 	cout<<"Sorry, you have chosen an invalid selection";
	 	cin.get();
	 }
     	
}
Your code doesn't seem to be making any use of the input supplied by the user.
Store the input in a variable and then test the value of that variable.
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
void buy_tickets()
{    
    char choice = ' ';

    cout << "Please choose the type of ticket you would like to purchase:\n"
         << " a - Day Ticket\n"
         << " b - Weekend (camping)\n"
         << " c - Weekend (non-camping)\n";
         
    cin >> choice;
    
    if ( choice == 'a')
    {
     	cout << "Day Ticket\n";
    }
    else if (choice == 'b')
    {
        cout << "Weekend (camping)\n";
    }
    else if (choice == 'c')
    {
        cout << "Weekend (non-camping)\n";
    }
    else
    {
        cout << "Sorry, you have chosen an invalid selection\n";
    }
     	
}


I'm going to work off the first attempt because it seems more correct for the most part.
So a couple things: unless you have a global variable in your code, it looks like you aren't assigning the value of the input to anything. So use the char datatype with whatever name you'd like. For example:

1
2
3
4
5
6
7
char input;
cin.get(input);

if (input==a)
{
//code
}

and so on.

The problem you're getting in that first example with everything you try going to that last statement is because your code is stating either cin (which it thinks is a variable) is 'a', 'b', or ANYTHING else, and it is going with the latter since the code is not properly set up.

I also don't entirely understand why you're asking for a new input in each statement.
Last edited on
char input;
I'd recommend cin >> input rather than cin.get(input).
Both get a character, but the first has the advantage of skipping whitespace, newlines etc.
I would've said the same, I just wasn't sure if OP was using it for a specific reason e.g. teacher specifications. But yeah, use cin in this case if you don't absolutely need to use cin.get.
Thank you both. I appear to have successfully reached the desired result I was looking for.

(also, I declared the values of a, b and c globally)

Chervil: your version wasn't quite what I needed but it set me on track and I found a viable way of getting it to fit my needs:

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
void buy_tickets()
{    
system("cls");
    char choice = ' ';

    cout << "Please choose the type of ticket you would like to purchase:\n"
         << " a - Day Ticket\n"
         << " b - Weekend (camping)\n"
         << " c - Weekend (non-camping)\n";
    
    cin >> choice;
    
    if ( choice == 'a')
    {
     	cout << "Day Ticket: " <<pound <<a <<endl;
    }
    else if (choice == 'b')
    {
        cout << "Weekend (camping): "<<pound <<b <<endl;
    }
    else if (choice == 'c')
    {
        cout << "Weekend (non-camping): "<<pound <<c <<endl;
    }
    else
    {
        cout << "Sorry, you have chosen an invalid selection\n";
    }
    cin.get();
    cin.ignore();
     	
}

@DigitalDaze
One thing that is good here, is that you put the
1
2
    cin.get();
    cin.ignore();
just once at the end, rather than repeating the same code inside each block. Keeping things as simple as possible is usually a good guide.

I'm not sure how you are being taught or what guidelines you are asked to follow. As you learn more you will probably find better ways of doing things than to use global variables and system(anything). These are generally not considered not good practice. Please don't take this personally, it's more a comment just so you are aware for the longer term.

Topic archived. No new replies allowed.