# c++ - C++帮助 - 查找用户输入中的最小值

``````
#include <iostream>

using namespace std;

int main()

{

double number, numberitems, sum = 0, average, max, min;

cout <<"Enter number of items: n";

cin >> numberitems;

//Make sure user can not enter negatives

if ( numberitems < 0 ) {

//no request to perform sum

std::cout <<"I said not to enter a negative number... 'n";

return 1;

}

//Get the user's values

for (int i = 0; i < numberitems; i++)

{

std::cout <<"Enter any NON-negative number:";

std::cin >> number;

std::cout << 'n';

//Maximum value entered

if (number > max) {

max = number;

}

//minimum value entered

if (number < min) {

min = number;

}

//Make sure user can not enter negatives

if ( number < 0 ) {

//no request to perform sum

std::cout <<"I said not to enter a negative number... 'n";

return 1;

}

//Sum of all the numbers

sum = sum + number;

//Average of all the numbers

average = sum / numberitems;

}

std::cout << endl;

std::cout << endl;

std::cout <<"Sum of numbers entered is:" << sum << 'n';

std::cout <<"Average of numbers entered is:" << average <<'n';

std::cout <<"The Highest number entered was:" << max <<'n';

std::cout <<"The Lowest number entered was:" << min <<'n';

return 0;

}

``````

``````
#include <iostream>

using namespace std;

int main()

{

int numberitems;

double number, sum = 0, average;

double max = 0;

double min = 99999999999999999999999999999999999999999999999;

cout <<"Enter number of items: n";

cin >> numberitems;

//Make sure user can not enter negatives

if ( numberitems < 0 ) {

//no request to perform sum

std::cout <<"I said not to enter a negative number... 'n";

return 1;

}

//Get the user's values

for (int i = 0; i < numberitems; i++)

{

std::cout <<"Enter any NON-negative number:";

std::cin >> number;

std::cout << 'n';

//Maximum value entered

if (number >= max) {

// debug - (remove before submission)

std::cout <<"number is the new max! number =" << number

<<", max =" << max << std::endl;

// end of debug

max = number;

}

//minimum value entered

if (number <= min) {

// (debug - remove before submission)

std::cout <<"number is the new min! number =" << number

<<", min =" << min << std::endl;

// end of debug

min = number;

}

//Make sure user can not enter negatives

if ( number < 0 ) {

//no request to perform sum

std::cout <<"I said not to enter a negative number... 'n";

return 1;

}

//Sum of all the numbers

sum = sum + number;

//Average of all the numbers

average = sum / numberitems;

}

std::cout << endl;

std::cout << endl;

std::cout <<"Sum of numbers entered is:" << sum << 'n';

std::cout <<"Average of numbers entered is:" << average <<'n';

std::cout <<"The Highest number entered was:" << max <<'n';

std::cout <<"The Lowest number entered was:" << min <<'n';

return 0;

}

``````

``````
double max, min; // BUG - not initialized

double max = 0; // CORRECT - initialized

double min = DBL_MAX; // CORRECT - initialized

``````

min和max未初始化。

## 然而

``````
//Get the user's values

for (int i = 0; i < numberitems; i++)

{

std::cout <<"Enter any NON-negative number:";

std::cin >> number;

std::cout << 'n';

//Maximum value entered

if (number > max) {

// debug - (remove before submission)

std::cout <<"number is the new max! number =" << number

<<", max =" << max << std::endl;

// end of debug

max = number;

}

//minimum value entered

if (number < min) {

// (debug - remove before submission)

std::cout <<"number is the new min! number =" << number

<<", min =" << min << std::endl;

// end of debug

min = number;

}

``````