I am confused

#include <iostream>
using namespace std;

int main()
{
int y ;
int x ;
cout <<"enter a valid number"<<endl;
cin >> x ;
if(x==5)
{
cout<<"number is not valid"<<endl;
}
if(x==6)
{
cout<<"number is not valid"<<endl;
}

if (x!=5 || x!=6);
{
cin >> y ;
cout<< y + x<<endl;
}

system("pause");
}

when I compiled this program and run it :

it says : enter a valid a number

if I put 5 it says invalid number

but when I put 6 it adds it to 5 and gives me 11 as a result . Normally it must say invalid number too but it does not .

I am confused why it does not say invalid number instead it adds the 6 to 5 ?? so why

and

how to correct it ?

but when I put 6 it adds it to 5 and gives me 11 as a result . Normally it must say invalid number too but it does not .
The reason for this is that you don't check y the second time.

This has no effect whatsoever: if (x!=5 || x!=6);
1. Semicolon at the end of the line.
2. Use && instead of ||

Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/
"The reason for this is that you don't check y the second time"

can you tell me , in my code , how to check for y ?

"2. Use && instead of ||"

why ?

Yes, you seem a little bit confused.
But, I'm confused too by your code.
Don't be offended, I'm kidding.
Please explain what your program should do.
It should add two numbers?
What values your program should accept for the two numbers?
Is this what you want to do?

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
#include <iostream>
using namespace std;

int main()
{
	int y;
	int x;
	cout << "enter a valid number" << endl;
	cin >> x;
	if (x == 5 ||x == 6)
	{
	        cout << "number is not valid" << endl;
	}

	if (x != 5 && x != 6)
	{
		cin >> y;
		if (y == 5 || y == 6)
		{
			cout << "number is not valid" << endl;
		}
		else
		{
			cout << y + x << endl;
		}
			
	}

	system("pause");
}
konstance (9)

try to compile the code you gave me and then run it and then you will know if that what I want or not

after compiling it and running it . I would like to know the result you get
I did that before posting the code.So i beleive running this code does not provide the functionality you need?Be more specific about what you need out of your code so someone can give you a clear answer.
#include <iostream>
using namespace std;

int main()
{
int y ;
int x ;
cout <<"enter a valid number"<<endl;
cin >> x ;
if(x==5)
{
cout<<"number is not valid"<<endl;
}
if(x==6)
{
cout<<"number is not valid"<<endl;
}

if (x!=5 || x!=6);
{
cin >> y ;
cout<< y + x<<endl;
}

system("pause");
}

when I compiled this program and run it :

it says : enter a valid a number

if I put 5 it says invalid number

but when I put 6 it adds it to 5 and gives me 11 as a result . Normally it must say invalid number too but it does not .

I am confused why it does not say invalid number instead it adds the 6 to 5 ?? so why

and


how to correct it ?


is this Chinese or English ? what you did not understand in the letters that are written in black dark colors?

if you are unable to answer my questions it does not mean that I am not clear with what I want . Now coming to your code :

the output begins by saying:

enter a valid number

when I put 5 , it says number is not valid

then it comes the message : press a key to continue ...

is the purpose of my code along with my questions to do as mentioned above ?? So think about it before you post a senseless reply to me




Why don't you use a debugger?
With a debugger you should locate the problems.
And, please use code tags.
However, I'm not sure what you are trying to accomplish exactly with your program, but here is a more working one:
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()
{
int y ;
int x ;
cout <<"enter a valid number"<<endl;
cin >> x ;
if(x==5)
{
cout<<"number is not valid"<<endl;
}
cin >> y ;
if(y==6)
{
cout<<"number is not valid"<<endl;
}

if (x!=5 && y!=6)
{
cout<< y + x<<endl;
}

system("pause");
}


Please note the differences.
And you should handle correctly when the user enters a non-number.
I am confused why it does not say invalid number instead it adds the 6 to 5 ?? so why
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()
{
    int y ;
    int x ;
    cout <<"enter a valid number"<<endl;
    cin >> x ; ////////////////////////////// get input
    if(x==5) //////////////////////////////// you entered 5
    {
        cout<<"number is not valid"<<endl;
    }
    if(x==6) //////////////////////////////// you entered 5 already, so this check is useless
    {
        cout<<"number is not valid"<<endl;
    }

    if (x!=5 || x!=6); ////////////////////// x is still 5 but it isn't 6 so once again this check is useless
    {
        cin >> y ; ////////////////////////// unprompted cin for y (you entered 6)
        cout<< y + x<<endl; //////////////// x (5) + y (6) == 11
    }

    system("pause");
}
Last edited on
To the OP: you need to pay more attention to what people are telling you.

1) use code tags, to format your code properly and make it readable

2) Look at the line:

if (x!=5 || x!=6);

Firstly, you have a semicolon at the end, which makes it equivalent to writing:

1
2
3
4
5
6
7
8
if (x!=5 || x!=6)
{
  ;
}
{
  cin >> y ;
  cout<< y + x<<endl;
}


However, even if you remove the semicolon, can you think of any value of x for which the condition:

(x!=5 || x!=6) would not be true?


"Why don't you use a debugger?"

Of course I always use a debugger however when I run the code I posted above , it absolutely showed me no errors .

"And, please use code tags " +
"1) use code tags, to format your code properly and make it readable"

I do not know how to use them properly even after reading the link . Should I use them like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;


int main()
{
int y ;
int x ;

....

system("pause");
}


??

"However, I'm not sure what you are trying to accomplish exactly with your program"

The user is asked to enter a number . If the user writes 5 , the console will output number is not valid . If the user writes 6 the console will output number is not valid . If the user writes a number that is not 5 or 6 , the console will output number is valid

The code you gave to me does not provide the wanted result.


"However, even if you remove the semicolon, can you think of any value of x for which the condition:

(x!=5 || x!=6) would not be true?"

yes : 6 or 5

with x == 5 or x == 6 , the condition is false

but why the question ?

my reply to konstance 44 was reported but I do not know why ? so why?

Many thanks to Texan 40 :)



Um, no. If x == 5, then the condition x != 6 is true, and the whole expression is therefore true.

Similarly, if x == 6, then the condition x != 5 is true, and the whole expression is true.

Have you properly understood the difference between the && operator, and the || operator?

You seem to have understood how to use code tags just fine.

You were pretty rude in your response to konstance, so I'm guessing that's why you were reported.
"Have you properly understood the difference between the && operator, and the || operator?"

&& : Called Logical AND. If both of the operands are non-zero, then condition becomes true

|| : Called Logical OR. If either of the operands is non-zero, then condition becomes true

if both operands are non zero the condition is true

if either operands is non zero the condition is true

(x!=5 || x!=6) becomes true if either x is not equal zero

(x!=5 && x!=6) becomes true if both x are not equal zero

"
You were pretty rude in your response to konstance, so I'm guessing that's why you were reported"

I was realistic not rude . The answer Konstance provided me did not in anyway helped me or showed any errors I made or boosted me or made me understood or gave me the wanted result so I called it senseless that all . Instead of konstance reporting me and putting the blame on me he would better revise his own answer because if I were C++ expert I would never give such an answer for people who need help . Better not replying than giving an answer that never help in anyway

Last edited on
So, just to check: do you now understand why there is no possible value for x that would cause (x!=5 || x!=6) to be false?

When people give up their time and effort free of charge, in an attempt to help you, then it behoves you to be courteous and grateful, even if their answer turns out not to be helpful. If you're going to be rude to people on here (and you were rude), then you'll quickly find no-one will want to offer you help.
"So, just to check: do you now understand why there is no possible value for x that would cause (x!=5 || x!=6) to be false?"

because using || logical operator , both x cannot be zero , one of both x should necessary be greater than zero and therefore the condition cannot be false . If I am wrong please correct me

"and you were rude"

You can say whatever you want but ,by all respect to you and the people who help for free , I was not rude . Anyway let's just not talk about it . The next time I will be grateful and courteous :)
Hi,

Just one more thing, to help you out:

Of course I always use a debugger however when I run the code I posted above , it absolutely showed me no errors .


Just so you know, the debugger doesn't print error messages, unless the program crashes. The compiler prints warnings and syntax error messages. This is what people do all the time in order to get an executable program. So the two are different things.

A debugger allows one to step through the code (it must compile properly first) 1 line at a time, with a "watch list " of variables and their values. By stepping through the code, one can deduce what went wrong by seeing where a value is wrong / unexpected. A debugger is very handy, and saves lots of time. Especially when the program runs fine, except it gives the wrong answer - a logical error.

If you are using an IDE, there should be a menu option to run the program in debug mode. If not, then there are command line versions.

Hope this helps :+)
Topic archived. No new replies allowed.