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;
}

Recommended Answers

All 4 Replies

Hint, initialize your variables.

>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;
}

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.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.