we did this program in the classroom but i dont type fast and fell behind, im doing homework for it now but before i go on i need to figure why this one wont work. on the return function on the last line it says "return value type does not match function" any help would be awesome thanks!
#include <iostream>
using namespace std;
void CalcValue(double, double&, double&, double);
int main ()
{
double num, sum, avg, count;
sum=0.0;
avg=0.0;
count=0.0;
cout<<"Enter a Number: "<<endl;
cin>>num;
while (num>0);
{
count=count+1;
CalcValue(num, sum, avg, count);
cin>>num;
}
cout<<"Sum: "<<endl;
cout<<"Average: "<<endl;
return 0;
system ("pause");
Remove the line return total, avg;; it's not needed here. These values are already being returned as they're being passed by reference (i.e. double&) rather than by value (double).
CalcValue is declared as returning nothing (that is, void). So you can't return anything.
1 2 3 4 5 6
void CalcValue(double num1, double & total, double & avg, double count)
{
total=total+num1;
avg=total/count;
return; // a bit pointless, so usually omitted
}
And while you can only return one value from a function, the following -- with the return changed to double -- will compile, but is a bit pointless/confusing
1 2 3 4 5 6
double CalcValue(double num1, double & total, double & avg, double count)
{
total=total+num1;
avg=total/count;
return total, avg; // only avg is returned
}
The comma operator evaluates from left to right, so in this case avg will be returned. So it's much easier to understand if you just write return avg;