A question about If else

Can someone tell me why does this program run even when I press q.this program should not show anything if we press q.

#include<iostream>
using namespace std;
int main()
{
char ch;
cout<<"press c to continue or press q to quit the program ";
cin>>ch;
if (ch='c')
{
cout<<" hello world\n";


}
else
return 0;
}

Last edited on
In your if conditional you need == (equality) not = (assignment).
A good habit to get into is to put your constants on the left hand side of an equality test.

1
2
3
4
5
if ('c' = ch)
{
cout << "hello world" << endl;
}
...


The compiler then flags the obvious error.

Thanks eker676 !!
Topic archived. No new replies allowed.