-1

I'm trying to take user input and check if its within a set of characters. if so then it should output some code

I've gotten past a few left or right handed error codes but now I cant seem to get it to recognize the set of initials.

    string gend = ""; //input
string Gend = gend.substr(0, 1); //accepts first character of input
string initial = 'm', 'M','f', 'F';

    cout << "Gender (M or F):" << endl;

cin >> gend;

while (Gend != initial){
    cout << "Wrong gender input. Please enter again. Gender (M or F):" << endl;
    cin >> gend;
    if (Gend == initial ) {
        (SOME CODE);

Using string has eliminated the error codes I was receiving. i.e. C2227 and C2228

DHowell
  • 17
  • 4

1 Answers1

1

Rather than debug the given code, because there is a lot wrong with it, let's show you a couple easier ways to do this.

Maker a function that gets gender. This makes it really easy to stay in a loop until you have valid input. If you get something you can use, you return from the function. If not, you loop around and ask again.

For input of a single character, read in a char, not a string, unless you have a compelling reason to read the character in as a string.

Consider using std::tolower or std::toupper to convert the char into a fixed case so that you don't have to test both cases. But for something really simple like this I would use a switch statement and test all 4.

Note that I'm always using the fully qualified name complete with identifier. This is because using namespace standard; can cause some really interesting bugs.

char getGender(std::istream & in) // using a generic input stream so this function can be used 
                                  // with any input method
{
    while (true) // keep looping until the user gives valid input.
    {
        char gend; // only need one character, just read one character 
        if (in >> gend) // read the gender and test to be sure the stream isn't broken
        {
            throw std::runtime_error("The input stream is broken!");
            // throw an exception to force the program to deal with the broken stream or crash
        }
        switch(gend) 
        {
            case 'm':
            case 'M':
                // do stuff for male case
                return 'm';
            case 'f':
            case 'F':
                // do stuff for female case
                return 'f';
            default:
                cout << "Wrong gender input. Please enter again. Gender (M or F):" << endl;
                break;
        }
    }
}

There is no way out of this function without an 'm' or 'f' other than the exception, and that needs special handling.

If all you want is an 'm' or and 'f' and you don't care which at this time, you can simplify things a bit with the std::strchr function.

char getGender(std::istream & in) // using a generic input stream so this function can be used 
                                  // with any input method
{
    while (true) // keep looping until the user gives valid input.
    {
        char gend; // only need one character, just read one character 
        if (in >> gend) // read the gender and test to be sure the stream isn't broken
        {
            throw std::runtime_error("The input stream is broken!");
            // throw an exception to force the program to deal with the broken stream or crash
        }

        if (std::strchr("mMfF", gend) != NULL)
        {
            return std::tolower(std::static_cast<unsigned char>(gend);
        }
        else
        {
            cout << "Wrong gender input. Please enter again. Gender (M or F):" << endl;
        }
    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54