Does it matter if I use a string or char for a simple input function? (aka y/n)
This is what I’m using at the moment:
using namespace std;
string somestr;
getline(cin,somestr);
if(somestr.empty())
{ //do something }
else if (somestr == "y"){
//do something else
}
else{}
And if it makes more sense to user char what would be the equivalent char code to this?
Yes, it matters, because
std::stringcannot be compared with acharusing==. You can compare it with a string literal:or you can test the initial element of the
std::string:In the latter case, you might want to check the length as well, otherwise you would accept such inputs as “yacht” and “yellow.” Comparing with a string literal containing the expected text is probably a better choice for most use cases.