I'm trying to get this to execute my main function but for the life of me, I can't seem to let is do that. there are no errors when running the code. What it is suppose to do is to prompt a user for gender between m for male and f for female. If a user enters a wrong character he/she will be prompted again until they choose the right character between the two. What is happening with my code is that it only keeps asking for the gender and never gets to the main function.

// Assignment 2 Question 5a
    #include <iostream>
    using namespace std;
    // The required function inputAndValidate should be inserted here.
    void inputAndValidate (char& sex, int& yearMark, int& examMark){
     
    do { cout << "Please enter gender: " << endl;
    cin >> sex;}
    while (sex != 'm' || sex != 'f');
     
     
    }
     
    int main( )
    {
    int yearMark, examMark;
    char sex;
    inputAndValidate(sex, yearMark, examMark);
    cout << "This student is ";
    if (sex == 'm')
    cout << "male and has a year mark of";
    else
    cout << "female and has a year mark of ";
    cout << yearMark << " and an exam mark of "
    << examMark << endl;
    return 0;
    }

I believe the reason for this is the or in the while condition. If you change it to and it will work. If I remember correctly this happens because the compiler evaluates the whole statement and causes a false negative. I know there is a post on here about this problem but I can not remember what it is. Either that or I'm losing my mind which I can't rule out. :)

You'll have to use "&&" (AND) instead of "||" (OR) as OR returns true if any one of it's expressions are true so even if you enter sex='m', although the first expression is false, the second 1 is true as sex!='f' so the entire expression will result in TRUE and the loop will go on.

AND will solve this as it requires both conditions to be true.

commented: Thanks for that. Wasnt thinking striaght. +9

I think you want to do this :--:D

// Assignment 2 Question 5a
#include <iostream>
using namespace std;
void inputAndValidate ()
{
    char sex;
    int yearMark,examMark;
    while (sex != 'm' || sex != 'f'){
        cout << "Please enter gender: " << endl;
        cin >> sex;
        if(sex=='m' || sex=='f')return ;
    }
}
int main( )
{
    int yearMark=100, examMark=100;
    char sex;
    inputAndValidate();
    return 0;
}

I think you want to do this :--:D

// Assignment 2 Question 5a
#include <iostream>
using namespace std;
void inputAndValidate ()
{
    char sex;
    int yearMark,examMark;
    while (sex != 'm' || sex != 'f'){
        cout << "Please enter gender: " << endl;
        cin >> sex;
        if(sex=='m' || sex=='f')return ;
    }
}
int main( )
{
    int yearMark=100, examMark=100;
    char sex;
    inputAndValidate();
    return 0;
}

Unfortunately not. My function was correct it just need the && instead of ||. I appreciate your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.