sergiocava
sergiocava

Reputation: 27

Comparing char and constant char

I'm new to c++ and programming I keep getting an error. I'm not able to compare char and constant chars. Any help will be appreciated.

char a;
cout << " presentation";
cout << "blablabla do you want to go left (type "l") or right (type "r")";
cin >> a;
if (a == "l")
{
  cout << "blablabla fall down it and brake your neck";
}
else
{
  cout << "blablabla";
}

Upvotes: 0

Views: 1376

Answers (3)

hvanbrug
hvanbrug

Reputation: 1301

You are comparing a char to an array of chars ("1" is an array of chars). The best bet is to probably change your if statement to:

if (a == '1')

that should solve your problem.

Also, you don't need the semicolons in front of the cout statements. ; should go at the end of the statement, not in front of it.

Also, quotes inside of char arrays should be escaped by putting a \ in front of them. ( \" )

Upvotes: 3

TypeIA
TypeIA

Reputation: 17248

Also you have double-quotes inside the string you're trying to print. You need to escape these by writing \".

Upvotes: 3

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 11492

"1" is a string literal and returns an object of type const char[] (array) not a const char

'1' is a char literal

Upvotes: 3

Related Questions