如何找到用户输入的最小值?

时间:2017-06-16 06:03:52

标签: c++

大多数代码似乎都可以,需要帮助找到最小的部分。我必须写什么条件才能找到用户输入的最小数字,并将零作为最小数字排除?

#include<iostream>//header
#include <string>
using namespace std;
int main() {
int n , sentinel = 0, max = 0, min, count = 0, sum = 0;
double avg;
cout << "Enter a series of number terminated by 0:" << endl;//prompt user to input a series of number

do {
    cin >> n;//read the input
    for (int i = 0; i < n; i++) {
        if (n>max)
            max = n;
        }
    for (int i = 0; i < n; i++) {
        if (n<min)
            min = n;
    }
    sum = sum + n;
    count++;



} while (n!= sentinel);
count--;
avg = sum / count;

cout << "You have enter " << count << " integers" << endl;//display how may input user enter
cout << "Average is " << avg << endl;//display average
cout << "Max is " << max << endl;//display maximum number
cout << "Min is " << min << endl;//display minimum number
    system("pause");
    return 0;

}

1 个答案:

答案 0 :(得分:0)

您可以从int min;的最大可能值和int max;的最小值开始:

int min = std::numeric_limits<int>::max();
int max = std::numeric_limits<int>::min();

然后min输入的必须小于其初始值,而为max输入的内容必须大于其初始值。