Whats Wrong with my code?

#include<iostream>
#include<windows.h>
using namespace std;

int main (void)
{
int password = 6;
cout << "Please Enter Password" << endl;
cin >> password;
if(password > 6){
cout << "Password Accepted!" << endl;
system("pause");
}else{
cout << "Incorrect AcessCode! Shutting Down...." << endl;
system("pause");
return(0);
}
}


When i type 6 it still says wrong password!
What am i doing wrong?

Thanks in advance
You have there the condition
if(password > 6) which means you must type number 7 or greater to pass the condition because of char ">" which means "greater" (not greater or equal) ;)
now when i try it it says password accepted for all entries!
what should i do?
if(password > 6)

if you type 6 if(password>6) mean false.
You maybe want to say by that code
" if the length of the password is greater than 6 then the password is accepted "
Am I right?
Im sorry i dont understand :/

What would i have to do to the code?

i changed it to = but that still doesnt work.
Change it to == which is the equal to operator. If you just put =6 you assigned 6 to password.

see http://www.cplusplus.com/forum/beginner/54735/ for a recent example.
Last edited on
Works!!
Thank you everyone for your assistance!
One more question it still accepts the password if you enter a letter. Is their anyway to fix that?
If you enter a character into an integer via std::cin the outcome becomes unpredictable.
Make your password a std::string and try using that, look into c-strings, or add a cin fail check loop to see if cin had trouble with the last input.

1
2
3
4
5
6
7
8
while (!std::cin) // cin has a badbit flag set.
{
   std::cin.clear(); // reset flags
   std::cin.ignore(0x7FFFFFFF, '\n'); // clear the buffer (you should use std::numeric_limits<std::streamsize>::max() for this. I'm cheating with a max int in hexadecimal.)
   std::cin.sync(); // sync buffer
   std::cout << "ERROR: Invalid Input. Please Retry.\n >";
   std::cin >> userInput;
}
Last edited on
Interesting. I will look into it thank you!
Topic archived. No new replies allowed.