vendredi 5 avril 2019

Why does an error occur when I try to test if a member variable (string) in a class is empty?

I'm working on a project and need to test one of my class' member variables to verify that the user did indeed enter a string.

I've also tried using (patronName == '') and (patronName == "") but have had no luck.

std::string Restaurant::getPatronName()
{
     bool controlFlag = true;
do
{
    getline(std::cin,patronName);

    if ((std::cin.fail()) || (patronName == '\n'))  // Error occurs here
    {
        std::cout << "You must enter a name!" << std::endl;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    else
    {
        controlFlag = false;
    }
} while (controlFlag);

return patronName;
}

The function should read and store the name entered by the user into patronName. When trying to build, I get an error that says "no match for 'operator=='". Could this be because the object called in main is a pointer of type Restaurant?

Aucun commentaire:

Enregistrer un commentaire