Im trying to create a program which displays the minimum and maximum integers as prompted but the user. The maximum is always correct, but the minimum always comes out as 0. Don't know what Im missing from my code?
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int intsWanted, min, max, input;
do {
cout << "How many integers would you like to
enter?" << endl;
cin >> intsWanted;
} while (intsWanted < 1);
cout << "Please enter " << intsWanted << " integers." << endl;
cin >> min;
min = max;
intsWanted--;
while (intsWanted >= 1) {
cin >> input;
if (input > max) max = input;
if (input < min) min = input;
intsWanted--; }
cout << "min: " << min << endl;
cout << "max: " << max << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire