•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,909 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,636 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 950 | Replies: 1
![]() |
•
•
Join Date: Feb 2007
Posts: 2
Reputation:
Rep Power: 0
Solved Threads: 0
I cannot figure out how to "call" the functions into the main. Please help!!
// ******************************************************************
//
// 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.
cplusplus 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():
cplusplus 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.
tuxation.com - Linux articles, tutorials, and discussions
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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



Linear Mode