fork download
  1. /******************************************************************************
  2. Find marks of students out of 40 to 100 and as percentage
  3. Enter admission number to see only his/her details
  4. Added compare_admission_no() method
  5. rakak - 21.06.2022
  6. V 1.2.0
  7. *******************************************************************************/
  8.  
  9. //using namespace std;
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13.  
  14. class Student
  15. {
  16.  
  17. int admission_no;
  18. std::string name;
  19. float english;
  20. float language;
  21. float maths;
  22. float science;
  23. float sst;
  24.  
  25. public:
  26. Student()
  27. {
  28. admission_no = 0;
  29. name = "no name";
  30. english = 0;
  31. language = 0;
  32. maths = 0;
  33. science = 0;
  34. sst = 0;
  35. }
  36.  
  37. Student(int admission_no, std::string name, float english, float language, float maths, float science, float sst)
  38. {
  39. this-> admission_no = admission_no;
  40. this-> name = name;
  41. this -> english = english;
  42. this -> language = language;
  43. this-> maths = maths;
  44. this-> science = science;
  45. this-> sst = sst;
  46. }
  47.  
  48. void get_details()
  49. {
  50. std::cout << "Roll Number: " << admission_no << std::endl << "Name: " << \
  51. name << std::endl << "English: " << english << std::endl << "Language: " << \
  52. language << std::endl << "Maths: " << maths << std::endl << "Science: " << \
  53. science << std::endl << "SST: " << sst << std::endl << std::endl;
  54. }
  55.  
  56. void convert_marks()
  57. {
  58. admission_no;
  59. name;
  60. english = (english/40)*100;
  61. language = (language/40)*100;
  62. maths = (maths/40)*100;
  63. science = (science/40)*100;
  64. sst = (sst/40)*100;
  65. }
  66.  
  67. float get_admission_no()
  68. {
  69. std::cout << "Enter admission number: ";
  70. std::cin >> admission_no;
  71.  
  72. return admission_no;
  73. }
  74.  
  75. void compare_admission_no(Student lhs, float rhs)
  76. {
  77. if (lhs.admission_no == rhs) {
  78. std::cout << "Marks out of 40:" << std::endl;
  79. lhs.get_details();
  80. std::cout << std::endl << "Marks out of 100:" << std::endl;
  81. lhs.convert_marks();
  82. lhs.get_details();
  83. } else {
  84. std::cout << "Student is not in the database" << std::endl;
  85. }
  86. }
  87. };
  88.  
  89. void file_not_exist_create(std::string file_name){
  90. std::string filename = file_name;
  91. std::fstream marks_file;
  92.  
  93. marks_file.open(filename, std::fstream::in | std::fstream::out | std::fstream::app);
  94.  
  95.  
  96. // If file does not exist, Create new file
  97. if (!marks_file)
  98. {
  99. std::cout << "Cannot open file, file does not exist. Creating new file..";
  100.  
  101. marks_file.open(filename, std::fstream::in | std::fstream::out | std::fstream::trunc);
  102. marks_file << std::endl;
  103. marks_file.close();
  104.  
  105. }
  106. else
  107. { // use existing file - Append, Write, Work with the opened file
  108. std::cout << filename << " exist." << std::endl;
  109.  
  110. marks_file << "Appending writing and working with existing file" << std::endl;
  111. marks_file.close();
  112. std::cout << std::endl;
  113. }
  114. }
  115.  
  116.  
  117.  
  118. int main()
  119. {
  120. Student s1(1,"Pavithra",33,36,31,39,35), s2(2,"Abhinav",22,31,29,24,37), s3(3,"David",25,21,29,31,33);
  121.  
  122. Student sid, comp;
  123. float result = sid.get_admission_no();
  124.  
  125. comp.compare_admission_no(s1,result);
  126. comp.compare_admission_no(s2,result);
  127. comp.compare_admission_no(s3,result);
  128.  
  129. file_not_exist_create("marks.txt");
  130.  
  131. return 0;
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
Success #stdin #stdout 0.01s 5516KB
stdin
2
5
1
1
1
1
6
2
1
3
stdout
Enter admission number: Student is not in the database
Marks out of 40:
Roll Number: 2
Name: Abhinav
English: 22
Language: 31
Maths: 29
Science: 24
SST: 37


Marks out of 100:
Roll Number: 2
Name: Abhinav
English: 55
Language: 77.5
Maths: 72.5
Science: 60
SST: 92.5

Student is not in the database
Cannot open file, file does not exist. Creating new file..