Cizel
Cizel

Reputation: 69

Why would the statement not work

I am a beginner at c++ programming and I am supposed to create a program in which answers that does not meet certain conditions would produce certain statements. I also added

 cin.ignore(INT_MAX, '\n');

in order to use

getline 

and cin

together. However, I think I may have misunderstood the nature of how they work and I used

 cin.ignore(INT_MAX, '\n') 

before using the first getline function, and it was causing my program to pause. I think that I am supposed to use this only if i use cin before, and when I want to use a getline function in order to prevent the getline function from taking in the empty space?

at the start, and it is causing me errors, I'm not sure when I use this. I think this part might be the error... but I'm not quite for how the || and the && operators work

else if (donorGender != "Male" || "Female" || "Trans Male" || "Trans Female" || "Queer" || "Different")

is this it the way I do it?

else if (donorGender != "Male" && donorGender != "Female" && donorGender != "Trans Male" && donorGender != "Trans Female" && donorGender != "Queer" && donorGender != "Different")

or is this the way I do it

Please help...

Upvotes: 0

Views: 96

Answers (2)

H.S.
H.S.

Reputation: 12669

The statement

cin.ignore(INT_MAX, '\n');

is pausing your program at the start until you press Enter key.

From this:

std::istream::ignore

istream& ignore (streamsize n = 1, int delim = EOF);

Extract and discard characters

Extracts characters from the input sequence and discards them, until either n characters have been extracted, or one compares equal to delim.

Generally, cin.ignore(INT_MAX, '\n') used with getline if you have some other input statements using cin before calling getline because when a user inputs something with cin, they hit Enter key and a \n (newline) character gets into the input buffer. Then if your program calling getline, it gets the newline character instead of the string you want. In your program you are is using getline for the first input from the user, so you don't need it. You can safely remove cin.ignore statement from your program.

This statement is wrong:

else if (donorGender != "Male" || "Female" || "Trans Male" || "Trans Female" || "Queer" || "Different")

You need to compare donorGender with all the possible valid values and not only with just one valid value. Even if you compare donorGender != with all valid values this will not work because || operator in the condition will always evaluate to true as a valid value of donorGender will be != rest of all valid values. Change it to:

else if (donorGender != "Male" && donorGender != "Female" && donorGender != "Trans Male" && donorGender != "Trans Female" && donorGender != "Queer" && donorGender != "Different")

With these changes, your program should work as expected. Also, I would suggest you to add some input validation for all the inputs you are taking from the user.

Upvotes: 1

ShivCK
ShivCK

Reputation: 659

Your code says that it will test donorName else donorGender and so on. You need to check all the conditions. and your donorGender checking if statement is not correctly formatted. What if user enters both name and gender invalid!

I think you should not check other conditions if one if false. if you want to tell all the wrong things then approach is different. but in your case below code can help. try it out!

Nested Conditional Statements

if(donorName != "") 
{
    if(donorGender == "Male" 
        or donorGender == "Female" 
        or donorGender == "Trans Male" 
        or donorGender == "Trans Female" 
        or donorGender == "Queer"
        or donorGender =="Different") 
    {
        if(donorAge >= 0) {
            if(donorWeight >= 0) {
                if (donorHeight >= 0)
                {
                    cout << "--- You must enter a valid height." << endl;
                    return (-1);
                }
                else {

                }
            }
            else {
                cout << "--- You must enter a valid weight." << endl;
                return (-1);    
            }
        }
        else {
            cout << "--- You must enter a valid age." << endl;
            return (-1);
        }
    }
    else {
        cout << "--- You must enter a valid gender." << endl;
        return(-1);
    }
} 
else {
    cout << " --- You must enter a valid name." << endl;
    return (-1);
}

Upvotes: 1

Related Questions