| | |
Calculating grade scores
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Hello all,
I am creating a program, and I am currently stuck. I need someone to guide me on track on what I am doing wrong. My main goal is to get the program to run. I can't even do that. My C++ code is provided below. Thanks for all who helps.
Directions: Write a C++ computer program to compute the grades (assignment scores,lab scores,test scores, final exam score) and find the final grade of what an individual would receive in the class. You must use functions and atleast one must be a value-returning function.
Here is my code:
I am creating a program, and I am currently stuck. I need someone to guide me on track on what I am doing wrong. My main goal is to get the program to run. I can't even do that. My C++ code is provided below. Thanks for all who helps.
Directions: Write a C++ computer program to compute the grades (assignment scores,lab scores,test scores, final exam score) and find the final grade of what an individual would receive in the class. You must use functions and atleast one must be a value-returning function.
Here is my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; void testpoints(); void labpoints(); void projectpoints(); void finalpoints(); void finalgrade(double& testpoints, double& labpoints, double& projectpoints, double& finalpoints); void printresults (double testpoints, double labpoints, double projectpoints, double finalpoints); int main () { cout<<fixed<<showpoint; cout.precision(2); double testpoints; double labpoints; double projectpoints; double finalpoints; gettestscores(); getlabscores(); getprojectscores(); getfinalexamscores(); getfinalgrade(testpoints,labpoints,projectpoints,finalpoints); printresults(testpoints,labpoints,finalpoints,projectpoints,finalgrade); system ("PAUSE"); return 0; } double gettestscores () { int numtest; double gettestscores double testscores; double average; double sum = 0; cout<<"How many test did you take? "<<endl; cin>>numtest; for (int i=1; i<=numtest; i++) { cout<<"Enter scores for tests? "<<i<<endl; cin>>testscores; sum+=testscores; } } double getlabscores () { int numlab; double labscores; double average; double sum = 0; cout<<"How many labs did you take? "<<endl; cin>>numlab; for (int i=1; i<=numlab; i++) { cout<<"Enter scores for labs? "<<i<<endl; cin>>labscores; sum+=labscores; } } double getprojectscores () { int numtest; double projectscores; double average; double sum = 0; cout<<"How many projects did you take? "<<endl; cin>>numproject; for (int i=1; i<=numproject; i++) { cout<<"Enter scores for project? "<<i<<endl; cin>>projectscores; sum+=projectscores; } } double getfinalexamscores () { int numfinalexam; double finalexamscores; double average; double sum = 0; cout<<"How many final exams did you take? "<<endl; cin>>numfinalexam; for (int i=1; i<=numfinalexam; i++) { cout<<"Enter score for final exam? "<<i<<endl; cin>>finalexamscores; sum+=finalexamscores; } }
You have missed a semicolon at the end of this line.
You can't get your program to run should mean compiler errors or link errors. Next time post the error or warning messages by the compiler if any.
cpp Syntax (Toggle Plain Text)
double gettestscores
You can't get your program to run should mean compiler errors or link errors. Next time post the error or warning messages by the compiler if any.
バルサミコ酢やっぱいらへんで
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
I think the most problems lay in the way you've defined your functions/prototypes.
For example the names & return type are different, for example you list all function prototypes as returning void yet when you actually wrote the function they are set to return type double. You also prototype their names but when you call those functions in main() you call different functions that don't exist.
The printresults function is overloaded, in the prototype you've set it to recieve 4 arguments while in main() you've called the function with 5 arguments. finalgrade is the extra 1 over & that hasn't even been created in main() yet.
When prototyping functions there is no need to add variable names, in fact it should actually be an error to do that since those variables wouldn't exist yet. so those function prototypes should...
A) Match return type
B) Match function call names
C) List only container types for their argument list not variable names.
I'm going to //comment your code below as example, but I'm not changing all (just some of it) your code myself, because of this there could be more errors that I've mentioned in my post... But hey it's a start!
I've changed atleast the function prototypes so they are somewhat more correct... Although I've left them as "void" as I don't know which functions you want to return anything.. Basically change those which should return a value to double leave the others as void. (You'll also need to change the functions return type at their definition, eg if line 7 is "void labpoints" then Line 28 needs to be "labpoints();" & Line 61 needs to be "void labpoints()") Line 42 is missing a semicolon, I've added it in bold in the above code. On line 90 & 93 "numproject" should be "numtest" or numtest on line 84 'numtest' should be made into 'numproject' while line 90 & 93 are left alone.
There are alot of unreferenced variables in your functions that is, you're creating variables but not using them in functions. eg in the last function in your code "double average;" is not used anywhere in that function anywhere out of that function is out of scope.
I've only just noticed aswell 2 of your functions aren't even there
getfinalgrade(testpoints,labpoints,projectpoints,finalpoints);
printresults(testpoints,labpoints,finalpoints,projectpoints,finalgrade);
Those 2 are only prototyped but not in the actual code? (Unless I've missed it)
Unfortunately my post is probably going to seem confusing cause I'm not good as attempting to explain stuff, so short version is, match up function names & return types is the best place to start in my opinion.
For example the names & return type are different, for example you list all function prototypes as returning void yet when you actually wrote the function they are set to return type double. You also prototype their names but when you call those functions in main() you call different functions that don't exist.
The printresults function is overloaded, in the prototype you've set it to recieve 4 arguments while in main() you've called the function with 5 arguments. finalgrade is the extra 1 over & that hasn't even been created in main() yet.
When prototyping functions there is no need to add variable names, in fact it should actually be an error to do that since those variables wouldn't exist yet. so those function prototypes should...
A) Match return type
B) Match function call names
C) List only container types for their argument list not variable names.
I'm going to //comment your code below as example, but I'm not changing all (just some of it) your code myself, because of this there could be more errors that I've mentioned in my post... But hey it's a start!

cpp Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; void testpoints(); void labpoints(); void projectpoints(); void finalpoints(); void getfinalgrade(double&, double&, double&, double&); /*Function prototype should be closer to this.*/ void printresults (double, double, double, double, double); int main () { cout<<fixed<<showpoint; cout.precision(2); double testpoints; double labpoints; double projectpoints; double finalpoints; //No double finalgrade; here gettestscores(); //These function calls don't match the prototype names getlabscores(); getprojectscores(); getfinalexamscores(); getfinalgrade(testpoints,labpoints,projectpoints,finalpoints); printresults(testpoints,labpoints,finalpoints,projectpoints,finalgrade); system ("PAUSE"); return 0; } double gettestscores () { int numtest; double gettestscores[B];[/B] double testscores; double average; /*This is an unreferenced variable, you don't seem to have used it not only in this function but not in the other functions neither, are you planning too? If not then take this line out of all the functions except where you actually are going to use it*/ double sum = 0; cout<<"How many test did you take? "<<endl; cin>>numtest; for (int i=1; i<=numtest; i++) { cout<<"Enter scores for tests? "<<i<<endl; cin>>testscores; sum+=testscores; } } double getlabscores () { int numlab; double labscores; double average; double sum = 0; cout<<"How many labs did you take? "<<endl; cin>>numlab; for (int i=1; i<=numlab; i++) { cout<<"Enter scores for labs? "<<i<<endl; cin>>labscores; sum+=labscores; } } double getprojectscores () { int numtest; double projectscores; double average; double sum = 0; cout<<"How many projects did you take? "<<endl; cin>>numproject; for (int i=1; i<=numproject; i++) { cout<<"Enter scores for project? "<<i<<endl; cin>>projectscores; sum+=projectscores; } } double getfinalexamscores () { int numfinalexam; double finalexamscores; double average; double sum = 0; cout<<"How many final exams did you take? "<<endl; cin>>numfinalexam; for (int i=1; i<=numfinalexam; i++) { cout<<"Enter score for final exam? "<<i<<endl; cin>>finalexamscores; sum+=finalexamscores; } }
I've changed atleast the function prototypes so they are somewhat more correct... Although I've left them as "void" as I don't know which functions you want to return anything.. Basically change those which should return a value to double leave the others as void. (You'll also need to change the functions return type at their definition, eg if line 7 is "void labpoints" then Line 28 needs to be "labpoints();" & Line 61 needs to be "void labpoints()") Line 42 is missing a semicolon, I've added it in bold in the above code. On line 90 & 93 "numproject" should be "numtest" or numtest on line 84 'numtest' should be made into 'numproject' while line 90 & 93 are left alone.
There are alot of unreferenced variables in your functions that is, you're creating variables but not using them in functions. eg in the last function in your code "double average;" is not used anywhere in that function anywhere out of that function is out of scope.
I've only just noticed aswell 2 of your functions aren't even there
getfinalgrade(testpoints,labpoints,projectpoints,finalpoints);
printresults(testpoints,labpoints,finalpoints,projectpoints,finalgrade);
Those 2 are only prototyped but not in the actual code? (Unless I've missed it)
Unfortunately my post is probably going to seem confusing cause I'm not good as attempting to explain stuff, so short version is, match up function names & return types is the best place to start in my opinion.
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Here is my compiler log. Thanks for the reply.
Compiler: Default compiler
Executing g++.exe...
In function `int main()':
:32: error: `gettestscores' undeclared (first use this function)
:32: error: (Each undeclared identifier is reported only once for each function it appears in.)
:33: error: `getlabscores' undeclared (first use this function)
:34: error: `getprojectscores' undeclared (first use this function)
:35: error: `getfinalexamscores' undeclared (first use this function)
:36: error: `getfinalgrade' undeclared (first use this function)
:19: error: too many arguments to function `void printresults(double, double, double, double)'
:38: error: at this point in file
At global scope:
:44: error: `double gettestscores()' used prior to declaration
:45: error: expected unqualified-id before '{' token
:45: error: expected `,' or `;' before '{' token
:66: error: `double getlabscores()' used prior to declaration
:67: error: expected unqualified-id before '{' token
:67: error: expected `,' or `;' before '{' token
:87: error: `double getprojectscores()' used prior to declaration
:88: error: expected unqualified-id before '{' token
:88: error: expected `,' or `;' before '{' token
:107: error: `double getfinalexamscores()' used prior to declaration
:108: error: expected unqualified-id before '{' token
:108: error: expected `,' or `;' before '{' token
Execution terminated
Compiler: Default compiler
Executing g++.exe...
In function `int main()':
:32: error: `gettestscores' undeclared (first use this function)
:32: error: (Each undeclared identifier is reported only once for each function it appears in.)
:33: error: `getlabscores' undeclared (first use this function)
:34: error: `getprojectscores' undeclared (first use this function)
:35: error: `getfinalexamscores' undeclared (first use this function)
:36: error: `getfinalgrade' undeclared (first use this function)
:19: error: too many arguments to function `void printresults(double, double, double, double)'
:38: error: at this point in file
At global scope:
:44: error: `double gettestscores()' used prior to declaration
:45: error: expected unqualified-id before '{' token
:45: error: expected `,' or `;' before '{' token
:66: error: `double getlabscores()' used prior to declaration
:67: error: expected unqualified-id before '{' token
:67: error: expected `,' or `;' before '{' token
:87: error: `double getprojectscores()' used prior to declaration
:88: error: expected unqualified-id before '{' token
:88: error: expected `,' or `;' before '{' token
:107: error: `double getfinalexamscores()' used prior to declaration
:108: error: expected unqualified-id before '{' token
:108: error: expected `,' or `;' before '{' token
Execution terminated
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Hey guys, here is my updated code with my compiler errors. I'm having trouble "returning the value".
Compiler errors:
Compiler: Default compiler
Executing g++.exe...
In function `void testpoints()':
:66: error: return-statement with a value, in function returning 'void'
In function `void labpoints()':
:92: error: return-statement with a value, in function returning 'void'
In function `void projectpoints()':
:115: error: return-statement with a value, in function returning 'void'
In function `void finalexampoints()':
:137: error: return-statement with a value, in function returning 'void'
Execution terminated
In my print results at the bottom of the code is what I would like to print out into the program.
Compiler errors:
Compiler: Default compiler
Executing g++.exe...
In function `void testpoints()':
:66: error: return-statement with a value, in function returning 'void'
In function `void labpoints()':
:92: error: return-statement with a value, in function returning 'void'
In function `void projectpoints()':
:115: error: return-statement with a value, in function returning 'void'
In function `void finalexampoints()':
:137: error: return-statement with a value, in function returning 'void'
Execution terminated
In my print results at the bottom of the code is what I would like to print out into the program.
#include <iostream> #include <iomanip> using namespace std; const double test_percent = 0.45; const double lab_percent = 0.10; const double project_percent = 0.20; const double finalexam_percent = 0.25; void testpoints(); void labpoints(); void projectpoints(); void finalexampoints(); void finalgrade(double& testpoints, double& labpoints,double& finalexampoints,double& projectpoints); void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade); int main () { cout<<fixed<<showpoint; cout.precision(2); double testscores; double labscores; double projectscores; double finalexamscores; double finalgradescore; testpoints(); labpoints(); projectpoints(); finalexampoints(); finalgrade(testscores,labscores,projectscores,finalexamscores); printresults(testscores,labscores,finalexamscores,projectscores,finalgradescore); system ("PAUSE"); return 0; } void testpoints () { double numtest; double testpoints; double testscores; double average; double sum = 0; cout<<"How many test did you take? "<<endl; cin>>numtest; for (int i=1; i<=numtest; i++) { cout<<"Enter score for test #"<<i<<endl; cin>>testscores; sum+=testscores; average = (sum/numtest); return (average * test_percent); } } void labpoints () { double numlab; double labscores; double average; double sum = 0; cout<<"How many labs did you take? "<<endl; cin>>numlab; for (int i=1; i<=numlab; i++) { cout<<"Enter score for lab #"<<i<<endl; cin>>labscores; sum+=labscores; average = (sum/numlab); return (average * lab_percent); } } void projectpoints () { double numproject; double projectscores; double average; double sum = 0; cout<<"How many projects did you take? "<<endl; cin>>numproject; for (int i=1; i<=numproject; i++) { cout<<"Enter score for project #"<<i<<endl; cin>>projectscores; sum+=projectscores; average = (sum/numproject); return (average * project_percent); } } void finalexampoints () { double numfinalexam; double finalexamscores; double average; double sum = 0; cout<<"How many final exams did you take? "<<endl; cin>>numfinalexam; for (int i=1; i<=numfinalexam; i++) { cout<<"Enter score for Final Exam #"<<i<<endl; cin>>finalexamscores; sum+=finalexamscores; average = (sum/numfinalexam); return (average * finalexam_percent); } } void finalgrade(double& testpoints, double& labpoints,double& finalexampoints,double& projectpoints) { } void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade) { double totalpoints; double testscores; double labscores; double projectscores; double finalexamscores; double finalgrade; totalpoints = (testscores + labscores + projectscores + finalexamscores); cout<<"Test points: "<<testscores<<endl; cout<<"Lab points: "<<labscores<<endl; cout<<"Project points: "<<projectscores<<endl; cout<<"Final Exam points: "<<finalexamscores<<endl; cout<<"Total points: "<<totalpoints<<endl; cout<<"Final Grade: "<<finalgrade<<endl; }
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
Prototype & Function return types don't match but other than that it's looking much better! 
I'll comment the code where you'll need to change, I'll change & comment 1, you just have to do the rest for each of the other errors, as it's the same for each one.
In the last function of print results, in the argument list which is
void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade)
in bold. The variables are being defined there as the function gets passed the variables from main() so there is no need to redefine them with new names especially considering they'd all be default value & you'd likely always get a printing out of 0 or something similar. I'd assume the values you want to use in there would be those in the argument list, as there is no other real reason to pass the values across.
I'd assume there be more errors/warnings after fixing those, perhaps best to take 1 at a time though, fix those, then work on the next lot that crop up. For example
Not so much an error but more a warning from compiler that there is no return value by default in that function (not all control paths return a value) if for some reason numlab is lower or equal to i before it can even run through the code in that block then in essense it's not returning anything. Could possibly turn up a run-time error or similar.

I'll comment the code where you'll need to change, I'll change & comment 1, you just have to do the rest for each of the other errors, as it's the same for each one.
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> using namespace std; const double test_percent = 0.45; const double lab_percent = 0.10; const double project_percent = 0.20; const double finalexam_percent = 0.25; double testpoints(); /*This & where you write the actual function needs to be changed, same for rest of functions that turn up the error/have return values*/ void labpoints(); void projectpoints(); void finalexampoints(); void finalgrade(double& testpoints, double& labpoints,double& finalexampoints,double& projectpoints); void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade); int main () { cout<<fixed<<showpoint; cout.precision(2); double testscores; double labscores; double projectscores; double finalexamscores; double finalgradescore; testpoints(); labpoints(); projectpoints(); finalexampoints(); finalgrade(testscores,labscores,projectscores,finalexamscores); printresults(testscores,labscores,finalexamscores,projectscores,finalgradescore); system ("PAUSE"); return 0; } double testpoints () //Change to return type double { double numtest; double testpoints; /*Remove this line it's not used in this function*/ double testscores; double average; double sum = 0; cout<<"How many test did you take? "<<endl; cin>>numtest; for (int i=1; i<=numtest; i++) { cout<<"Enter score for test #"<<i<<endl; cin>>testscores; sum+=testscores; average = (sum/numtest); return (average * test_percent); } } void labpoints () { double numlab; double labscores; double average; double sum = 0; cout<<"How many labs did you take? "<<endl; cin>>numlab; for (int i=1; i<=numlab; i++) { cout<<"Enter score for lab #"<<i<<endl; cin>>labscores; sum+=labscores; average = (sum/numlab); return (average * lab_percent); } } void projectpoints () { double numproject; double projectscores; double average; double sum = 0; cout<<"How many projects did you take? "<<endl; cin>>numproject; for (int i=1; i<=numproject; i++) { cout<<"Enter score for project #"<<i<<endl; cin>>projectscores; sum+=projectscores; average = (sum/numproject); return (average * project_percent); } } void finalexampoints () { double numfinalexam; double finalexamscores; double average; double sum = 0; cout<<"How many final exams did you take? "<<endl; cin>>numfinalexam; for (int i=1; i<=numfinalexam; i++) { cout<<"Enter score for Final Exam #"<<i<<endl; cin>>finalexamscores; sum+=finalexamscores; average = (sum/numfinalexam); return (average * finalexam_percent); } } void finalgrade(double& testpoints, double& labpoints,double& finalexampoints,double& projectpoints) { } void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade) { double totalpoints; double testscores; double labscores; double projectscores; double finalexamscores; double finalgrade; /*finalgrade is redfined here, but overall if you're passing by value & reference you shouldn't need these if you're expecting to use values passed to the function & should only define local variables if you expect them to only be used locally in a function.*/ totalpoints = (testscores + labscores + projectscores + finalexamscores); /*If you meant to use the variables passed into this function then use the variables in the argument list that is use 'testpoints', 'labpoints' that are in the function argument list if that is what you were meaning to do*/ cout<<"Test points: "<<testscores<<endl; cout<<"Lab points: "<<labscores<<endl; cout<<"Project points: "<<projectscores<<endl; cout<<"Final Exam points: "<<finalexamscores<<endl; cout<<"Total points: "<<totalpoints<<endl; cout<<"Final Grade: "<<finalgrade<<endl; }
In the last function of print results, in the argument list which is
void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade)
in bold. The variables are being defined there as the function gets passed the variables from main() so there is no need to redefine them with new names especially considering they'd all be default value & you'd likely always get a printing out of 0 or something similar. I'd assume the values you want to use in there would be those in the argument list, as there is no other real reason to pass the values across.
I'd assume there be more errors/warnings after fixing those, perhaps best to take 1 at a time though, fix those, then work on the next lot that crop up. For example
cpp Syntax (Toggle Plain Text)
double labpoints () { double numlab; double labscores; double average; double sum = 0; cout<<"How many labs did you take? "<<endl; cin>>numlab; for (int i=1; i<=numlab; i++) { cout<<"Enter score for lab #"<<i<<endl; cin>>labscores; sum+=labscores; average = (sum/numlab); return (average * lab_percent); } }
Not so much an error but more a warning from compiler that there is no return value by default in that function (not all control paths return a value) if for some reason numlab is lower or equal to i before it can even run through the code in that block then in essense it's not returning anything. Could possibly turn up a run-time error or similar.
Last edited by Yiuca; Sep 6th, 2008 at 5:43 pm.
•
•
Join Date: Mar 2008
Posts: 370
Reputation:
Solved Threads: 0
Thanks for replying again. I applied everything you told me to do except the last 2 parts when you explained about the "final grade" and "total points" part. I didn't quite understand that part. I'm new to C++, so I don't quite understand a lot of the terminology yet. My program runs now (which is a good thing).
After booting up program:
When my program asks "How many tests did you take?"
I answer with "3" (I want to put in 3 test scores)
Then it takes only 1 score, and then goes on to "How many labs did you take?" which only takes 1 lab score. How do I fix this problem?
Also, I'm trying to understand about the final grade and total points part at the bottom.
Here is my updated code:
After booting up program:
When my program asks "How many tests did you take?"
I answer with "3" (I want to put in 3 test scores)
Then it takes only 1 score, and then goes on to "How many labs did you take?" which only takes 1 lab score. How do I fix this problem?
Also, I'm trying to understand about the final grade and total points part at the bottom.
Here is my updated code:
#include <iostream> #include <iomanip> using namespace std; const double test_percent = 0.45; const double lab_percent = 0.10; const double project_percent = 0.20; const double finalexam_percent = 0.25; double testpoints(); double labpoints(); double projectpoints(); double finalexampoints(); double finalgrade(double& testpoints, double& labpoints,double& finalexampoints,double& projectpoints); double printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade); int main () { cout<<fixed<<showpoint; cout.precision(2); double testscores; double labscores; double projectscores; double finalexamscores; double finalgradescore; testpoints(); labpoints(); projectpoints(); finalexampoints(); finalgrade(testscores,labscores,projectscores,finalexamscores); printresults(testscores,labscores,finalexamscores,projectscores,finalgradescore); system ("PAUSE"); return 0; } double testpoints () { double numtest; double testscores; double average; double sum = 0; cout<<"How many test did you take? "<<endl; cin>>numtest; for (int i=1; i<=numtest; i++) { cout<<"Enter score for test #"<<i<<endl; cin>>testscores; sum+=testscores; average = (sum/numtest); return (average * test_percent); } } double labpoints () { double numlab; double labscores; double average; double sum = 0; cout<<"How many labs did you take? "<<endl; cin>>numlab; for (int i=1; i<=numlab; i++) { cout<<"Enter score for lab #"<<i<<endl; cin>>labscores; sum+=labscores; average = (sum/numlab); return (average * lab_percent); } } double projectpoints () { double numproject; double projectscores; double average; double sum = 0; cout<<"How many projects did you take? "<<endl; cin>>numproject; for (int i=1; i<=numproject; i++) { cout<<"Enter score for project #"<<i<<endl; cin>>projectscores; sum+=projectscores; average = (sum/numproject); return (average * project_percent); } } double finalexampoints () { double numfinalexam; double finalexamscores; double average; double sum = 0; cout<<"How many final exams did you take? "<<endl; cin>>numfinalexam; for (int i=1; i<=numfinalexam; i++) { cout<<"Enter score for Final Exam #"<<i<<endl; cin>>finalexamscores; sum+=finalexamscores; average = (sum/numfinalexam); return (average * finalexam_percent); } } double finalgrade(double& testpoints, double& labpoints,double& finalexampoints,double& projectpoints) { } double printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade) { double totalpoints; double testscores; double labscores; double projectscores; double finalexamscores; totalpoints = (testscores + labscores + projectscores + finalexamscores); cout<<"Test points: "<<testscores<<endl; cout<<"Lab points: "<<labscores<<endl; cout<<"Project points: "<<projectscores<<endl; cout<<"Final Exam points: "<<finalexamscores<<endl; cout<<"Total points: "<<totalpoints<<endl; cout<<"Final Grade: "<<finalgrade<<endl; }
•
•
Join Date: Jul 2005
Posts: 1,749
Reputation:
Solved Threads: 283
>>How do I fix this problem?
move these two line outside of the for loop.
average = (sum/numtest);
return (average * test_percent);
You only need to do them once. The way you have it you exit the function with the return statement after just one iteration of the loop.
>>I'm trying to understand about the final grade and total points part at the bottom
When a function ends it returns a value, maybe it's nothing, ie, void, maybe it's a double, maybe it's a reference to a stream, maybe it's a user defined type, whatever. The return value can be ignored, as you are doing in all of your functions, or stored in a variable. You have appropriate variables declared to hold the return values of the various functions and then pass those values to the functions that are expecting them.
I'd just add up the return values of all appropriate functions to get a total score and then compare that score to a curve. Maybe it's
100-93 A
92-86 B
85-79 C
78-70 D
<70 F
I don't know what curve you want/need to use is though. I'll leave it up to you to determine how you write the code to determine the grade received.
move these two line outside of the for loop.
average = (sum/numtest);
return (average * test_percent);
You only need to do them once. The way you have it you exit the function with the return statement after just one iteration of the loop.
>>I'm trying to understand about the final grade and total points part at the bottom
When a function ends it returns a value, maybe it's nothing, ie, void, maybe it's a double, maybe it's a reference to a stream, maybe it's a user defined type, whatever. The return value can be ignored, as you are doing in all of your functions, or stored in a variable. You have appropriate variables declared to hold the return values of the various functions and then pass those values to the functions that are expecting them.
I'd just add up the return values of all appropriate functions to get a total score and then compare that score to a curve. Maybe it's
100-93 A
92-86 B
85-79 C
78-70 D
<70 F
I don't know what curve you want/need to use is though. I'll leave it up to you to determine how you write the code to determine the grade received.
•
•
Join Date: Sep 2008
Posts: 83
Reputation:
Solved Threads: 15
•
•
•
•
Thanks for replying again. I applied everything you told me to do except the last 2 parts when you explained about the "final grade" and "total points" part. I didn't quite understand that part. I'm new to C++, so I don't quite understand a lot of the terminology yet. My program runs now (which is a good thing).
Here is simply a copy of the code/function with my comments so you don't have to keep scrolling up & down the page too much.
cpp Syntax (Toggle Plain Text)
void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade) { double totalpoints; double testscores; double labscores; double projectscores; double finalexamscores; double finalgrade; /*finalgrade is redfined here, but overall if you're passing by value & reference you shouldn't need these if you're expecting to use values passed to the function & should only define local variables if you expect them to only be used locally in a function.*/ totalpoints = (testscores + labscores + projectscores + finalexamscores); /*If you meant to use the variables passed into this function then use the variables in the argument list that is use 'testpoints', 'labpoints' that are in the function argument list if that is what you were meaning to do*/ cout<<"Test points: "<<testscores<<endl; cout<<"Lab points: "<<labscores<<endl; cout<<"Project points: "<<projectscores<<endl; cout<<"Final Exam points: "<<finalexamscores<<endl; cout<<"Total points: "<<totalpoints<<endl; cout<<"Final Grade: "<<finalgrade<<endl; }
Starting with re-defined, the variable finalgrade was passed into printresults() near the end you see the last argument double& finalgrade, the other variables you made in there aren't needed however as they would all be created with a default value (usually 0) so what you'd have as output in that function is
Test points: 0
Lab points: 0
etc
Because you're using newly created local variables which are unitialized, that is they have been assigned no values. When you have a function like
void printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade)
what is happening is in main() where printresults is called, it passes testpoints, labpoints & all the other values, then in printresults, it automatically creates them as you create them, example.
cpp Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::cin; using std::endl; double printresults(double, double&, int, int&); int main() { double d_value1 = 58.5; double d_value2 = 33.7; int i_value1 = 4; int i_value2 = 7; double ret_value; ret_value = printresults(d_value1, d_value2, i_value1, i_value2); cout << "d_value1 is: " << d_value1 << endl; cout << "d_value2 is: " << d_value2 << endl; cout << "i_value1 is: " << i_value1 << endl; cout << "i_value2 is: " << i_value2 << endl; cout << "ret_value is: " << ret_value << endl; cin.get(); return 0; } double printresults(double d_value1, double &d_value2, int i_value1, int &i_value2) { cout << "printresults function-----------------\n"; cout << "d_value1 is: " << d_value1 << endl; cout << "d_value2 is: " << d_value2 << endl; cout << "i_value1 is: " << i_value1 << endl; cout << "i_value2 is: " << i_value2 << endl; cout << "endof printresults output-------------\n"; d_value1 = d_value1 * 10; d_value2 = d_value2 * 5; i_value1 = i_value1 * i_value1; i_value2 = i_value2 * i_value1; return d_value1 * d_value2; }
Ok so in main() on line 17 as you can see main called the function "printresults" & sends 4 values, 2 by value, 2 by reference, the ones passed by reference are the ones with the ampersand in front of them (&) so basically d_value2 & i_value2 are the values sent by reference, now on line 29 you can see the function & it's argument list, but as you can see in the function itself I have not made anymore doubles or ints, I've simply used the names from that argument list to main multiply those variables in different ways.
So generally what I'm saying is if in the printresults() function I added say added a line to line 37 & added "double d_value1" that would be redefining a variable, since it is already made by the function when it were invoked.
For explaination sake aswell, just incase. If you noticed 2 of the numbers when displayed in main() don't change, while the others 2, if observant, you'll notice that the numbers that change are the ones passed by reference. That is, (double &d_value2, int &i_value2) have changed while (double d_value1, int i_value1) have not. Passing by value only passes by value, the function creates a copy in which you can change the value but the original in main() is not changed, while if you pass by reference you'll be passing the address of the variable & hence changing the actual value in main() which is why the 2 passed by reference show change in main() while the others passed by value are not.
So
totalpoints = (testscores + labscores + projectscores + finalexamscores);
should use the variable names from
double printresults(double testpoints,double labpoints,double finalexampoints,double projectpoints,double& finalgrade)
so perhaps it should look more like
totalpoints = (testpoints + labpoints + projectpoints + finalexampoints);
Hope what I meant is a little more clearer to you now.
Lerner answers other question, so not going to repeat!
Last edited by Yiuca; Sep 6th, 2008 at 9:30 pm.
![]() |
Similar Threads
- c++ functions for grades (C++)
- serious help calculating averages (C++)
- correct my algorithm for average problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Problem with a string (char array) and self made strcpy
- Next Thread: if help
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






