Ok, when I run my program it is asking me twice what I only need to be asked once.

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    //Variables
    double calories = 0.0;
    double weight = 0.0;
    char gender = ' ';
    char level = ' ';
    
    
    //enter gender
    cout << "Enter Gender: ";
    cin >> gender;
    gender = toupper (gender);

    
    //calculate
    if (gender == 'F');{
    
               cout << "Enter Your Activity Level (A/I): ";
               cin >> level;
               level = toupper(level);
               
	}
               
               if      (level == 'A');
                       cout << "Enter Current Weight: ";
                       cin >> weight;
                       calories = (weight*12);
                       cout << "You Need This Many Calories Daily To Maintain Your Weight: " << calories << endl;
                       
               
               
               else    (level == 'I');
                       cout << "Enter Current Weight:  ";
                       cin >> weight;
                       calories = (weight*10);
                       cout << "You Need This Many Calories Daily To Maintain Your Weight: " << calories << endl;
                       
   
    
    

    else (gender == 'M');  {         
   
               cout << "Enter Your Activity Level (A/I): ";
               cin >> level;
               level = toupper(level);
	}
               
               if       (level == 'A');{
                        cout << "Enter Current Weight: ";
                        cin >> weight;
                        calories = (weight*15);
                        cout << "You Need This Many Calories Daily To Maintain Your Weight: " << calories <<endl;
                        }
               
               
               else     (level == 'I');{
                        cout << "Enter Current Weight:  ";
                        cin >> weight;
                        calories = (weight*13);
                        cout << "You Need This Many Calories Daily To Maintain Your Weight:  " << calories <<endl;
                        }
     

               
// display output
               
system ("pause");
return 0;

} //end main function

Would someone please guide me in the right direction. Thank you in advance. :)

Dani AI

Generated

The root cause is the stray semicolons and misplaced blocks: if (cond); ends the if with an empty statement, so the following braced block runs unconditionally. That explains why prompts repeat — both "branches" execute regardless of gender. spotted the semicolons; the C++ if syntax and the notion of an empty (null) statement explain why this happens (if statement reference).

Also note the incorrect use of else (condition) — that is not valid C++. Use else if (condition) or else { ... }. Always put the intended statements inside braces for clarity and to avoid accidental scoping bugs. For character case conversion, pass an unsigned char to std::toupper to avoid undefined behaviour on platforms where char is signed (std::toupper reference).

A concise, corrected flow (keeps each prompt once, validates input, no stray semicolons):

#include <iostream>
#include <cctype>

int main() {
    char gender;
    std::cout << "Enter Gender (M/F): ";
    if (!(std::cin >> gender)) return 1;
    gender = static_cast<char>(std::toupper(static_cast<unsigned char>(gender)));

    char level;
    std::cout << "Enter Activity Level (A/I): ";
    if (!(std::cin >> level)) return 1;
    level = static_cast<char>(std::toupper(static_cast<unsigned char>(level)));

    double weight = 0;
    std::cout << "Enter Current Weight: ";
    if (!(std::cin >> weight)) return 1;

    double calories = 0;
    if (gender == 'F') {
        calories = (level == 'A') ? weight * 12 : weight * 10;
    } else {
        calories = (level == 'A') ? weight * 15 : weight * 13;
    }

    std::cout << "Calories needed: " << calories << '\n';
    return 0;
}

Regarding 's question about keys pressed and input: formatted extraction (operator>>) skips leading whitespace and reads the next token, so a typed character plus Enter will behave as expected; problems usually appear when mixing operator>> and std::getline (leftover newline), or when assuming if actually guarded a block when a stray semicolon made it a no-op (operator>> behaviour). Final tips: remove system("pause") for portable code, validate inputs, and keep if / else if / else blocks clean and braced.

Recommended Answers

All 2 Replies

I can't tell you why you have to give twice a answer, but you can correct your code.
After the conditions you write a semicolon what makes the condition end here.
Your braces are wrong placed.
I suggest a gender should contain the case a and i, but they are out of the scope of a gender-condition.
If you are new to C++ go read more about scopes and conditions.

When you get to this section of the program that executes:

//enter gender
    cout << "Enter Gender: ";
    cin >> gender;
    gender = toupper (gender);

how many and what keys are pressed?
Are all the keys read by the program? Why or why not?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.