Posted By

woofeR on 02/21/09


Tagged

error c Handling interface maximum avarage minimum grade compute student do-while


Versions (?)

Average, Minimum, Maximum Grade Computer


 / Published in: C++
 

  1. #include <cstdlib>
  2. #include <iostream>
  3. #include "firstQuiz.h"
  4.  
  5. using namespace std;
  6.  
  7. static int studentNo = 1;
  8. static int zeroInput = 0;
  9.  
  10. firstQuiz::firstQuiz(int i)
  11. {
  12. errorC = i;
  13. }
  14.  
  15. void firstQuiz::errorHandling()
  16. {
  17. //static int errorC; //we can use this instead of interface solution
  18.  
  19. //if there is an error, show the error message
  20. if (errorC == 1)
  21. cout << "error: try again: ";
  22.  
  23. //no problem
  24. else
  25. cout << "enter the negative number tolerance quantity: ";
  26.  
  27. cin >> zeroInput;
  28.  
  29. if (zeroInput < 0)
  30. {
  31. errorC = 1; //error logger
  32. errorHandling(); //recursion
  33. }
  34. }
  35.  
  36. //if we don't want to write cout's again and again, we creat a function
  37. void firstQuiz::pointInput()
  38. {
  39. cout << "please enter the grade for student " << studentNo++ << " : ";
  40. cin >> point;
  41. }
  42.  
  43. //"everything is something happen" (sinyor terim)
  44. void firstQuiz::compute()
  45. {
  46. int zeroCount = 0;
  47. char closer;
  48. counterA = 0; avarageA = 0; maximumA = 0; minimumA = 100;
  49. counterB = 0; avarageB = 0; maximumB = 0; minimumB = 100;
  50.  
  51. //classic do-while structure
  52. do{
  53.  
  54. pointInput();
  55.  
  56. //when entered grade is negative and error handler is activated
  57. while(point<0 && zeroCount != zeroInput)
  58. {
  59. zeroCount++;
  60. cout << "negative! want to quit? (y / n) : ";
  61. cin >> closer;
  62. if (closer == 'y' || closer == 'Y')
  63. {
  64. break;
  65. }
  66. if( closer == 'n' || closer == 'N')
  67. {
  68. pointInput();
  69. }
  70. }
  71. //for above 50
  72. if(point>50)
  73. {
  74. counterA++;
  75. avarageA+=point;
  76. if(point > maximumA)
  77. maximumA=point;
  78. if(point < minimumA)
  79. minimumA=point;
  80. }
  81.  
  82. //for belove 50
  83. if(point<=50)
  84. {
  85. //if it is negative, easily ignore that!
  86. if(point<0)
  87. break;
  88. counterB++;
  89. avarageB+=point;
  90. if(point > maximumB)
  91. maximumB=point;
  92. if(point < minimumB)
  93. minimumB=point;
  94. }
  95.  
  96. }while(point>=0);
  97. }
  98.  
  99. //compute avarages seperately
  100. void firstQuiz::computeAvarage()
  101. {
  102. avarageA = avarageA / counterA; //for above 50
  103. avarageB = avarageB / counterB; //for belove 50
  104. }
  105.  
  106. //result outputs
  107. void firstQuiz::printResult()
  108. {
  109. //+50
  110. cout << endl << "maximum (+ 50) is " << maximumA << endl;
  111. cout << "minimum (+ 50) is " << minimumA << endl;
  112. cout << "avarage (+ 50) is " << avarageA << endl << endl;
  113. //-50
  114. cout << "maximum (- 50) is " << maximumB << endl;
  115. cout << "minimum (- 50) is " << minimumB << endl;
  116. cout << "avarage (- 50) is " << avarageB << endl << endl;
  117. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: woofeR on February 21, 2009

It was my CS201 (at Bilkent University, Computer Science) Quiz question.

You need to login to post a comment.