Write the function void readTest(double & exam, double & tavge) the does the following:
a) print the prompt "enter exam score:" than read the exam score
b) print the prompt "enter all test scores:" then read 7 test scores in a loop, and then computer their average
c)the exam score and the average of the test score are returned to the calling function using the reference parameters exam and tavge respectively.
#include <iostream>
#define MAXCOUNT 7
usingnamespace std;
int studID; // the students ID number
int MaxCount; // the number of exam scores up to 7
double avgScore; // the average number of exam scores
int main ()
{
cout << "Enter Student ID:";
cin >> studID;
/****************definition of the function readTestScores()***************/
/************************read scores and compute their average*************/
void readTestScores ();
{
double score, // a student's test score
totalScore; // a student's total test score
int count; // to hold a student's test score
totalScore=0;
count =0;
cout << "enter exam score:";
cin >> score;
/***********************read scores and compute their total****************/
while (count < MaxCount)
{
cin >> score;
totalScore = totalScore + score;
count = count +1;
}
cout << "Enter all test scores:";
cin >> totalScore;
/*******************compute the average score******************************/
avgScore = totalScore/ MaxCount;
double tAvgScore; // the average of the tests
cout << endl << "Test Average is:";
MaxCount = 7;
readTestScores();
tAvgScore = avgScore;
}
system ("PAUSE");
return (0);
}
You have a fundamental misunderstanding of functions. There is no such thing as a "void function". There is, however, such a thing as a function which returns nothing.
As for your problem, you're trying to define the function inside of main - you can't define a function inside another function like that. Separate them, break it up.
What your tutor is getting at is a function that returns nothing but yet the results of the function are assigned to the variables in the argument list that are passed by reference, hence the calling function can receive the results back.
BTW - you can't declare a function inside another function. See the function:
void readTestScores ();
inside your
int main()
If I were to defclare a function foo that take an int as its only argument and returns nothing I would declare it as:
void foo(int a);
The function source is then written:
1 2 3 4
void foo(int a)
{
// some code
}
But if I wanted a function to return a value:
1 2 3 4 5 6
int foo()
{
int val(0);
// some code, where 'val' is potentially updated...
return val; // return result
}
When I want to use the code, say in my main function:
1 2 3 4 5 6 7
int main()
{
int n = foo();
// code...
return 0; // return ok signal back to OS
}
void readTest(double& exam, double& tavge);
int main()
{
double exam, tavge;
// do parts (a) and (b) of question here
// part (c):
readTest(exam, tavge);
return 0;
}
void readTest(double& exam, double& tavge)
{
// calculate average scores, and return result in arg
}
isnt a void function when you do:
void computeAreaPeri (void)
{
area= len * width;
peri= 2*(len + width);
}
and than you use void computeAreaPeri() when ever you want to use those problems?
No, that is a function that does not return anything. The term "void function" is wrong because it encourages an incorrect way of thinking about functions.