C++:使用错误 C4700 未初始化的局部变量""

C++: Error C4700 uninitialized local variable " " used

本文关键字:初始化 局部变量 C4700 错误 C++      更新时间:2023-10-16

我正在尝试创建一个程序,该程序接受 3 个数字并按升序排列。

我已经写出了代码,但不断收到错误说:

"错误 C4700 使用未初始化的局部变量"num2"。

"错误 C4700 未初始化的局部变量 "num3" 使用。

我之前做过 If/if-else/else 嵌套,但我从未遇到过此错误。我是C++新手,仍在学习。

我尝试将变量设置为默认值为 0,然后要求输入一个数字,以便将其覆盖,然后我可以将它们相互比较。当我不将它们设置为 0 时,我也会收到一个随机运行时错误;

这是代码:

#include <iostream>
using namespace std;
int main()
{
int num1; //I get an error message when I do not set it to 0.
int num2;
int num3; 
cout << "Please enter three numbers: " << endl;
cin >> num1; num2; num3; 
if (num1 > num2 && num2 > num3){
    cout << "In ascending order: " << num1 << num2 << num3 << endl;
}
else if (num2 > num1 && num1 > num3)
{
    cout << "In ascending order: " << num2 << num1 << num3 << endl;
}
else if (num3 > num1 && num1 > num2){
    cout << "In ascending order: " << num3 << num1 << num2 << endl;
}
else
{
    cout << "There was an invalid input." << endl; 
}
system("pause");
return 0;
}

这是我遇到的错误。

cin >> num1; num2; num3; 

这可能应该是

cin >> num1 >> num2>> num3; 

其他一些评论:

1)避免"使用命名空间标准;"

2)尝试找到一个更高效C++开发环境,不会强迫您浪费时间解决诸如系统之类的愚蠢问题("暂停")