PDA

View Full Version : C++ value-returning function problem


amazingme001
07-27-2005, 05:58 AM
gif if anyone could help me with this problem, I would greatly appreciate it!
I have to use value-returning function so it computes and returns the course grade(must be output in the main() ) . So far I have program written as a void function. What would be the next step?

here is the code:
// Program: Compute grade.This program reads a course score and prints the
// associated course grade.

#include <iostream>
#include "EXAMPLE07.H" // my headerfile

using namespace std;
void getScore(int& score);
void printGrade(int score);

int main ()

{
int courseScore;

cout<<"Line 1: Based on the course score, this program "
<<"computes the course grade."<<endl;
getScore(courseScore);
printGrade(courseScore);
return 0;
}
void getScore(int& score)

{

cout<<"Line 4: Enter course score--> ";
cin>>score;
cout<<endl<<" Course score is "

<<score<<endl;

}
void printGrade(int score)

{

cout<<"Your grade for the course is ";

if(score >= 90)

cout<<"A"<<endl;

else if(score >= 80)

cout<<"B"<<endl;

else if(score >= 70)

cout<<"C"<<endl;

else if(score >= 60)

cout<<"D"<<endl;

else

cout<<"F"<<endl;

}


p.s. I'll keep on trying to figure it out

shmoove
07-27-2005, 10:00 AM
The next step is to make that void function into an "int function", that returns the value instead of setting a reference:

int getScore()
{
int score;
cout<<"Line 4: Enter course score--> ";
cin>>score;
cout<<endl<<" Course score is "
<<score<<endl;
return score; // this is where we return the value
}

You can then call it and get the return value like this:

courseScore = getScore();


shmoove

amazingme001
07-27-2005, 04:11 PM
I will continue working on the problem! :thumbsup: