我正在编写一个分配代码,要求我创建一个程序,要求用户输入他们想要输入的整数量,然后在测试值是最大值还是最小值时接受每个输入。
程序运行正常,但由于某种原因,当我输入的1个整数小于原始输入时,它会停止并显示最小和最大数字。
我遇到的问题的例子:
此外,
我有两个主要问题:
代码:
#include <iostream>
using namespace std;
int main()
{
int input;
int tmp;
int counter = 1;
int max_num = 0;
int min_num;
// prompt user for integer amount
cout << "How many integers would you like to enter? " << endl;
cin >> input;
cout << "Please enter " << input << " integers." << endl;
tmp = input;
// loop for requested amount with a test for each input
while (counter <= tmp) {
cin >> input;
// if smaller than previous number it is the minimum
if (input < min_num || min_num == -1) {
min_num = input;
counter++;
}
// if larger than previous number it becomes max number else
if (input > max_num) {
max_num = input;
counter++;
} else { // continue loop if number isn't bigger than max or smaller than min
counter++;
}
}
// display the max and min
cout << "min: " << min_num << endl;
cout << "max: " << max_num << endl;
return 0;
}