| | |
struct functions - lost a wee bit (i hope) in calling the functions
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2007
Posts: 2
Reputation:
Solved Threads: 0
I cannot figure out how to "call" the functions into the main. Please help!!
C++ Syntax (Toggle Plain Text)
// ****************************************************************** // // Grades.cpp // // This program computes student grades. For each student, two // quiz grades (graded on a 10 point basis), one midterm exam grade // and one final exam grade (each on a 100 point basis) are read // in. The final numeric grade is computed weighing the final // exam 50%, the midterm 25%, and the quizzes 25%. The numeric // grade and corresponding letter grade are output. // // ****************************************************************** #include <iostream> using namespace std; //declarations double quiz1; double quiz2; double midtermExam; double finalExam; double courseAverage; // // Structure for a student record struct StudentRecord { //member functions int quiz1, quiz2, midtermExam, finalExam, courseAverage, letterGrade; }; // //function declarations void inputRecord (StudentRecord record); //postcondition void computeAverage (StudentRecord& record); //precondition char letterGrade (double numericGrade); //precondition void outputRecord (StudentRecord record); //precondition // //main function - declares StudentRecord, Calls functions into action int main() { StudentRecord record; void inputRecord (StudentRecord.record); void computeAverage (StudentRecord.record); char letterGrade (double numericGrade); void outputRecord (StudentRecord.record); } // //input definition void inputRecord (StudentRecord record) { cout << "Please Enter Your Grades Below" << endl << "Quiz Scores: "; cin >> quiz1; cout << " and "; cin >> quiz2; cout << endl; cout << "Midterm Exam Score: "; cin >> midtermExam; cout << endl; cout << "Final Exam Score: "; cin >> finalExam; cout << endl; } // // average definition void computeAverage (StudentRecord& record) { const double EXAM_WT = 0.5; const double MIDTERM_WT = 0.25; const double QUIZ_WT = 0.25; double quiz1Percent, quiz2Percent; // Convert the 10 point quizzes to a percent, then find the average quiz1Percent = 100 * record.quiz1 / 10.0; quiz2Percent = 100 * record.quiz2 / 10.0; double quizAvg = (quiz1Percent + quiz2Percent) / 2; // Compute the weighted average to get the numeric course grade record.courseAverage = quizAvg * QUIZ_WT + record.midtermExam * MIDTERM_WT + record.finalExam * EXAM_WT; // Call the letterGrade function to find the letter grade record.letterGrade = letterGrade (record.courseAverage); } // // letterGrade defintion char letterGrade (double numericGrade) { char letter; if (numericGrade < 60) letter = 'F'; else if (numericGrade < 70) letter = 'D'; else if (numericGrade < 80) letter = 'C'; else if (numericGrade < 90) letter = 'B'; else letter = 'A'; return letter; } //output definition void outputRecord (StudentRecord record) { cout << endl; cout << "Quiz Scores: " << record.quiz1 << " " << record.quiz2 << endl; cout << "Midterm Exam Score: " << record.midtermExam << endl; cout << "Final Exam Score: " << record.finalExam << endl; cout << endl; cout << "Course Average: " << record.courseAverage << endl; cout << "Final Letter Grade: " << record.letterGrade << endl; cout << endl; }
First of all, trash these globals at the top of the program!
That's why you made a struct. So you wouldn't have to use globals. If you need to modify values inside one of your functions, use references or pointers. That's what they're there for.
Also, don't put the function's return type when calling. For example, this is wrong in main():
Another thing is that when you're passing
And don't forget that you need to pass a reference to inputRecord. Otherwise you'll just lose the values that the user inputted when the function returns.
C++ Syntax (Toggle Plain Text)
double quiz1; double quiz2; double midtermExam; double finalExam; double courseAverage;
That's why you made a struct. So you wouldn't have to use globals. If you need to modify values inside one of your functions, use references or pointers. That's what they're there for.
Also, don't put the function's return type when calling. For example, this is wrong in main():
C++ Syntax (Toggle Plain Text)
void inputRecord (StudentRecord.record);
record, it's just that: record. You don't need to prefix it with StudentRecord. .And don't forget that you need to pass a reference to inputRecord. Otherwise you'll just lose the values that the user inputted when the function returns.
"Technological progress is like an axe in the hands of a pathological criminal."
All my posts may be freely redistributed under the terms of the MIT license.
All my posts may be freely redistributed under the terms of the MIT license.
![]() |
Similar Threads
- calling python functions from cpp functions. (Python)
- __declspec(dllexport) calling exported functions (C++)
- Struct in Functions (C)
- Student requesting help - Please! (C++)
- Statistical functions in C++/Excel (C++)
- Converting Struct to class lost with pointers (C++)
Other Threads in the C++ Forum
- Previous Thread: string binary output
- Next Thread: C++ File Generator
Views: 1429 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph 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 search simple sort sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






