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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2007
Posts: 2
Reputation: tatumkay is an unknown quantity at this point 
Solved Threads: 0
tatumkay tatumkay is offline Offline
Newbie Poster

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

 
0
  #1
Mar 29th, 2007
I cannot figure out how to "call" the functions into the main. Please help!!

  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #2
Mar 29th, 2007
First of all, trash these globals at the top of the program!
  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():
  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.
"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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1429 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC