简体   繁体   中英

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";
}

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

'1' is a char literal

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

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. ( \\" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM