New programmer -- need help with boolean expressions
Nov 17, 2014 at 11:04am UTC
I have an issue with my boolean expressions. I want my code to be able to specify between male and female parameters, yet it all conjoins into one. I'm not sure how to properly do it.
The area I'm referring to is regarding the heightOk and weightOk - I need help with how to set them up properly so they're independent.
I.e (input for a female would be: f, (any height 62-72), (any weight 110-185)
Any help is appreciated!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
//Variables
int height;
int weight;
char gender;
bool heightOk;
bool weightOk;
int candidateAccepted;
int candidateCount;
float candidateAverage;
int candidatePercent;
cout << left;
candidateAverage = 0;
candidateCount = 0;
candidateAccepted = 0;
do
{
cout << "Please enter the candidate's information (enter 'X' to exit)." ;
cout << endl;
cout << "Gender: " ;
cin.get(gender);
if (gender != 'X' && gender != 'x' )
{
do
{
if (gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f' )
{
cout << "***** Invalid gender; please enter M or F *****" ;
cout << endl << endl;
cin.ignore(1000,'\n' );
cout << "Gender: " ;
cin.get(gender);
}
}while (gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f' );
cin.ignore(1000,'\n' );
if (gender != 'X' && gender != 'x' )
{
do
{
cout << "Height: " ;
cin >> height;
if (height < 24 || height > 110)
{
cout << "***** Invalid height; please enter a height in inches between 24 and 110. *****" ;
cout << endl << endl;
}
cin.ignore(1000,'\n' );
}while (height < 24 || height > 110);
do
{
cout << "Weight: " ;
cin >> weight;
if (weight < 50 || weight > 1400)
{
cout << "***** Invalid weight; please enter a weight in lbs between 50 and 1400. *****" ;
cout << endl << endl;
}
}while (weight < 50 || weight > 1400);
heightOk = ((gender == 'M' && gender == 'm' ) &&
(height >= 65 && height <= 80)) ||
((gender == 'F' && gender == 'f' ) &&
(height >= 62 && height <= 72));
weightOk = ((gender == 'M' && gender == 'm' ) &&
(weight >= 130 && height <= 250)) ||
((gender == 'F' && gender == 'f' ) &&
(weight >= 110 && height <= 185));
cin.ignore(1000,'\n' );
if (heightOk)
{
if (weightOk)
{
cout << endl << endl << endl;
cout << "This candidate has been ACCEPTED!" ;
cout << endl;
candidateCount = candidateCount + 1;
candidateAccepted = candidateAccepted + 1;
}
else
{
cout << endl << endl << endl;
cout << "This candidate has been rejected based on the WEIGHT requirement." ;
cout << endl;
candidateCount = candidateCount + 1;
}
}
else if (!heightOk && weightOk)
{
cout << endl << endl << endl;
cout << "This candidate has been rejected based on the HEIGHT requirement." ;
cout << endl;
candidateCount = candidateCount + 1;
}
else if (!heightOk && !weightOk)
{
cout << endl << endl << endl;
cout << "This candidate has been rejected based on the HEIGHT and WEIGHT requirements." ;
cout << endl;
}
}
}
}while (gender != 'X' && gender != 'x' );
candidateAverage = candidateAccepted / candidateCount;
candidatePercent = candidateAverage * 10;
cout << candidateAccepted << " (s) accepted!" ;
cout << "That's" << candidatePercent << "%!" ;
return 0;
}
Nov 17, 2014 at 11:06am UTC
And yes I am aware I have some faults in my code regarding elegance and some computations toward the end. I'm trying to solve this first before I do anything else
Nov 17, 2014 at 7:33pm UTC
Looks like you're using ands when you should be using ors.
Topic archived. No new replies allowed.