### can't figure out why only last part of if-else statement is doing anything?

hi, im learning c++ and this is part of a project i'm doing. i cannot figure out why only the last part of this if else statement is doing anything. no matter what I enter as the sex, it only ever says the sex is R.
I have tried flipping it the other way around but then it only ever displays T as the sex.

i don't know what i've done wrong, thanks for help. this is the part i can't figure out

 ``1234567891011`` `````` if (sex = 3) //this decides what sex factor to use {sexDisplay = "T"; sexfac = 0.53;} else if(sex = 2) {sexDisplay = "S"; sexfac = 0.25;} else (sex = 1); {sexDisplay = "R"; sexfac = 0.77;}``````

and the full code here just in cade

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` `````` //Lab Project 5 #include #include "PSProjectOne.h" using namespace std; int main() { bool repeat = 1; double kilo; double age; double bLevel = 0; double sexfac; int sex; string sexDisplay = "N"; while (repeat == 1) { cout << "How much do you weigh in kilograms?\n"; cin >> kilo; cout << "How many solar cycles old are you?\n"; cin >> age; cout << "What is your sex? Enter 1 for R, 2 for S or 3 for T.\n"; cin >> sex; cout << "What is your current Biximent level?\n"; cin >> bLevel; if (sex = 3) //this decides what sex factor to use {sexDisplay = "T"; sexfac = 0.53;} else if(sex = 2) {sexDisplay = "S"; sexfac = 0.25;} else (sex = 1); {sexDisplay = "R"; sexfac = 0.77;} double BMI = (kilo * bLevel * 0.375) / (age * sexfac); cout << "Sex: " << sexDisplay << endl; cout << "Age: " << age << " cycles\n"; cout << "Weight: " << kilo << endl; cout << "Biximent level: " << bLevel << endl; cout << "Your BMI is " << BMI << endl; if (BMI < 75) cout << "Mild case. No treatment required. If any symptoms persist after a week, see a doctor.\n"; else if (BMI >= 75 && BMI < 127.5) cout << "Serious case. Please begin anti-Ginjuk medication immediately.\n"; else if (BMI >= 127.5 && BMI < 250) cout << "Acute case. Please seek ultraviolet treatment immediately.\n"; else if (BMI >= 280 && BMI < 375) cout << "Severe case. Please seek cellular replacement therapy immediately.\n"; else if (BMI >= 375) cout << "EXTREME CASE! Hospitalization required. The doctor has already been notified and an ambulance has been called. Expect to be hospitalized for at least 1 quarter cycle." << endl; cout << "Would you like to check another patient? Press 1 for yes, 0 for no.\n"; cin >> repeat; } } ``````
Last edited on
= in if should trigger a warning.
if (x = 3) //assign x the value 3. is x zero? If not, proceed as 'true'.

if(x==3) //is x 3? If so, proceed. this is what you usually want.

you have == in your other ones ...

 hi, im learning c++ and this is part of a project i'm doing. i cannot figure out why only the last part of this if else statement is doing anything. no matter what I enter as the sex, it only ever says the sex is R. I have tried flipping it the other way around but then it only ever displays T as the sex. i don't know what i've done wrong, thanks for help if (sex = 3) //this decides what sex factor to use {sexDisplay = "T"; sexfac = 0.53;} else if(sex = 2) {sexDisplay = "S"; sexfac = 0.25;} else (sex = 1); {sexDisplay = "R"; sexfac = 0.77;}
Last edited on
i just tried to use == instead, but it didn't change anything. it still only displays the last part of the ifelse regardless of what number i put in.
Last edited on
you also have missing an if and a bad ; on the last else.

if (whatever); //conditionally executes the ; which does nothing!
statement; //this ALWAYS EXECUTES

and human readables are not c++ accepted:
if(x && y == 3) //if x is not zero and y is 3, not 'if x and y are both 3')

if(whatever)
statement
else if(another)
statement2
else (condition without if that is ignored)
{
//stuff that always happens
}

you will get used to it. {} and () may or may not actually DO anything beyond marking code for readability. (sex = 1); is the same as sex = 1; and actually makes it harder to read.
{sexDisplay = "R";
sexfac = 0.77;} //the {} pair do nothing here, and mislead the reader as well, because there is no 'outer statement' for the 'block of code' inside the {} pair.

it is OK to occasionally use {} to set off a block of code, to scope a variable or make it easier to read, but this is unusual (for readability) and serves a purpose for scoping (still, do not do this excessively). Here, its just a mistake though.
Last edited on
i'm not sure which thing you just said fixed it, but it's working now!

thank you so much, i really appreciate it!
good! Make sure you understand this, it is IMPORTANT.
c++ compiler ALLOWS you do make these mistakes (with a warning) because they are legit code structures. So you need to understand the proper format for conditional blocks to avoid these mistakes again, as they will compile but fail to work properly if you do not do it right.

Topic archived. No new replies allowed.