comparison with string literal results in unspecified behaviour

#include <iostream>

char satasfied;
bool reAsk;

void satasfiedFunction()
{
cout << "Are you satasfied with your care? Yes or no? ";
cin >> satasfied;
cout << "\n";

satasfied = tolower(satasfied);

if(satasfied == "yes")
{
cout << "Great to hear, have a nice day.";
reAsk = false;
}
else if(satasfied == "no")
{
cout << "Too bad.";
reAsk = false;
}
else
{
cout << "I don't understand. Please answer with yes or no.";
reAsk = true;
}

cout << "\n\n------------------------------------------------------------------------------------------\n";

if (reAsk == true)
{
satasfiedFunction();
}
}

int main()
{
satasfiedFunction();

return 0;
}
Last edited on
char satasfied

A char variable holds a single character, not a string like "yes" or "no." Sounds like it should be defined as a string (#include <string>) or input for satisfied should be a single 'y' or 'n'.

tolower takes in one character at a time, so that code will need to be tweaked if the input is going to be a string.

I might consider having the code in the satisfied function run in a while loop based on the value of reAsk, instead of calling the function recursively if reAsk is true.
Topic archived. No new replies allowed.