/* I am trying to make a program that takes an average of two numbers using multiple functions and out put the results here is what I have so far*/
#include <iostream>
using namespace std;
void getMathScore(int a)
{
int mathScore = 0;
cout << "Enter your math score: ";
cin >> mathScore;
cout << mathScore;
}
void getChemScore(int b)
{
int chemScore = 0;
cout << "\nEnter your chem score: ";
cin >> chemScore;
cout << chemScore;
}
void compAverage(int a, int b, int& r)
{
int mathScore, chemScore, avg;
avg = (a + b) / 2;
cout << "\nYour average is: " << avg;
}
void avgNumRef (int mathScore, int chemScore, int avg)
{
cout << "\nYour math score is: " << mathScore;
cout << "\nYour chem score is: " << chemScore;
cout << "\nYour average score is: " << avg;
}
int main()
{
int mathScore, chemScore, avg;
getMathScore(mathScore);
getChemScore(chemScore);
compAverage(mathScore, chemScore, avg);
avgNumRef(mathScore, chemScore, avg);
return 0;
}
To thing to understand here is that, for example, the 'mathScore' variable in your main function is different than the 'mathScore' variable in your getMathScore function.
If you want the mathScore variable in main to be updated when you call getMathScore, you need to pass mathScore by reference instead of copying it.
e.g.
1 2 3 4 5 6
void getMathScore(int& mathScore)
{
cout << "Enter your math score: ";
cin >> mathScore;
cout << mathScore;
}
Notice the type of the input parameter is int&, which means "reference to int".