Need help with my sumnumbers program!

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

int sumNumbers(int num, int &sum, bool send);
void dispavg(double average);

int main()
{
int num, sum = 0, cnt = 0;
cout << fixed << showpoint << setprecision(1);
while (!cin.eof())
{
cout << "Enter an interger, (ctrl+z to display average): ";
cin >> num;
sumNumbers(num, sum, false);
}
dispavg((double)sum / sumNumbers(num, sum, true));

system("pause");
return 0;
}

int sumNumbers(int num, int &sum, bool send)
{
static int count = 0;
if (send)
return count;
else
{
sum += num;
count++;
}
cout <<"The sum is " << sum << endl;
return 0;
}
void dispavg(double average)
{
cout << "The Average is " << average << endl;

}

The problem I'm having is The sum and average are incorrect in my program and i can't figure out how to fix it. The rest of my program is fine.
I must follow these guidelines:
Add a program that reads integer values from the keyboard until the user enters cntl_z (eof). The integers are to be summed to a total and the program displays the sum and average of the numbers.
Code a function with the following prototype:
int sumNumbers(int num, int &sum, bool send);
To sum an input number, invoke the function with the value in the num parameter, the sum reference variable, and false for the send parameter. To get the number count, invoke the function with zero for the num parameter, the sum reference variable, and true for the send parameter. Upon return, the summed value will be in the sum variable and the count will be returned. Display the sum and average of the numbers with 1 decimal place.
while (!cin.eof()) Do not loop on eof(). It reads one time more than needed:
Enter an interger, (ctrl+z to display average): 1
The sum is 1
Enter an interger, (ctrl+z to display average): 2
The sum is 3
Enter an interger, (ctrl+z to display average): ^Z
The sum is 5
The Average is 1.7
Note that it read another value even after I hit Ctrl + Z.

loop on input operations:
1
2
3
4
5
std::cout << "Enter an interger, (ctrl+z to display average): ";
while (std::cin >> num) {
    sumNumbers(num, sum, false);
    std::cout << "Enter an interger, (ctrl+z to display average): ";
}
Or check failure after read:
1
2
3
4
5
6
7
while (std::cin){ //Not actually needed, can be true instead
    std::cout << "Enter an interger, (ctrl+z to display average): ";
    std::cin >> num;
    if(std::cin.fail())
        break; //Stop loop as we fail to read next integer
    sumNumbers(num, sum, false);
}

The problem is that you're calling sumNumbers() one too many times. When the user enters ctrl-Z, the program executes cin >> num, which fails, then it calls sumNumbers(), passing in the last value of num. As a result, if you enter 1 2 3, it calls sumNumbers() with 3 twice.

To fix this change the loop to:
1
2
3
4
5
    while (true) {
	cout << "Enter an interger, (ctrl+z to display average): ";
	if (!(cin >> num)) break;
	sumNumbers(num, sum, false);
    }


For what it's worth, the design specified in the assignment is really bad. sumNumbers() does two completely different things depending on where it's called from. My friends and I call this a "computed come-from," which is a pun on a "computed go-to."
thanks guys i appreciate it, I was going nuts trying to figure it out.
Topic archived. No new replies allowed.