整数在 VSCommunity2019 中未初始化,windows10

Integer is uninitialized in VSCommunity2019,windows10

我正在用 C++ 制作一个计算器,除了阶乘有一个问题,我已经完成了所有工作: 我的 int 编号是“未初始化的”,如果有帮助的话,这是 windows10 上 vscommunity2019 上的控制台应用程序。除了阶乘外,一切都很完美,当我测试它时,唯一的错误是整数。

#include <iostream>
using namespace std;
int main()

{
float n1;
char op;
float n2;
int co;
int i;
int fact = 1;
int number;
calculations: std::cout << "Enter n1!";
    std::cin >> n1;
    std::cout << "\nEnter operator! Here are your choices: + - * / !";
    std::cin >> op;
    std::cout << "\nEnter n2!";
    std::cin >> n2;
    switch (op)
    {
    case '+':
        std::cout << n1 + n2;
        break;
case '-':
    std::cout << n1 - n2;
    break;

case '*':
    std::cout << n1 * n2;
    break;

case '/':
    std::cout << n1 / n2;
    break;
case '!':
    for (i = 1; i <= number; i++) {
        fact = fact * i;
    }
    std::cout << number;
    break;
default:
    std::cout << "Error! operator is not correct";
    break;
}
std::cout << "\nWant to continue?Y(type 1)/N(type 0)";
std::cin >> co;
if (co == 1) {

    goto calculations;
}
return 0;


}

嗯,number 未初始化。

int main()
{
int number; //uninitialized local variable
[...] //no usage of number
case '!':
    for (i = 1; i <= number; i++) { //first usage of number, before initializing it!
        fact = fact * i;
    }
    std::cout << number;
    break;
[...]
}

幸运的是你得到了一个编译错误并且可以修复它(感谢 MSVC!)。对于 gcc(在编译资源管理器中尝试时,它会编译,但我认为会有运行时错误)。

解决方案?大概在循环中将number替换为n1,输出fact.