You can assume they will enter a number >= 1. The program will then prompt the user to enter that many integers.
After all the numbers have been entered, the program should display the largest and smallest of those numbers.
I am having a couple problems.
For example, if I input that I want to enter 4 integers I can only input 3 numbers before it calculates the min and max.
Additionally, if I input that I want to enter 4 integers, and one of those integers is negative (-1 for instance), the output is shortened and is sometimes incorrect.
Any insight is much appreciated.
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using namespace std;
- int main()
- {
- int input;
- int tmp;
- int counter = 1;
- int max_num = -1;
- int min_num = -1;
- //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++;
- }
- //continue loop if number isn't bigger than max or smaller than min
- else {
- counter++;
- }
- }
- //display the max and min
- cout << "min: " << min_num << endl;
- cout << "max: " << max_num << endl;
- return 0;
- }