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