am having tough time to get the correct answere

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";

return 0;
}
Last edited on
Hello cirro.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

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 these changes and see how it works.

Andy
@cirro

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.
First, please use code tags when posting code. Easier to read, easier to comment. See http://www.cplusplus.com/articles/jEywvCM9/

Your code, with some indentation too:
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
#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 ) { // #A
    cout << "you can enter the bar";
  }
  else if ( age < 18 || cash => 100 ) { // #B
    cout << "the cash is enough but you are too young";
  }
  else if ( 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?
thanks let me try ur ideas
sorry guys this was my first post i will try to follow all the steps next time
Hello cirro,

Not a problem. Everyone learns from mistakes.

Andy
thanks it worked out i appreciate ur help
Topic archived. No new replies allowed.