program help, please?

I need help finishing this program for my c++ class. I need a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information:
1. the number of integers in the series (not including zero)
2. the average of the integers
3. the largest integer in the series
4. the smallest integer in the series
5. the difference between the largest and the smallest integer in the series

This is what i have so far:




#include <iostream.h>

using namespace std;

int main()
{
double num, average, count, largest, smallest, difference, numbers_entered, total;
do
{
cout << "Enter a series of numbers (Enter 0 to quit): ";
cin >> num;
if (num == 0) // break out of loop if
{break; } //number entered is zero.

count = count++;

total = total + num;


if (num > largest)
{ num = largest}

if (num < smallest)
{ num = smallest}

else if (num < smallest)
{ smallest = num}
}
while (1); // Create an infinite loop and allow the
//break statement to end the loop.

average = total / count;

difference = largest - smallest;

cout << num << " total is " << total << endl;

cout << count;

cout << "The average is " << average << endl;

cout << "The smallest number is " << smallest << endl;

cout << "The largest number is " << largest << endl;

cout << "The difference is " << difference << endl;

system ("pause");
return 0;
}



i know i looks like crap now, but any help would be greatly appreciated :)
First, please always use code tags, edit the post, select your code then press the <> button on the right.

This code:
1
2
3
count = count++;

total = total + num;


Is better written like this:

1
2
3
count++;

total += num;


If you are going to use a do loop, put the while condition on the same line as the closing brace:

} while (1);

This helps readers to recognise it as a do loop. I would have put the comments at the start of the loop with the do, rather than at the end. I would have made it a while loop anyway:

1
2
3
while (true) {
// your code
}


Alternatively, a for loop:

1
2
3
for (;;) {
// your code
}


Now for your actual problem, The variables largest & smallest are not initialised before you test them:

1
2
3
4
5
6
7
if (num > largest) { 
       num = largest
}

if (num < smallest) { 
       num = smallest
}


Assign the value of the first input number to both the smallest & largest variables, then start doing the comparisons from the second input value onwards.

Hope all goes well.

Here is a tip on spacing to make it very easy to read:
Notice the spacing makes it easy to pick out which brackets go where.
1
2
3
4
5
6
7
8
9
10
11
int main()
{
     Do //tabbed in 5 spaces
     {
          if(stuff) //tabbed in 5 spaces to indicate it is part of the loop
          {
                do this //tabbed in to indicate it is part of the if statement
          } //tabbed in the same amount as the original to indicate this bracket closes the if statement
     } //tabbed in the same as the other to indicate this ends the do loop
     while(condition);
}
Topic archived. No new replies allowed.