无论用户输入什么,C ++ cin都返回0表示整数

时间:2012-12-17 10:47:28

标签: c++

无论是cin还是继续输出0得分。这是为什么?我试着返回“return 0;”但仍然没有去:/

#include "stdafx.h"
#include <iostream>

using namespace std;

// Variables
int enemiesKilled;
const int KILLS = 150;
int score = enemiesKilled * KILLS; 

int main()
{
    cout << "How many enemies did you kill?" << endl;
    cin >> enemiesKilled;
    cout << "Your score: " << score << endl;
    return 0;
}

4 个答案:

答案 0 :(得分:4)

您需要在用户输入输入后执行乘法运算:

int main()
{
    cout << "How many enemies did you kill?" << endl;
    cin >> enemiesKilled;
    int score = enemiesKilled * KILLS;
    cout << "Your score: " << score << endl;
    return 0;
}

在线查看:ideone

答案 1 :(得分:1)

您需要在用户输入号码后重新计算score

答案 2 :(得分:1)

cin >> enemiesKilled;
score = enemiesKilled * KILLS; // <-- Put the calculation here!!
cout << "Your score: " << score << endl;

答案 3 :(得分:1)

程序启动时,

int enemiesKilled;初始化为0.

int score = enemiesKilled * KILLS;同时计算。由于enemiesKilled为0,因此它也为0.

如其他答案中所述,您需要在运行时计算score