i cant get this to giv right answer on the second else if .when i age is less than 18 and cash is more than 100 it is saying enter the bar instead of saying u can not... any suggestions?
#include <iostream>
using namespace std;
int main()
{
char name;
int age,cash;
cout<<"how old are you?";
cin>>age;
cout<<"how much cash do you have?";
cin>>cash;
if(age>=18||cash>100)
{
cout<<"you can enter the bar";
}
else if (age<18||cash=>100)
{
cout<<"the cash is enough but you are too young";
}
else if (age<18||cash<100)
{
cout<<"you are too young";
}
else
cout<<"the details are invalid";
Hint: You can edit your post, highlight your code and press the <> formatting button, but you may still need to change the indenting.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
In your if and else/if statements you are trying to compare 2 things. Using (||) means only one side needs to be true. What you want is (&&). This way both sides need to be true.
In the first else/if statement you used "=>" you used this correctly elsewhere, so why did you think it should be different here. It is "!=", ">=" or "<=".
Try changing all of your '||' OR statements to '&&' AND. By saying OR in your first IF, when cash > 100, that statement executes, whether or not age is over 17.
#include <iostream>
usingnamespace std;
int main() {
char name;
int age, cash;
cout<<"how old are you?";
cin>>age;
cout<<"how much cash do you have?";
cin>>cash;
if ( age >= 18 || cash > 100 ) { // #A
cout << "you can enter the bar";
}
elseif ( age < 18 || cash => 100 ) { // #B
cout << "the cash is enough but you are too young";
}
elseif ( age < 18 || cash < 100 ) { // #C
cout << "you are too young";
}
else // #D
cout << "the details are invalid";
return 0;
}
Four cases.
A is true, if age is at least 18 OR you have at least 101 coins.
If cash is more than 100 then A is true, no matter what age you have.
If you are old enough, then A is true, even if you are broke.
Should you require both conditions, old AND rich, for A?