Advertisement
khaiwen1111

last c++

Apr 19th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #define SIZE 30
  6. using namespace std;
  7.  
  8. typedef struct
  9. {
  10. char name[31];
  11. int grade1;
  12. int grade2;
  13. double average;
  14. } STUDENTREC;
  15.  
  16. int read_grade(STUDENTREC stud_arr[], int index);
  17. void list(const STUDENTREC stud_arr[], int size);
  18.  
  19. int main(void)
  20. {
  21. int action, index = 0;
  22. bool quit = false;
  23. STUDENTREC students[SIZE];
  24.  
  25. while(!quit)
  26. {
  27. cout <<"\n\t\t******************************************";
  28. cout <<"\n\t\t**\tStudent Records Menu\t\t**";
  29. cout <<"\n\t\t******************************************\n\n";
  30. cout <<"\t\t<1> Read Grade.\n";
  31. cout <<"\t\t<2> List.\n";
  32. cout <<"\t\t<3> Quit.\n\n";
  33. cout <<"\tPlease enter your option --> ";
  34. cin >> action;
  35. cout <<"\n\n";
  36.  
  37. if(action == 1)
  38. index = read_grade(students, index);
  39. else if(action == 2)
  40. list(students, index);
  41. else if(action == 3)
  42. quit = true;
  43. else
  44. cout <<"\tWrong selection.\n";
  45. }
  46. }
  47.  
  48. int read_grade(STUDENTREC stud_arr[], int index)
  49. {
  50. if(index >= SIZE)
  51. cout << "Array is full.\n";
  52. else
  53. {
  54. cout <<"\tPlease enter the student name : ";
  55. fflush(stdin);
  56. cin.get(stud_arr[index].name, 31);
  57. cout <<"\tPlease enter the student grade 1: ";
  58. cin >> stud_arr[index].grade1;
  59. cout <<"\tPlease enter the student grade 2: ";
  60. cin >> stud_arr[index].grade2;
  61. stud_arr[index].average = (double)(stud_arr[index].grade1 + stud_arr[index].grade2) / 2.0;
  62. cout <<"\tThe average of the student grades is "
  63. << fixed << setprecision(2) << stud_arr[index].average << endl;
  64. index++;
  65. }
  66. return index;
  67. }
  68.  
  69. void list(const STUDENTREC stud_arr[], int size)
  70. {
  71. if(size <= 0)
  72. {
  73. cout << "\tNo record.\n";
  74. return;
  75. }
  76.  
  77. cout <<"Name\t\t\tGrade1\t\tGrade2\t\tAverage\n";
  78. for(int i = 0; i < size; i++)
  79. {
  80. if (strlen(stud_arr[i].name) < 15)
  81. cout << stud_arr[i].name << "\t\t"
  82. << setw(3) << stud_arr[i].grade1 << "\t\t"
  83. << setw(3) << stud_arr[i].grade2 << "\t\t"
  84. << setw(6) << fixed << setprecision(2)
  85. << stud_arr[i].average << endl;
  86. else
  87. cout << stud_arr[i].name << "\t"
  88. << setw(3) << stud_arr[i].grade1 << "\t\t"
  89. << setw(3) << stud_arr[i].grade2 << "\t\t"
  90. << setw(6) << fixed << setprecision(2)
  91. << stud_arr[i].average << endl;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement