trouble with functions and pass by reference

My computer science class has this very specific homework assignment and I don't understand how to do all of it.

Write a program that will calculate the average score and letter
grade earned by a student in any course. The program should allow the user to enter the student’s name. It should
also prompt for the number of exams taken by the student and the score earned for each exam. Calculate the
average and letter grade earned. Display the student’s name, average, and letter grade. (You must use functions in
your source code.)
a. Use main( ) as the driver function. Allow the user to run the program as many times as desired.
b. Write the function getStudentName() that prompts the user for the name of the student and returns it back to
main().
c. Write the function getNumberExams( ) that prompts the user for the total number of exams taken by the student
in the course and returns this value back to main( ). Do not allow the user to enter a value less than one for the
number of exams. Display an error message and keep prompting until a valid value is entered.
d. Write the function getScoresAndCalculateTotal( ) to determine the total score of the exams take by the student.
Use a loop to prompt for each exam score and accumulate the total as the user enters the scores. Return the
calculated total back to main( ).
e. Write the function calculateAverage( ) to determine the average grade earned by the student and return this
value back to main( ).
f. Write the function determineLetterGrade( ) that determines the letter grade earned by the student for the course
and returns this back to main( ).
g. Write the function displayAverageGrade( ) to display the student’s name, the average (to the nearest tenth of a
decimal) and letter grade earned by the student.

How do I get going past this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  
#include <iostream>
#include <string>

using namespace std;

string getStudentName(string a, string b);
void getNumberExams();
void getScoresandCalculateTotal();
void calculateAverage();
void determineLetterGrade();
void displayAverage();

int main()
{
	getStudentName(string a, string b);
	getNumberExams();
	getScoresandCalculateTotal();
	calculateAverage();
	determineLetterGrade();
	displayAverage();
}

string getStudentName(string& a, string& b)
{
	
}

void getNumberExams(int &test)
{
	
}

void getScoresandCalculateTotal()
{

}

void calculateAverage()
{

}

void determineLetterGrade()
{

}

void displayAverage()
{

}
Most of your functions currently take no parameters and return no value. Re-read the assignment and decide what the arguments and return value for each one should be.

After that, start writing each function. You may want to test each function before moving on to the next.
I'm trying to start off with the first function to get the student names but I don't understand how to use "pass by" methods to return a name :/
You need to pass the strings for first and last name by reference into the getStudentName() function. So line 7 should declare it like this:
void getStudentName(string &a, string &b);
The "&" means to pass the strings by reference. Next, line 24 should define it like this:
1
2
3
4
5
void getStudentName(string &a, string &b)
{
    // prompt the user for the name of the student. Assign first name to "a"
    // and last name to "b"
}


In main() you define two strings and pass them to getStudentName():
1
2
string first, last;
getStudentName(first, last);

Now when you call getStudentName(), the parameters a and b become synonyms for strings first and last. So when you change a and b in getStudentName(), you are changing first and last in main(). Pretty cool huh?

So given this, see if you can fill in the code in getStudentName().
Last edited on
So first and last become arguments in the main function but the &first and &last reference variables are the same as the &a and &b?
Technically first and last become local variables in the main() function, but I think that's what you meant. I probably confused you with my post because I had different names for the parameters in the declaration and the definition of getStudentName(). I'll edit the post to clarify.
Topic archived. No new replies allowed.