fork download
  1. #include <iostream>
  2. #include <string.h>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. // struct //////////////////////
  7. struct TStudent {
  8. string name;
  9. int grade[4];
  10. float avg;
  11. };
  12. // end struct //////////////////////
  13.  
  14. // class //////////////////////
  15. class CGrade {
  16.  
  17. private:
  18. int amount;
  19. TStudent Student[30];
  20.  
  21. public:
  22. CGrade();
  23. ~CGrade();
  24. bool read();
  25. void display();
  26. void calculateAvg();
  27. void sort();
  28. void write();
  29. void add();
  30. void search();
  31. };
  32. // end class //////////////////////
  33.  
  34. // methods //////////////////////
  35. CGrade::CGrade() {}
  36.  
  37. CGrade::~CGrade() {}
  38.  
  39. bool CGrade::read()
  40. {
  41.  
  42. fstream File;
  43. int i = 0;
  44. bool success;
  45.  
  46. File.open("Text_11-17.txt", ios::in);
  47.  
  48. if (File.good()) {
  49.  
  50. while (!File.eof()) {
  51.  
  52. File >> Student[i].name;
  53.  
  54. for (int k = 0; k < 4; k++) {
  55.  
  56. File >> Student[i].grade[k];
  57. }
  58.  
  59. i++;
  60. }
  61.  
  62. amount = i;
  63. success = true;
  64. }
  65. else {
  66.  
  67. cout << "File could not be opened";
  68. File.close();
  69.  
  70. success = false;
  71. }
  72.  
  73. return success;
  74. }
  75.  
  76. void CGrade::display()
  77. {
  78.  
  79. for (int i = 0; i < amount; i++) {
  80.  
  81. cout << ">>>> Name: " << Student[i].name << endl;
  82.  
  83. for (int k = 0; k < 4; k++) {
  84.  
  85. cout << "> " << Student[i].grade[k] << endl;
  86. }
  87.  
  88. cout << ">> Durchschnitt: " << Student[i].avg << endl
  89. << endl;
  90. }
  91. }
  92.  
  93. void CGrade::calculateAvg()
  94. {
  95.  
  96. float total = 0;
  97.  
  98. for (int i = 0; i < amount; i++) {
  99.  
  100. total = 0;
  101.  
  102. for (int k = 0; k < 4; k++) {
  103.  
  104. total += Student[i].grade[k];
  105. }
  106.  
  107. Student[i].avg = total / 4;
  108. }
  109. }
  110.  
  111. void CGrade::sort()
  112. {
  113.  
  114. TStudent swap;
  115. bool sorted;
  116.  
  117. do {
  118.  
  119. sorted = 1;
  120.  
  121. for (int i = 0; i < amount - 1; i++) {
  122.  
  123. if (Student[i].avg > Student[i + 1].avg) {
  124.  
  125. swap = Student[i];
  126. Student[i] = Student[i + 1];
  127. Student[i + 1] = swap;
  128. sorted = 0;
  129. }
  130. }
  131. } while (sorted == 0);
  132. }
  133.  
  134. void CGrade::write()
  135. {
  136.  
  137. fstream File;
  138.  
  139. File.open("Text_11-17_new.txt", ios::out);
  140.  
  141. if (File.good()) {
  142. for (int i = 0; i < amount; i++) {
  143. File << ">>>> Student #" << i + 1 << ":\t" << Student[i].name << " <<<<" << endl
  144. << ">> Grades:\t";
  145.  
  146. for (int k = 0; k < 4; k++) {
  147. File << Student[i].grade[k] << " ";
  148. }
  149.  
  150. File << endl
  151. << "> Average:\t" << Student[i].avg << endl
  152. << endl;
  153. }
  154. }
  155. else {
  156.  
  157. cout << "File could not be opened";
  158. File.close();
  159. }
  160. }
  161.  
  162. void CGrade::add()
  163. {
  164. fstream File;
  165. string name;
  166. int grades[4];
  167.  
  168. cout << "Name des Schuelers:";
  169. cin >> name;
  170.  
  171. for (int i = 0; i < 4; i++) {
  172. cout << "Grade #" << i + 1 << ":";
  173. cin >> grades[i];
  174. }
  175.  
  176. File.open("Text_11-17.txt", ios::out | ios::app);
  177.  
  178. if (File.good()) {
  179.  
  180. File << endl
  181. << name;
  182.  
  183. for (int i = 0; i < 4; i++) {
  184. File << " " << grades[i];
  185. }
  186. }
  187. else {
  188.  
  189. cout << "File could not be opened";
  190. File.close();
  191. }
  192. }
  193.  
  194. void CGrade::search() {
  195.  
  196. string name;
  197.  
  198. cout<<"Enter a name to search for:";
  199. cin>>name;
  200.  
  201. for (int i=0; i<amount; i++) {
  202.  
  203. if(name == Student[i].name) {
  204.  
  205. cout<<">> Student found!"<<endl<<endl;
  206.  
  207. cout << ">>>> Name: " << Student[i].name << endl;
  208.  
  209. for (int k = 0; k < 4; k++) {
  210.  
  211. cout << "> " << Student[i].grade[k] << endl;
  212. }
  213.  
  214. cout << ">> Durchschnitt: " << Student[i].avg << endl
  215. << endl;
  216. }
  217. else {
  218. cout<<"Student could not be found";
  219. }
  220. }
  221. }
  222.  
  223. // end methods //////////////////////
  224.  
  225.  
  226. // main //////////////////////
  227. main()
  228. {
  229.  
  230. CGrade Grade;
  231. int choice;
  232. bool continue = true;
  233.  
  234. if (Grade.read()) {
  235.  
  236. Grade.calculateAvg();
  237. Grade.sort();
  238.  
  239.  
  240. do {
  241. cout<<">>>> Choose an action >>>>"<<endl<<
  242. "1)\tDisplay all students"<<endl<<
  243. "2)\tCreate new file with more student data"<<endl<<
  244. "3)\tAdd a student"<<endl;
  245.  
  246. cin>>choice;
  247.  
  248.  
  249. switch(choice) {
  250. case 1 :a
  251. Grade.display();
  252. break;
  253.  
  254. case 2 :
  255. Grade.write();
  256. break;
  257.  
  258. case 3 :
  259. Grade.add();
  260. break;
  261.  
  262. default:
  263. continue=false;
  264. break;
  265. }
  266.  
  267. } while (continue==true);
  268. }
  269. }
  270. // end main //////////////////////
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:20: fatal error: iostream: No such file or directory
 #include <iostream>
                    ^
compilation terminated.
stdout
Standard output is empty