Help with errors in if else if statements.

Hi I'm still a beginner at programming and having to do a Biological Age calculator program with if else if statements. I've done the bulk of my work but need help understanding why I'm getting certain errors and some help with how to calculate what I need.

I've added comments about the errors in my code. I wasn't able to post all of my code because it made my post over 9000 characters. But most of my code is using if else if statements and multiple choice questions to add numbers amounts based on each answer to calculate biological age.

Thank you very much and I appreciate all help. :)

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
//Biological Age Calculator

#include <iostream>
#include <string>

using namespace std;

int main()

{

//The main error I'm seeing when I try to build is warning C4244 conversion from double to int. Possible loss of data. This occurs on many lines.
	int ageWork = 0; //This is supposed to be set to zero
	char ch, gender; //One of my errors when I try to build it says uninitialized local variable ch.
	double chronAge, bioAge, answer;
	string name;

ch = toupper(ch); //I need to use this for when the user enter a letter it's converted to a capital letter but I don't know if I'm using it properly.

	cout << "Welcome to Programmer's Biological Age Calculator" << endl;
	cout << "Enter the subject's age: " << endl;
	cin >> chronAge;
	cout << "Enter your gender (M or F): " << endl;
	cin >> gender;
	cout << "Enter your first name: " << endl;
	cin >> name;
	cout << endl;

	cout << "What's your body mass index (BMI)?"
		 << "A. 0 to 17"
		 << "B. 18 to 21"
		 << "C. 22 to 24"
		 << "D. 25 to 30"
		 << "E. >30\n\n";
		 cin >> answer;

		 //This is where the first if else if statement starts and I'm not sure if I'm doing this completely right. I have to add numbers to ageWork in order to get the calculations at the end and get bioAge.
		 if (answer = 'A')
			 ageWork = answer - 7; //Calculations by adding amount to ageWork. I believe I'm doing this wrong but I'm not entirely sure how to approach this.
		 else if (answer = 'B')
			ageWork = answer - 7;
		 else if (answer = 'C')
			 ageWork = answer + 4;
		 else if (answer = 'D')
			 ageWork = answer + 10;
		 else if (answer = 'E')
                 else
		 {
		 cout << "Please enter a letter" << endl; //Trailing else to assure user enters a letter.
		 }

	//Biological age and chronological age calculations
	ageWork = ageWork / 10; //ageWork is set to 0 and every answer the user enters adds a number to ageWork at the end.
	bioAge = ageWork + chronAge;

	cout << "CSDE Biological Age Calculator Report for: " << name << endl;
	cout << endl;

	cout << "Chronological Age: " << chronAge << endl;
	cout << "Biological Age" << bioAge << endl;
	cout << endl;

	cout << "Thank you for using programmer's Biological Age Calculator" << endl;

	system("cls");
	return 0;
}
Okay so I fixed the C4244 errors by removing answer from the (ageWork = ) lines. Whenever I'm doing this none of the numbers in the if else if statements are being added to ageWork. So I know I'm doing it wrong but I don't know how to fix it.
What do you want as body max index?
Do you want a number or a character ('A' or 'B' or 'C'...)?
I would answer with a character to that question, but then you try to apply formulas to that answer...
Thank you for responding :)

I would need the user to enter a character.

I wasn't able to post the entirety of the multiple choice questions in my question here because it went over 9,000 characters and told me I can't post it. So that's just question 1 but I have 12 questions in total.

The multiple choice questions are where the user inputs their answer 'A' 'B' 'C' etc. and based on the user input an amount is added to the variable ageWork which is set to 0 so I can calculate Biological age at the end of the program.

Each answer has a numerical value to it. For example, if the user enters 'A' for question 1 it subtracts 7 from ageWork. While if they enter 'C' it adds 4 to ageWork.

After all the questions are answered ageWork is divided by 10 to get the bioAge.

I'm not really sure how to accomplish this with if else if statements but that's what I have to use. I also may approaching this in the wrong way. :/
I see what you're saying :) right after I replied I was like "Oh!" So I put answer in the char area for initialization.
I hope this code could be of some inspiration:
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
#include <iostream>
#include <string>

void clearscreen();

using std::cin;
using std::cout;
using std::endl;

int main()
{
    clearscreen();
    cout << "Welcome to Programmer's Biological Age Calculator" << endl;
    cout << "Enter the subject's age: ";
    double chronAge{0};
    cin >> chronAge;
    cout << "\nEnter your gender (M or F): ";
    char gender;
    cin >> gender;
    cout << "\nEnter your first name: ";
    std::string name;
    cin >> name;
    cout << endl;

    cout << "What's your body mass index (BMI)? "
            "\nA. 0 to 17"
            "\nB. 18 to 21"
            "\nC. 22 to 24"
            "\nD. 25 to 30"
            "\nE. >30\n\n";
    char answer;
    cin >> answer;

    cout << "\nSo, what's your BMI in number? ";
    double bmi;
    cin >> bmi;

    int ageWork = 0;
    switch(answer) {
    case 'a':
    case 'A':
        ageWork = bmi - 7; // Should it be the same of B?
        break;
    case 'b':
    case 'B':
        ageWork = bmi - 7; // The same that A?
        break;
    case 'c':
    case 'C':
        ageWork = bmi + 4;
        break;
    case 'd':
    case 'D':
        ageWork = bmi + 10;
        break;
    case 'e':
    case 'E':
        // Missing formula for case 'E'
        break;
    default:
        break;
    }

    //Biological age and chronological age calculations
    ageWork = ageWork / 10;
    double bioAge = ageWork + chronAge;

    cout << "CSDE Biological Age Calculator Report for: " << name << endl;
    cout << endl;

    cout << "Chronological Age: " << chronAge << endl;
    cout << "Biological Age: " << bioAge << endl;
    cout << endl;

    cout << "Thank you for using programmer's Biological Age Calculator" << endl;

    return 0;
}

void clearscreen()
{
    std::cout << std::string(50, '\n') << std::endl;
}

Topic archived. No new replies allowed.