| CAP (2) | |
|
I've never posted in a forum before. I wish I could highlight my "else" errors I'm getting. Does anyone see anything wrong with this code pertaining to if/else statements? It's maddening. I'm getting four errors on my "else's". It's the four that are in the two separate functions (each one starts with "else if", then has another else after it). It's for class, and it's a program to calculate the calories a male/female needs to maintain their current weight (with an activity level and weight). Please and thank you //IV. Code the algorithm into a Program #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { //declare variables and named constants char gender = ' '; char level = ' '; double weight = 0.0; double dcn = 0.0; //enter input items cout << "Enter gender to determine how many calories are needed to maintain your current weight (M or F): "; cin >> gender; gender = toupper(gender); // selection if (gender == 'F') { cout << "Enter activity level (A or I): "; cin >> level; level = toupper(level); // nested selection if (level == 'I') cout << "Enter weight: "; cin >> weight; dcn = (weight*10); cout << dcn << endl; else if (level == 'A') cout << "Enter weight: "; cin >> weight; dcn = (weight*12); cout << dcn << endl; else cout << "Invalid selection: "; } else if (gender == 'M') { cout << "Enter activity level (A or I): "; cin >> level; level = toupper(level); // nested selection if (level == 'I') cout << "Enter weight: "; cin >> weight; dcn = (weight*13); cout << dcn << endl; else if (level == 'A') cout << "Enter weight: "; cin >> weight; dcn = (weight*15); cout << dcn << endl; else cout << "Invalid selection: "; } else cout << "Invalid selection: "; //system("pause"); // use if needed return 0; } //end of main function | |
|
|
|
| arzhon (5) | |||
|
yes there seems to be two "else" statement. try to use brackets "{}" to execute more that one line in "if" statement, that will help you find the problem. for example from your code,
when "level" is not equal to 'I', it wont run the cout << "Enter weight: " but still gonna run the cin >> weight; dcn = ...... statement. | |||
|
|
|||
| CAP (2) | |
| Ok, thanks. I'll play around some more with it. Thanks for opening my eyes (again!) to thinking like a computer. I can't assume anything. | |
|
|
|