WHY DOES IT ALWAYS SAY "A"...I NEED IT SAY "OK"



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace 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";}
Last edited on
In this code
 
    if (x = 5)


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.
using namespace 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.

I tend to use Allman style, or something close to it.
https://en.wikipedia.org/wiki/Indent_style#Allman_style

example:
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()
{

    system("color e9");

    bool a = true;
    int x;

    cout << "x = "; 
    cin  >> x;

    while (a)
    {
        if (x == 5)
        {
            cout << "A";
        }
        else
        {
            a = false;
        }
    }

    cout << "OK";

}

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++
Assuming the strings have type std::string, you would compare them in the same way you compare mostly everything else (with two equals signs.)

C-strings (and arrays in general) should be avoided.
Last edited on
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"
Topic archived. No new replies allowed.