Ken Casimiro
Ken Casimiro

Reputation: 85

c++ loops and boolean expressions

I'm having trouble trying to do this assignment for my class for a couple of days now and would like some help.

The assignment is to write a program that informs the user of their acceptance status based on their heigh and weight qualifications depending on their gender.

At the end of the program it wants to output the number of candidates that were accepted and the average of those accepted to the overall number of candidates.

Assignment - https://www.saddleback.edu/faculty/slinker/CS1A/CS1A_Fall2013/Assignment8.pdf

We can't use switch, conditional operators, and selection (only for outputting the correct message to the results). We can only use loops and complex boolean expressions

The problems I'm having is:

  1. If all 3 of my inputs are valid, why are they not outputting if they are accepted and if one of the input (height or weight) or both were rejected then why isn't it outputting it. Is my boolean variable incorrect? If so, how do I fix it.

  2. Why am i unable to exit the loop/program when I enter X. Is my while loop correct or no?

  3. Is there any selection statements I can change into "not selection-statements".

Here is my code

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

    char gender;
    int  height;
    int  weight;
    bool heightOK;
    bool weightOK;
    int candidateCount;
    int validCandidateCount;
    bool invalidGender;
    bool invalidHeight;
    bool invalidWeight;
    float percentOutput;

    candidateCount = 0;
    validCandidateCount = 0;

    cout << "Please enter the candidate's information (enter 'X' to exit)."
     << endl;

    do
    {
        cout << "Gender: ";
        cin.get(gender);

        cin.ignore (1000,'\n');
        invalidGender = ( !(gender == 'm' ||
                            gender == 'M' ||
                            gender == 'f' ||
                            gender == 'F' ));

        candidateCount = candidateCount + 1;

        if(invalidGender)
        {
            cout << "***** Invalid gender; please enter M or F*****" <<     endl;
        }

    }while(invalidGender);

    while (gender != 'X' || gender != 'x')
    {
        candidateCount = candidateCount + 1;

        do
        {
            cout << "Height: ";
            cin  >> height;

            invalidHeight = height < 24 || height > 110;

            heightOK = ((gender == 'm' || gender == 'M') &&
                        (height > 65 && height < 80));

            heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
                                    (height > 62 && height < 75));

            if(invalidHeight)
            {
                cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
                     << endl;
            }

        }while(invalidHeight);


        do
        {
         cout << "Weight: ";
         cin  >> weight;

         invalidWeight = weight < 50 || weight > 1400;

         weightOK = ((gender == 'm' || gender == 'M') &&
                        (weight > 130 && weight < 250));

         weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
                    (weight > 110 && weight < 185));

        if(invalidWeight)
            {

                cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
                   << endl;
            }

         }while(invalidWeight);



        if(heightOK && weightOK)
        {
            cout << "This candidate has been ACCEPTED!" << endl;

            validCandidateCount = validCandidateCount + 1;
        }
        else if (!heightOK)
        {
            cout << "This candidate has been rejected based on the HEIGHT requirement."
                 << endl;
        }
        else if (!weightOK)
        {
            cout << "This candidate has been rejected based on the WEIGHT requirement."
                 << endl;
        }
        else if (!(heightOK && weightOK))
        {
            cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
                 << endl;
        }
        do
        {
            cout << "Gender: ";
            cin.get(gender);
            cin.ignore (1000,'\n');

            candidateCount = candidateCount + 1;

            if(invalidGender)
            {
            cout << "***** Invalid gender; please enter M or F*****" <<        endl;  
            }
        }while(invalidGender);


    }

    cout << validCandidateCount << " candidate(s) accepted!" << endl;

    percentOutput = validCandidateCount / candidateCount;

    cout << "That's " << percentOutput <<"%!" << endl;



return 0;
}

Upvotes: 1

Views: 1120

Answers (2)

Garriga
Garriga

Reputation: 1

The Boolean data types need to be declared true or false. And this needs to be set before iterations.

Also:

The program needs to run before a user can input x to quit.

std:string QUIT;

QUIT = “XXX”;

Then use selection structure.

Upvotes: -1

Chandini
Chandini

Reputation: 550

The main while loop should have and condition.

while(gender !='X' && gender!='x)

And your selection code has wrong conditional statements.

   if(heightOK && weightOK)
    {
        cout << "This candidate has been ACCEPTED!" << endl;

        validCandidateCount = validCandidateCount + 1;
    }
    else if (!heightOK) // you have written else if(heightOK)
    {
        cout << "This candidate has been rejected based on the HEIGHT requirement."
             << endl;
    }
    else if (!weightOK)  // you have written else if(weightOK)
    {
        cout << "This candidate has been rejected based on the WEIGHT requirement."
             << endl;
    }
    else if (!(heightOK && weightOK))
    {
        cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
             << endl;
    }

You should remove that invalidgender condition in the last do while loop, otherwise it will cause an infinite loop even if you wish to exit by pressing X. Instead the invalid gender condition can be placed at the starting of main While loop.

And invalidGender variable should be assigned its value again otherwise it will pick up the previously stored value.

  invalidGender = ( !(gender == 'm' ||
                        gender == 'M' ||
                        gender == 'f' ||
                        gender == 'F' ));

the whole code

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

char gender;
int  height;
int  weight;
bool heightOK;
bool weightOK;
int candidateCount;
int validCandidateCount;
bool invalidGender;
bool invalidHeight;
bool invalidWeight;
double percentOutput;

candidateCount = 0;
validCandidateCount = 0;

cout << "Please enter the candidate's information (enter 'X' to exit)."
 << endl;

  cout << "Gender: ";
  cin.get(gender);

while (gender != 'X' && gender != 'x')
{
    candidateCount = candidateCount + 1;

    do
    {
        invalidGender = ( !(gender == 'm' ||
                        gender == 'M' ||
                        gender == 'f' ||
                        gender == 'F' ));

        if(invalidGender)
        {
           cout << "***** Invalid gender; please enter M or F*****" <<        endl;  
           cout << "Gender: ";
           cin>>gender;
           cin.ignore (1000,'\n');
        }
    }while(invalidGender);

    do
    {
        cout << "Height: ";
        cin  >> height;

        invalidHeight = height < 24 || height > 110;

        heightOK = ((gender == 'm' || gender == 'M') &&
                    (height > 65 && height < 80));

        heightOK = heightOK || ((gender == 'f' || gender == 'F') &&
                                (height > 62 && height < 75));

        if(invalidHeight)
        {
            cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****"
                 << endl;
        }

    }while(invalidHeight);


    do
    {
     cout << "Weight: ";
     cin  >> weight;

     invalidWeight = weight < 50 || weight > 1400;

     weightOK = ((gender == 'm' || gender == 'M') &&
                    (weight > 130 && weight < 250));

     weightOK = weightOK || ((gender == 'f' || gender == 'F') &&
                (weight > 110 && weight < 185));

    if(invalidWeight)
        {

            cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400."
               << endl;
        }

     }while(invalidWeight);



    if(heightOK && weightOK)
    {
        cout << "This candidate has been ACCEPTED!" << endl;

        validCandidateCount = validCandidateCount + 1;
    }
    else if (!heightOK)
    {
        cout << "This candidate has been rejected based on the HEIGHT requirement."
             << endl;
    }
    else if (!weightOK)
    {
        cout << "This candidate has been rejected based on the WEIGHT requirement."
             << endl;
    }
    else if (!(heightOK && weightOK))
    {
        cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements"
             << endl;
    }


        cout << "Gender: ";
        cin>>gender;
        cin.ignore (1000,'\n');

}

cout << validCandidateCount << " candidate(s) accepted!" << endl;

percentOutput = (double)validCandidateCount / (double)candidateCount;

cout << "That's " << percentOutput*100 <<"%!" << endl;



return 0;
}

Upvotes: 2

Related Questions