else is being ignored? :o

SOS //sobs internally
it seems like there is a problem with my code as it doesn't give the output as I want. I already asked my friends but they can't help me too.

Supposedly if the BMI >30, gender is F and age >40 the output is C(retire at 55)
However it seems like whenever I run the program, it ignore the else statement and it copy the if statement instead.
In other program, if I run it without array, the output seems fine.
And sorry in case my code is messy and not a good one. I'm still a newb. Do guide me.

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
  /*		Program		: Determine retirement age to choose based on BMI and Mortality Predictor
		Input		: Number of staffs, age, gender, height, weight
		Output		:  */
#include <iostream>
#include <iomanip>
using namespace std;
int a=0; int b=0; //counter for number of staff

double calcBMI(double height, double weight){
	double BMI, IMB;
	BMI = ( weight / (height*height) );
	return BMI;
}

char calcMP(double BMI, int age, char gender){
	char MP , PM ;
	if (BMI>30) //the problem is in here
	 {
		if (age<40) 
		{
			if ((gender=='M') || (gender=='m'))	
			{MP='A'; a++;}
			else 
			{MP='B'; a++;} 
		}
		else		
		{
			if ((gender=='M') || (gender=='m'))
			{MP='B'; a++;}
			else 
			{MP='C'; b++;}
		}
	}		
	else if(BMI<=30)
	{
		if (age<40)
		{
			if ((gender=='M') || (gender=='m'))
			{MP='A'; a++;}
			else
			{MP='A'; a++;}
		}
		else
		{
			if ((gender=='M') || (gender=='m'))
			{MP='B'; a++;}
			else
			{MP='B'; a++;}
		}
	}
	return MP;
}

int main (){
	char gender; int age; char MP;
    int i, j; double IMB[100]; char PM[100];

    cout << "\nHow many staffs you wish to evaluate? " <<endl;
    cin  >> j;

    for (int i=0; i < j; i++)
	{
		cout << "\n\nPlease enter the following:\n";
	
		double height, weight, BMI;
		cout << "Height (in m) : " ; cin  >> height;
		cout << "Weight (in kg): " ; cin  >> weight;
		IMB[i]=calcBMI( height, weight);
		cout<< "Your BMI is "<< IMB[i]<< endl;
	
		char gender; int age; char MP;
		cout << "Gender : "; cin  >> gender;
		cout << "Age : "; cin  >> age;
		PM[i]=calcMP(IMB[i], age, gender);
			if ((PM [i]=='A')||(PM[i] =='B')){
				cout << "Your code is " << MP<< " .Retire at 60."<<endl;}
			else{
				cout << "Your code is " << MP<< " .Retire at 55."<<endl;}
	}
	
    double BMI, height, weight; 
    cout << "\n\n*Retirement Age Summary*" << endl;
    cout << "------------------------------------------------"<<endl;
    cout << setw(10) << "Staff" << setw(11) << "BMI" <<  setw(17) <<  "MP Code" <<endl;
    cout << "------------------------------------------------"<<endl;
    for (int i=0; i<j; i++)
		{cout << setw(8) << i+1 << setw(13) << IMB[i] << setw(13) << PM[i] << endl;}
    cout << "------------------------------------------------"<< endl;
    
    cout << "Total number of staff retire at 60 : " << a <<endl;   
	cout << "Total number of staff retire at 55 : " << b <<endl;
	system ("pause");
	return 0;
}
Last edited on
Bump
PM[i]=calcMP(BMI, age, gender);

I'm guessing your problem is probably right there. BMI is unitialized and is never assigned a value. You're going to get unexpected results.

My compiler actually won't compile this because it is unitialized when you use it. But I do see another variable that you're storing the BMIs in...
I think I can see where the issue lies in your code. On line 68 when you call function calcBMI you haven't changed your local variable of BMI in the main function. This causes confusion a few lines later when you use that local variable in the constructor of you calcMP function. I am not a professional but I would guess using a pointer here might mitigate the issues or some clever usage of your local variables.

Don't you store the BMI's that you calculate in that IMB array?

IMB[i]=calcBMI( height, weight);

Isn't that what you should be sending in to calcMP?
Ok so I edited the code a little bit. And yayy the counter for total number of staffs finally works.
BUT another problem arise though. I can't seem to display the code 'A', 'B' or 'C' in the output. OTL
How to initialize the BMI though?
And sadly my lecturer said we're not learning pointer so I don't know how to apply


Edit;; It's already solved. Thanks everyone for helping. :)
Last edited on
Topic archived. No new replies allowed.