C ++ If语句导致CMD不发布输出?

时间:2016-09-29 22:45:14

标签: c++

我用C ++制作了BMI计算,并决定我想添加一个计算用户新BMI的部分,每周给予一次体重变化(比如,每周减掉2磅,或者每周增加2磅, n周)我有两个if语句设置,所以当被问到他们正在寻找什么样的体重变化时,根据这个结果,它会跳转到一个if语句或另一个。我的意思是,如果用户想要 LOSE 重量,他们会输-2,这将跳转到我为减肥设置的if语句。如果用户想要 GAIN 权重,它会跳转到另一个if语句,设置为在给定新的权重变化时计算新的BMI。以下是我的两个if语句设置:

#include "stdafx.h"
#include <iostream> // Allows us to use cout and cin
#include <cmath> // To allow us to calculate the BMI
#include <iomanip> // Set specific decimal points(two)
using namespace std;

int main()
{
double weight;
cout << "What is your current weight?: "; // Using the cout() function, we get user input
cin >> weight;
double heightFeet;
cout << "What is your height(in feet, just the first part(Example, if you're 5'11, put 5)";
cin >> heightFeet;
double heightInches;
cout << "What is your height in inches, just the second part(Example if you're 5'11, put 11)\n";
cin >> heightInches;
double HeightConverter = 12 * heightFeet + heightInches;
cout << "Your height in inches is: " << HeightConverter << "\n";
double BMICalc = (weight * 703) / (pow(HeightConverter, 2));
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << "Your current BMI is: " << BMICalc << "\n";
double GoalWeight;
std::cout << "What is your goal weight change per week? (lb)\n";
cin >> GoalWeight;
if (GoalWeight >= 0) {
    int weeks;
    cout << "How many weeks do you plan to continue this trend?\n";
    cin >> weeks;
    double PosBMI = (weight + GoalWeight) * 703 / (pow(HeightConverter, 2));
    cin >> PosBMI;
    cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << PosBMI;

}
if (GoalWeight < 0) {
    int weeks;
    cout << "How many weeks do you plan to continue this trend?\n";
    cin >> weeks;
    double NegBMI = (weight - GoalWeight) * 703 / (pow(HeightConverter, 2));
    cin >> NegBMI;
    cout << "If you complete your plan for " << weeks << "weeks you will have a new BMI of: \n" << NegBMI;
}
system("pause");
return 0;

}

编译完成后,它会进入&#34;你计划继续这个趋势多少个星期?&#34; ,如果我输入3,它会暂停在3终端,并没有做任何事情,最终我必须关闭它。有谁看到if语句中的问题是什么?对于丑陋的代码也提前抱歉。跟我在一起。

1 个答案:

答案 0 :(得分:1)

PosBMI被声明并初始化然后立即被std :: cin调用覆盖的大问题。 NegBMI中的类似问题。

double PosBMI = (weight + GoalWeight) * 703 / (pow(HeightConverter, 2));
cin >> PosBMI;

然后当然,重量没有宣布......我认为你需要首先编译它。

所有这一切,我想补充说我的个人BMI需要工作。

我想你的代码更可能是(伪编码)

cout << "Enter your weight";
cin >> weight;
cout << "Enter Goal"
cin >> WeightGoal

if (WeightGoal > weight) {
  .....
} else {
  ....
}
cout << "If you complete your plan for " << weeks << .....