C++ cin 无论用户输入什么,都返回 0 作为整数
C++ cin returns 0 for integer no matter what the user inputs
无论 cin 如何,它都会继续输出 0 以获得分数。为什么?我尝试返回"返回 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
您需要在用户输入数字后重新计算score
。
cin >> enemiesKilled;
score = enemiesKilled * KILLS; // <-- Put the calculation here!!
cout << "Your score: " << score << endl;
程序启动时int enemiesKilled;
初始化为 0。
同时计算int score = enemiesKilled * KILLS;
。 由于enemiesKilled
为 0,因此它也为 0。
如其他答案中所述,您需要在运行时计算score
。
相关文章:
- 让bool方法返回其他整数
- C 字符串返回字符串的整数/双精度/长整型值
- 如何使此递归函数从给定的起始位置返回最小的整数?
- 如何优化代码以返回最接近给定整数的数字,但给定列表中不存在?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- C++:为什么我的掷骰子函数在掷骰子数量时只返回偶数整数?
- 从 C++ 中定义的异常返回整数
- 递归返回可被给定整数 k 整除的位数
- 类型转换问题:返回为整数而不是浮点/类型
- 分段故障在类之间返回整数
- 如何将字符串传递给函数并返回整数
- atof 仅返回整数
- AVX/SSE 回合向下浮动并返回整数向量
- 元函数计算 x^n 并返回整数限制而不溢出(如果不可能)
- 我已经声明了整数并尝试返回整数,但它给了我错误
- C++ 堆栈字符串流函数返回整数与 ASCII
- 是否可以从模板函数返回整数
- 为什么 floor 不返回整数?
- 如何同时在本机 dll 中返回整数和字符 * 变量C++?
- 为什么difftime只返回整数