944,147 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1597
  • C++ RSS
Mar 29th, 2007
0

struct functions - lost a wee bit (i hope) in calling the functions

Expand Post »
I cannot figure out how to "call" the functions into the main. Please help!!

C++ Syntax (Toggle Plain Text)
  1.  
  2. // ******************************************************************
  3. //
  4. // Grades.cpp
  5. //
  6. // This program computes student grades. For each student, two
  7. // quiz grades (graded on a 10 point basis), one midterm exam grade
  8. // and one final exam grade (each on a 100 point basis) are read
  9. // in. The final numeric grade is computed weighing the final
  10. // exam 50%, the midterm 25%, and the quizzes 25%. The numeric
  11. // grade and corresponding letter grade are output.
  12. //
  13. // ******************************************************************
  14. #include <iostream>
  15. using namespace std;
  16.  
  17.  
  18. //declarations
  19. double quiz1;
  20. double quiz2;
  21. double midtermExam;
  22. double finalExam;
  23. double courseAverage;
  24. //
  25. // Structure for a student record
  26. struct StudentRecord
  27. {
  28. //member functions
  29. int quiz1, quiz2, midtermExam, finalExam, courseAverage, letterGrade;
  30. };
  31.  
  32. //
  33. //function declarations
  34. void inputRecord (StudentRecord record);
  35. //postcondition
  36. void computeAverage (StudentRecord& record);
  37. //precondition
  38. char letterGrade (double numericGrade);
  39. //precondition
  40. void outputRecord (StudentRecord record);
  41. //precondition
  42.  
  43. //
  44. //main function - declares StudentRecord, Calls functions into action
  45. int main()
  46. {
  47. StudentRecord record;
  48. void inputRecord (StudentRecord.record);
  49. void computeAverage (StudentRecord.record);
  50. char letterGrade (double numericGrade);
  51. void outputRecord (StudentRecord.record);
  52. }
  53.  
  54. //
  55. //input definition
  56. void inputRecord (StudentRecord record)
  57. {
  58. cout << "Please Enter Your Grades Below" << endl << "Quiz Scores: ";
  59. cin >> quiz1;
  60. cout << " and ";
  61. cin >> quiz2;
  62. cout << endl;
  63. cout << "Midterm Exam Score: ";
  64. cin >> midtermExam;
  65. cout << endl;
  66. cout << "Final Exam Score: ";
  67. cin >> finalExam;
  68. cout << endl;
  69. }
  70.  
  71. //
  72. // average definition
  73. void computeAverage (StudentRecord& record)
  74. {
  75. const double EXAM_WT = 0.5;
  76. const double MIDTERM_WT = 0.25;
  77. const double QUIZ_WT = 0.25;
  78. double quiz1Percent, quiz2Percent;
  79.  
  80. // Convert the 10 point quizzes to a percent, then find the average
  81. quiz1Percent = 100 * record.quiz1 / 10.0;
  82. quiz2Percent = 100 * record.quiz2 / 10.0;
  83. double quizAvg = (quiz1Percent + quiz2Percent) / 2;
  84.  
  85. // Compute the weighted average to get the numeric course grade
  86. record.courseAverage = quizAvg * QUIZ_WT + record.midtermExam * MIDTERM_WT +
  87. record.finalExam * EXAM_WT;
  88.  
  89. // Call the letterGrade function to find the letter grade
  90. record.letterGrade = letterGrade (record.courseAverage);
  91. }
  92.  
  93. //
  94. // letterGrade defintion
  95. char letterGrade (double numericGrade)
  96. {
  97. char letter;
  98.  
  99. if (numericGrade < 60)
  100. letter = 'F';
  101. else if (numericGrade < 70)
  102. letter = 'D';
  103. else if (numericGrade < 80)
  104. letter = 'C';
  105. else if (numericGrade < 90)
  106. letter = 'B';
  107. else
  108. letter = 'A';
  109.  
  110. return letter;
  111. }
  112.  
  113.  
  114. //output definition
  115. void outputRecord (StudentRecord record)
  116. {
  117. cout << endl;
  118. cout << "Quiz Scores: " << record.quiz1 << " " << record.quiz2 << endl;
  119. cout << "Midterm Exam Score: " << record.midtermExam << endl;
  120. cout << "Final Exam Score: " << record.finalExam << endl;
  121. cout << endl;
  122. cout << "Course Average: " << record.courseAverage << endl;
  123. cout << "Final Letter Grade: " << record.letterGrade << endl;
  124. cout << endl;
  125. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tatumkay is offline Offline
2 posts
since Feb 2007
Mar 29th, 2007
0

Re: struct functions - lost a wee bit (i hope) in calling the functions

First of all, trash these globals at the top of the program!
C++ Syntax (Toggle Plain Text)
  1. double quiz1;
  2. double quiz2;
  3. double midtermExam;
  4. double finalExam;
  5. 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)
  1. void inputRecord (StudentRecord.record);
Another thing is that when you're passing 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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: string binary output
Next Thread in C++ Forum Timeline: C++ File Generator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC