以下代码旨在要求用户指定他们想要输入的整数数,然后查找并显示它们之间的最大值和最小值。但是,它没有编译,可能是因为我初始化minInt和maxInt变量的方式,但我不确定。请帮忙!提前谢谢!
#include <iostream>
using namespace std;
int main()
{
int inNum, inInt, minInt, maxInt, i;
minInt = INT_MAX;
maxInt = INT_MIN;
cout << "How many numbers would you like to enter?" << endl;
cin >> inNum;
cout << "Please enter " << inNum << " integers." << endl;
for(i = 0 ; i < inNum; i++)
{
cin >> inInt >> endl;
if(inInt > maxInt)
{
maxInt = inInt;
}
if(inInt < minInt)
{
minInt = inInt;
}
}
cout << "min: " << minInt << endl;
cout << "max: " << maxInt << endl;
return 0;
}
答案 0 :(得分:3)
这是你的问题:
cin >> inInt >> endl;
您不需要在此endl
。
cin >> inInt;
答案 1 :(得分:0)
我发现了2个错误:
<climits>
,其中INT_MAX
和INT_MIN
已定义cin
不支持endl
所以代码应该是如此:
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int inNum, inInt, minInt, maxInt, i;
minInt = INT_MAX;
maxInt = INT_MIN;
cout << "How many numbers would you like to enter?" << endl;
cin >> inNum;
cout << "Please enter " << inNum << " integers." << endl;
for(i = 0 ; i < inNum; i++)
{
//Error 3: cin >> inInt >> endl; cin does not support endl
cin >> inInt;
if(inInt > maxInt)
{
maxInt = inInt;
}
if(inInt < minInt)
{
minInt = inInt;
}
}
cout << "min: " << minInt << endl;
cout << "max: " << maxInt << endl;
return 0;
}