Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. // Lab 02 - Question #1 solution
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstring>
  6.  
  7. using namespace std;
  8. const int NO_OF_QUESTIONS = 20;
  9.  
  10. //function to return grade given score
  11. char examGrade(int score)
  12. {
  13. float percent = ((float) score / (NO_OF_QUESTIONS * 2)) * 100;
  14. if (percent < 60)
  15. return 'F';
  16. else if (percent >= 60 && percent < 70)
  17. return 'D';
  18. else if (percent >= 70 && percent < 80)
  19. return 'C';
  20. else if (percent >= 80 && percent < 90)
  21. return 'B';
  22. else
  23. return 'A';
  24. }
  25.  
  26. int main()
  27. {
  28. char keyString[NO_OF_QUESTIONS + 1];
  29. char answerString[NO_OF_QUESTIONS + 1];
  30. char ID[9];
  31. char ch;
  32.  
  33. int score;
  34. int len;
  35. ifstream infile;
  36.  
  37. //should take this open to a function
  38. infile.open("Lab02_Q01_Data.txt");
  39. if (!infile)
  40. {
  41. cout << "Cannot open the input file. Program terminates!"
  42. << endl;
  43. return 1;
  44. }
  45.  
  46. cout << "Processing Data" << endl << endl;
  47. infile.get(keyString, NO_OF_QUESTIONS + 1);
  48.  
  49. cout << "Key: " << keyString << endl << endl;
  50. cout << endl << endl;
  51. cout << " ID " << "\t\tAnswers \tScore \t\tGrade" << endl << endl;
  52. //reading the first line
  53. while (!infile.eof())
  54. {
  55. score = 0;
  56.  
  57. infile >> ID;
  58. infile.get(ch);
  59. infile.get(answerString, NO_OF_QUESTIONS + 1);
  60.  
  61. for (int i = 0; i < NO_OF_QUESTIONS; i++)
  62. {
  63. if (answerString[i] == keyString[i])
  64. score += 2;
  65. else
  66. score--;
  67. }
  68.  
  69. if (ID[0] != '\0')
  70. cout << ID << " " << answerString << "\t" << score << "\t\t" << examGrade(score) << endl;
  71. }
  72.  
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement