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

标签 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;
}

最佳答案

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

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

在线查看:ideone

关于无论用户输入什么,C++ cin 都会为整数返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13912628/

相关文章:

c++ - 合并 2 个链表并附加到链表的末尾 C++

c++ - 用于 C/C++ 开发的 Mac 与 Ubuntu?

c++ - 显式调用 DLL

c++ - ESP 的值未在函数调用错误中正确保存

c++ - LowLevelKeyboardProc 和 KeyboardProc

c++ - 在哪里可以找到有关 C++11、std 新功能和 Tr1 的可靠信息?

c++ - char 赋值中的不兼容类型?

c++ - (C++) 调用函数时变量值发生变化

c++ - 我如何改进这个 Pollard 的 rho 算法来处理半大素数的乘积?

c++ - 在开源应用程序中检索数据