• Advertisement
Sign in to follow this  

I need them helps... again!

This topic is 2039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
using namespace std;
int main()
{
char a;
char b = "y";
char c = "n";

cout << "Am I a troll? (n/y)";
cin >> a;
if(a == b){
cout << "?????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????" << endl;
cout << "???????????????????????????????";
}
if(a == c){
cout << "D'aw";
}else{
cout << "Am I a troll? (n/y)";
cin >> a;
}

return 0;
}


I need help, what this program is meant to do is to ask if I'am a troll and if the answer == "y" then it will print out the ASCII troll face(:D) and if the answer == "n" then it will print "D'aw" and if answer is anything else then it will ask again (I'm 99% sure that the else par I made is completly wrong so yeah, I'am a newbie in C++) BUT I get an error in line 8 and 9 or the lines with "char b = 'y' " and "char c = 'n' " it says "invalid conversion from 'const char*' to 'char'"

Share this post


Link to post
Share on other sites
Advertisement
You confused strings with characters. They are not the same thing.
char b = "y"; // put addres of first element of array{'y', 0} in variable b. b is not a pointer thus invalid assignment.
char c = "n"; // same

char d = 'y'; // correct
As a side note, you can compare char literals directly inside if conditional evaluations. char is not magic, it's 8 bit storage which, when fetched to cout is used to look up a glyph to display. What the quoting does is to tell the compiler to look inside for a char/escape sequence and encode it as appropriate storage.
Similarly, double-quoting informs the compiler to build a string.
They are two very different things. Be careful in particular that you cannot trivially compare raw c-strings (usage of [font=courier new,courier,monospace]std::string[/font] is encouraged).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement