Averaging Function

New to programming here. I'm trying to write an averaging program that takes in three integers and returns a double (their average). When I try to run the program it takes in the inputs but always returns QNaN. Can you tell me what I'm doing wrong here?


#include <cstdlib>
#include <iostream>

using namespace std;

double average3(int a, int b, int c)
{double r;
r = (a+b+c)/3;}

int main()
{ int a;
int b;
int c;
double z;
z = average3(a, b, c);
cout << "Please enter first integer" << endl;
cin >> a ;
cout << "Please enter second integer" << endl;
cin >> b ;
cout << "Please enter final integer" << endl;
cin >> c;


cout << "The average of the three integers is " << z;

system("PAUSE");
return 0;
}
You are calculating the average before you input the values.
Alright so is this right? I know it's not because it doesn't work, but I'm wondering why.


#include <cstdlib>
#include <iostream>

using namespace std;

double average3(int a, int b, int c)
{double r;
r = (a+b+c)/3;}

int main()
{ int a;
int b;
int c;

cout << "Please enter first integer" << endl;
cin >> a ;
cout << "Please enter second integer" << endl;
cin >> b ;
cout << "Please enter final integer" << endl;
cin >> c;

double z;
z = average3(a, b, c);

cout << "The average of the three integers is " << z;

system("PAUSE");
return 0;
}
add return r; after you find r in your average3 function.
Last edited on
That worked. Thanks so much.
Topic archived. No new replies allowed.