In line 13, password and password2 are both character arrays. This means that in the comparison password==password2, you're comparing the memory addresses of the starts of those arrays. Obviously, these will never be the same, even if the contents of the arrays are the same.
As wildblue says, use the C standard library strcmp function to compare the strings.
(Even better would be to use the C++ std::string class.)
In the example below (which I'm sure could be improved) I tried to avoid having the same cin or the same cout or the same string comparison in more than one place.
ESSPECIALY WANA SAY THANKS TO "MIKE BOY" AND "WHILE BLUE" FOR DETAILS :)
"CHERVIL" YOUR CODE IS TOO COMPLICATED FOR ME TO UNDERSTAND... BUT I WILL DISCOVER IT IN FUTURE...
ANYWAY I REALLY APPRECIATE YOUR CODES,VERY DETAIL :)
if(x=5){cout<<"A";}. Replace it with if(x==5){cout<<"A";}
Alright now let me explain.
x=5 makes x become 5;
while x==5 is either true or false.
The first one is an expression and the second one is a comparison. ;)
Giving you the same tip, format your code better, avoid lots of blank lines and use tabs properly. Here is your fixed code, I hope this helped you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
#include <stdlib.h> //for the system() thing you got there.
usingnamespace std;
int main(){
system("color e9");
bool a=true;
int x;
cout<<"x= ";
cin>>x;
while(a){
if(x==5)
cout<<"A";
else
a=false;
}
cout<<"OK";
}
Also in ifs with just one instruction curly brackets {} are not neccesary. Also, if you still have any issues you can ask me personally on discord: https://discord.gg/rwhEXqJ
Have fun coding!
Also in ifs with just one instruction curly brackets {} are not neccesary.
They're not necessary, but I'd advise using them anyway. It's very, very easy to introduce a bug into your code by changing an if block to include a second statement, and forgetting to add the braces. Putting the braces in even when you only have one line, protects you from making that mistake in the future.
It's not "banned" as such; I doubt the admins would take any action over it.
But it's a stupid thing to do. It wastes everyone's time, because people answer in one thread, not knowing that someone else has already answered in another. Also, it fragments the discussion across two threads, meaning you're less likely to get the best answer.
Why on earth would you think it was a good thing to do?
> Correcting a code where the programmer skimped on braces can become a nightmare.
Writing correct code is much simpler when we use all the support we can get from tools.
Use an editor that can auto indent, compile at high warning levels, and you have a much better chance of writing correct code than if you rely on whole lot of silly code formatting rules.
main.cpp: In function 'int main(int, char**)':
main.cpp:5:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
if( argc == 1 )
^~
main.cpp:7:9: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
std::cout << "foo foo\n" ;
^~~
whole lot of silly code formatting rules
I’m not a computer :) I find formatting rules helpful for me - maybe I’m silly.
And, yes, I’m a creature of habit and I stuck on my personal set of rule for naming and for indentation, but I wasn’t talking about that; I was just saying, when you need to modify an existing code, if you also need to add a lot of braces, it’s boring. It doesn’t happen only on errors, but even when the code is correct, but you want to add a second line after your if, else, for..., you’ll have to get back and wrap the existing line into the braces. Just boring. Perhaps I shouldn’t have said a nightmare. Sorry.