Hi..can anyone help me..debugging my codes..
when i compile and run it.. i dont get the accurate result..of sum and avg..can anyone help me telling where is the error??
thanks so much....

Here are the codes:

#include <iostream>

void getNum(int& num,int& sum);
void doProcess(int num, int& sum, float& avg);
void printNum(int sum, float avg);

using namespace std;
int main ()
{
int num,sum;
float avg;
getNum(num,sum);
doProcess(num,sum,avg);
printNum(sum,avg);

}

void getNum(int& num,int& sum)
{
cout<<"Please enter 5 numbers:"<<endl;
for(int i=0, sum=0; i<=4; i++)
{
cin>>num;
sum=sum+num;
}
return;
}

void doProcess(int num, int& sum, float& avg)
{
avg=sum/5;
return;
}

void printNum(int sum, float avg)
{
cout<<"The total is:"<<sum<<endl;
cout<<"The average is:"<<avg<<endl;
return;
}

>Hint, initialize your variables.
It's not that simple. An untrained eye could easily say that the variables are initialized prior to use.

>for(int i=0, sum=0; i<=4; i++)
You're actually defining a new sum variable here, not initializing the parameter named sum. The new variable hides the old one, and any changes you make in the loop modify the local variable rather than the reference parameter. You need something more like this:

void getNum(int& num,int& sum)
{
  cout<<"Please enter 5 numbers:"<<endl;
  sum = 0;
  for(int i=0; i<=4; i++)
  {
    cin>>num;
    sum=sum+num;
  }
  return;
}

Edited 6 Years Ago by Narue: n/a

do you talk about the variable sum?
i have initialize it it the for loop..
it is wrong?

thanks so much my dear Narue..
i have solved this problem..
once again thanks.......

This question has already been answered. Start a new discussion instead.