Hi all,
I'm a rank beginner to C++ and programming in general. I'm in week 6 of my first course, and we have an assignment I'm having a little trouble with. If it matters, we're using standard (?) C++ in our class, not ANSI/ISO.
Anyway....
The problem is to do the following:
1. Ask for input of up to 10 numbers
2. Return the number of entries, the total, the average, the minimum and maximum values
3. Allow user to enter a sentinel of 9999 to end the program.
The part I'm having trouble with is the minimum/maximum. Here's part of the code I have so far:
If I put the min = num; max = num; part inside the while loop, min and max are reset every time the loop repeats. If I put it outside the loop, I keep getting a return of 0 for min.
Can someone please nudge me in the right direction? Thank you so much!
I'm a rank beginner to C++ and programming in general. I'm in week 6 of my first course, and we have an assignment I'm having a little trouble with. If it matters, we're using standard (?) C++ in our class, not ANSI/ISO.
Anyway....
The problem is to do the following:
1. Ask for input of up to 10 numbers
2. Return the number of entries, the total, the average, the minimum and maximum values
3. Allow user to enter a sentinel of 9999 to end the program.
The part I'm having trouble with is the minimum/maximum. Here's part of the code I have so far:
Code:
double num, total = 0, avg, min, max;
int count = 0;
min = num;
max = num;
while ( count < 10 )
{
cout << "Enter value #" << count + 1 << ": ";
cin >> num;
if ( num != SENTINEL ) // Check for the SENTINEL value 9999
{
total += num; // total = total + num
count++; // Raise count by 1
}
else
break; // End program if SENTINEL is entered.
// Evaluate min and max.
if ( min > num )
min = num; // Change min only if num is lower
else if ( max < num )
max = num; // Change max only if num is higher
}
Can someone please nudge me in the right direction? Thank you so much!
Comment