Total newbie bool problem

I was doing a practice excercise from a book and it asked me to Implement a simple "password" system that takes a password in the form of a number. Make it
so that either of two numbers are valid, but use only one if statement to do the check.

So I type up the following code, and it's accepting anything regardless of it being equal or not to the if statement.

just started out with C++, this kinda confuses but I'm pretty sure it's because I'm not doing something correctly in the if statement.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
	int password;
	
	cout << "Enter either of the two passwords you use: " << endl;
	cin >>password;
	
	if(password == 654321 || 123456)
	{
		cout << "Access approved. Welcome!" << endl;
	}
	else
	{
		cout << "Access denied. Incorrect password, please try again." << endl;
	}
}
You can't chain the || operator like that. The correct syntax is:
if( password == 654321 || password == 123456)

What you are doing is the same as

if( (password == 654321) || 123456 )

Which is the same as

if password is true or 123456 is true..

Which will always be true because both of them are not 0 nor could ever be since 123456 is a constant value.
if pass == 1 || pass == 2
awsome thanks guys !
Last edited on
Topic archived. No new replies allowed.