problems with if and and characters

hello guys i made code cause i wanted to see if could make it read chracters and not numbers, beacuase i know there is another way to make this with something like a menu but the user responds in numbers and i wanted to do some thing diferent xd.

#include<iostream>
#include <stdlib.h>
using namespace std;

int main (){

char r [8];

cout<< "is milk white? yes or no?: ";
cin>> r;

if ( 'r'=='no'){

cout<< "im really disappointed"<<endl;
}
else if ('r'=='yes'){

cout<< "im really proud of you"<<endl;
}

else{

cout<< "keep it simple I said yes or no"<<endl;
}

return 0;
}
In your if and else statements you are comparing the character 'r' to the characters 'no' and 'yes'. Those comparisons will ALWAYS fail.
Last edited on
Remove the quotes from 'r' to compare 'yes' and 'no' to the variable r and not a character literal.

Aceix
Hello mastgraft123,

Even removing the single quotes from "r" will not solve your problem because first (yes) and (no) need to be in double quotes. and even then the "==" will not wor with a C style character array. You would need to use the C function "strcmp()" to compare the two.

Another option is to include the header file "string" and define "r" as a std::string and long with enclosing (yes) and (no) in double quotes the "==" will work.

Hope that helps,

Andy
Registered users can post here. Sign in or register to post.