C++ | PCSPECIALIST

C++

sted

Rising Star
I'm currently learning C++ and I have to finish off a program, but I'm having a bit of an issue getting an if statement to work correctly. The problem I'm encountering is that I'm trying to get the program to output a value based on the users input, but it always gives one particular value which is incorrect. I've narrowed it down to what seems to be the program always outputting the value of the last part of the statement, rather than the corresponding value of the user input.


In other words (as I'll show below), if the user inputs a, the program should output 50, but instead it outputs the value of c regardless of input.
Unfortunately the break; code doesn't work for if statements like it does for switch, which would have presumably made this easier. Not to mention I have to include the if statement (I need at least one included in the program).

Here is what it looks like at the moment:

Code:
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 (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();
	 }
	 
     	
}
 

Rakk

Moderator
Moderator
Unfortunately the break; code doesn't work for if statements like it does for switch, which would have presumably made this easier. Not to mention I have to include the if statement (I need at least one included in the program).

I haven't used C++, so am not sure entirely what all the commands are doing (I use C#),
But IF statements do not need a break (and never will), if they satisfy the first IF condition (cin=="a" in this case) then the ELSE's will never be run so will never happen.
IF statements really are a case of 'IF this condition THEN DO whatever you tell it do OTHERWISE DO something else'
IF statements are very useful, can be much easier than a Case/Switch statement dependent on what you're doing.

In your case it's always returning c (ie. its doing the 'cout<<c' I think), because the cin=="a" condition is not being satisfied, and neither is the cin=="b" condition.

After some googling, I think you are just not assigning the typed character to a variable
So instead of
Code:
        cin.get();
	cin.ignore();
        if (cin=="a"){
     	    cout<<a;
     	    cin.ignore();
     	    cin.get();
         } .....
You may need
Code:
        cin>>myResult;
	cin.ignore();   
        if (myResult=="a"){
     	    cout<<a;
     	    cin.ignore();
     	    cin.get();
	} ....


Note: as I said I don't code in C++, so this is only what I got from Google, so may not be correct if I've read it wrong - also note: I couldn't get it to compile in an online compiler but again I don't code in C++, I don't know if I'm missing references and I don't know what a, b and c are declared as or where they are even declared :)
Also note: the cin.ignore(), I saw something telling me why this was used, but have lost the webpage, but you may want to check if you actually need it in all the places you have it.
 

Tony1044

Spamtastic
Gosh I haven't done C++ for donkeys, but try testing for switch cases rather than if?

I don't even have any C++ compilers or tools so this is a combination of from memory and Google so apologies if it turns out to be a crock;

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 >> ch;
switch (ch) {
case 'a'+
cout << a;
break;
case 'b'+
cout << b;
break
case 'c'
cout << c;
break;
}
 

sted

Rising Star
I don't know if I'm missing references and I don't know what a, b and c are declared as or where they are even declared

I declared them up at the top so they'd be global:

Code:
#include<iostream>
#include <time.h>

using namespace std;

//global variables for storing details
int contactnum; 
int a = 50, b = 150, c = 100;
char firstname[20];
char surname[20];
char address1[20];
char address2[20];
char postcode[20];

I made changes based on what you shown, but no luck. Thanks for the input!



Dagnaggit. I am not doing too well at that lately.

Haha, thanks for trying to throw me some help either way!



I might have to give in on this one and maybe see if the switch statement works instead, assuming I can use the if statement elsewhere (maybe I can just try and use one for confirming that the user wants to close the program or something haha).
 

Rakk

Moderator
Moderator
I made changes based on what you shown, but no luck. Thanks for the input!

Weird, as the below works for me once I'd figured out how to get the online compiler that I was using to send through an input :)
Code:
#include <iostream>
using namespace std;

void buytickets() 
{
     int a = 50, b = 150, c = 100;
     string MyResult;
     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 >> MyResult;
	 cin.ignore();
     if (MyResult=="a"){
     	cout<<a;
     	cin.ignore();
     	cin.get();
		 }
     	
     else if (MyResult=="b") {
     	cout<<b;
     	cin.ignore();
     	cin.get();
     }
     
     else {
     	cout<<c;
     	cin.ignore();
     	cin.get();
	 }
     	
}

int main(){
    buytickets();
}

PS: I think the problems you are having are not so much to do with the IF statement, it's to do with the conditions you are checking ie. cin=="a" and cin=="b"

Edit: It also works in Visual Studio 2015 (once I included a #include <string> in it) - you can tell I've been busy at work this morning :)
 
Last edited:

Tony1044

Spamtastic
Works for me, too.

Weirdly the first run in the online compiler seemed to use someone else's code..it was asking if I was a manager etc...

Weird, as the below works for me once I'd figured out how to get the online compiler that I was using to send through an input :)
Code:
#include <iostream>
using namespace std;

void buytickets() 
{
     int a = 50, b = 150, c = 100;
     string MyResult;
     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 >> MyResult;
	 cin.ignore();
     if (MyResult=="a"){
     	cout<<a;
     	cin.ignore();
     	cin.get();
		 }
     	
     else if (MyResult=="b") {
     	cout<<b;
     	cin.ignore();
     	cin.get();
     }
     
     else {
     	cout<<c;
     	cin.ignore();
     	cin.get();
	 }
     	
}

int main(){
    buytickets();
}
 

sted

Rising Star
I must have just made a mistake adding it in the first time, as I tried again with what looks like basically the same thing you shown me.
In the end I'd gone with this, which I managed to get working:

Code:
system("cls");
    char choice2 = ' ';

    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 >> choice2;
    
    if ( choice2 == 'a')
    {
     	cout << "Day Ticket: " <<pound <<a <<endl;
    }
    
    else if (choice2 == 'b')
    {
        cout << "Weekend (camping): "<<pound <<b <<endl;
    }
    
    else if (choice2 == 'c')
    {
        cout << "Weekend (non-camping): "<<pound <<c <<endl;
    }
    
    else
    {
        cout << "Sorry, you have chosen an invalid selection\n";
    }
    
    cin.get();
    cin.ignore();
     	
}

Thanks again for the help!
 
Top