It should use the comparison operator == instead of assignment operator =.
The value 5 is assigned to the variable x.
Then the resulting value of the entire expression is 5. Since it is non-zero, it is interpreted as boolean true, and the statement cout<<"A"; is executed.
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
#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, dont double post ;)
By the way, the inconsistent indentation and spacing makes the code hard to read. There are different ways to do this, but choose a style and be consistent - it helps both yourself and others, especially as the code gets longer and more complex.
thanks all....you really help me in this one
could i ask a question ?
how can i compare two strings is the same in c++...i google it...but,it use #include<cstring> and syntax is: strcmp(A,B)==0....i think it is in C not C++
could you tell me more details about why would we should use "char arrays" than "string"
nowaday actually i dont know programer makes password by using "char" or "string"