接受用户输入的5个数字并输出最大和最小

时间:2018-10-14 10:59:08

标签: c++

我是C ++的新手,我希望您能在这段代码中为我提供帮助。

我觉得这应该非常容易,但是我只是不知道如何做到这一点,而且我一直在搜索互联网。

我只需要接受5个数字,然后输出最大和最小即可。我想我需要使用一个循环然后检查数字,但是我不明白该程序背后的逻辑。任何小窍门都是非常有益的。

#include "pch.h"
#include <iostream>

int main()
{
std::cout << "Hello World!\n"; 
int numberCounter;
int number;
int number2;
int number3;
int number4;
int number5;
int largestNumber;
int smallestNumber;
numberCounter = 1;

while (numberCounter <= 5) {
    std::cout << "Please Enter a number";
    std::cin >> number >> number2 >> number3 >> number4 >> number5;
    numberCounter++;
}

if (number < number2) {
    number = smallestNumber;
    number2 = largestNumber;
}
else {
    number2 = largestNumber;
    number = smallestNumber;
}
if (number3 > largestNumber) {
    number3 = largestNumber;
}
else {
    if (number3 < smallestNumber) {
        number3 = smallestNumber;
    }
}
if (number4 > largestNumber) {
    number4 = largestNumber;
}
else {
    if (number4 < smallestNumber) {
        number4 = smallestNumber;
    }
}
if (number5 > largestNumber) {
    number5 = largestNumber;
}
else {
    if (number5 < smallestNumber) {
        number5 = smallestNumber;
    }
}

std::cout << "The smallest number is: " << smallestNumber << "\n";
std::cout << "The largest number is: " << largestNumber<< "\n";
}

我收到这样的错误消息:使用了未初始化的局部变量'largestNumber'。

1 个答案:

答案 0 :(得分:0)

以下是工作示例

callback ("params",20);