Preparing for test

And one more practice I am having issues with. Problem - allow users to enter any quantity of numbers until a negative is entered. Then display the highest and lowest of the numbers entered. When I run it my output is backwards min = max number max number = min. Thanks for any assistance with this.

#include <iostream>
#include <math.h>

using namespace std;
int main()

{
//Declaration
int min = 0;
int max = 0;
int num;
int count = 0;
//int sum = 0;
cout << "Enter your number or a negative number will exit: ";
cin >> num;
while (num >=0){
//sum += num;
count ++;
if (num > min)
min = num;
else if (num > max)
max = num;
cout << "Enter a number: ";
cin >> num;
}
// if (count == 0)
//cout << "No entry" << endl;
//else
{
cout << "Minimum Number is: " << min <<endl;
cout << "Maximum Number is: " << max <<endl;
}
return 0;
}
You have the lineif (num > min) this is making a new minimum only if the number is bigger than the current minimum not smaller. Try changing that toif(num < min)

Also why are some lines that seem useful and needed such as else commented out? Make sure to check that as your program won't run as they are.
Last edited on
Hi,

==>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
    int lowest = 10000000;
    int highest = 0;
    int num = 0;
    int count = 0;
    int sum = 0;

    cout << "Enter your number, or a negative number to exit: ";
    cin >> num;
    while (num >=0)
    {
        sum += num;
        count++;
        if (num < lowest)
lowest = num;
        if (num > highest)
highest = num;
        cout << "Enter a number: ";
        cin >> num;
    }

    cout << "Numbers counted : " << count << endl;
    if(count > 0)
    {
        cout << "Minimum Number is: " << lowest << endl;
        cout << "Maximum Number is: " << highest << endl;
        cout << "Total sum of numbers is: " << sum << endl;
    }
    return 0;
}
(Does that help you?) :)

Edit : It seems that I just have a troublesome stalker.
Topic archived. No new replies allowed.