Guest User

Untitled

a guest
May 13th, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. struct Admin {
  10. string username;
  11. string password;
  12. };
  13.  
  14. struct User {
  15. int rollNo;
  16. string name;
  17. string fatherName;
  18. bool gender;
  19. double cgpa;
  20. };
  21.  
  22. Admin admin;
  23. User user[100];
  24. int num_users = 0;
  25. bool isLoggedIn = true;
  26.  
  27. void writeUser(User user) {
  28. ofstream myFile("users.txt", ios::app);
  29. if (myFile.is_open()) {
  30. myFile << user.rollNo << endl;
  31. myFile << user.name << endl;
  32. myFile << user.fatherName << endl;
  33. myFile << user.gender << endl;
  34. myFile << user.cgpa << endl;
  35. myFile << endl;
  36. myFile.close();
  37. }
  38. }
  39.  
  40. void readRecords() {
  41. ifstream myFile("users.txt");
  42. string line;
  43. if (myFile.is_open()) {
  44. num_users = 0;
  45. while (getline(myFile, line)) {
  46. user[num_users].rollNo = stoi(line);
  47.  
  48. getline(myFile, line);
  49. user[num_users].name = line;
  50.  
  51. getline(myFile, line);
  52. user[num_users].fatherName = line;
  53.  
  54. getline(myFile, line);
  55. if (line == "1") {
  56. user[num_users].gender = true;
  57. } else {
  58. user[num_users].gender = false;
  59. }
  60.  
  61. getline(myFile, line);
  62. user[num_users].cgpa = stod(line);
  63. num_users++;
  64.  
  65. getline(myFile, line);
  66. }
  67. }
  68. cout<< endl;
  69. }
  70.  
  71. void userAccount() {
  72. cout << "Enter User Details"<< endl;
  73. cout << "Roll-No : ";
  74. cin >> user[num_users].rollNo;
  75.  
  76. cout << "Name : ";
  77. cin >> user[num_users].name;
  78.  
  79. cout << "Father's Name : ";
  80. cin >> user[num_users].fatherName;
  81.  
  82. cout << "Gender : "<<endl;
  83. cout << "1. Male"<< endl;
  84. cout << "2. Female"<< endl;
  85. int genderInput;
  86. cin >> genderInput;
  87. if (genderInput == 1) {
  88. user[num_users].gender = true;
  89. } else {
  90. user[num_users].gender = false;
  91. }
  92.  
  93. cout << "CGPA : ";
  94. cin >> user[num_users].cgpa;
  95.  
  96. writeUser(user[num_users]);
  97.  
  98. num_users++;
  99. }
  100.  
  101. void displayRecords() {
  102. cout <<"--------------------Records Display---------------------" <<endl << endl;
  103.  
  104. for (int i = 0; i < num_users; i++)
  105. {
  106. cout << "Roll-No: " << user[i].rollNo << endl;
  107. cout << "Name: " << user[i].name << endl;
  108. cout << "Father's Name: " << user[i].fatherName << endl;
  109. cout << "Gender: " << user[i].gender << endl;
  110. cout << "CGPA: " << user[i].cgpa << endl;
  111. cout << endl;
  112. }
  113.  
  114. system("pause");
  115. }
  116.  
  117. void searchRecord() {
  118. int rollNo;
  119. cout << "Enter Roll-No: ";
  120. cin>> rollNo;
  121. for (int i = 0; i < num_users; i++)
  122. {
  123. if (user[i].rollNo == rollNo) {
  124. cout << "Roll-No: " << user[i].rollNo << endl;
  125. cout << "Name: " << user[i].name << endl;
  126. cout << "Father's Name: " << user[i].fatherName << endl;
  127. cout << "Gender: " << user[i].gender << endl;
  128. cout << "CGPA: " << user[i].cgpa << endl;
  129. cout << endl;
  130.  
  131. break;
  132. }
  133. }
  134.  
  135. system("pause");
  136.  
  137. }
  138.  
  139. void deleteRecord() {
  140. int rollNo;
  141. cout << "Enter Roll-No: ";
  142. cin>> rollNo;
  143.  
  144. remove("users.txt");
  145. for (int i = 0; i < num_users; i++)
  146. {
  147. if (user[i].rollNo != rollNo) {
  148. writeUser(user[i]);
  149. }
  150. }
  151. readRecords();
  152. }
  153.  
  154. void showNumberOfUsers() {
  155. cout<< "Number of records: " << num_users<< endl;
  156.  
  157. system("pause");
  158. }
  159.  
  160. void login() {
  161. string username;
  162. string password;
  163.  
  164. cout << "Login"<< endl;
  165. cout << "User Name : ";
  166. cin >> username;
  167. cout << "Password : ";
  168. cin >> password;
  169.  
  170. if (admin.username == username && admin.password == password) {
  171. cout << "Login Successful"<<endl;
  172. isLoggedIn = true;
  173. }
  174. }
  175.  
  176. int _tmain(int argc, _TCHAR* argv[])
  177. {
  178. int input;
  179.  
  180. admin.username = "admin";
  181. admin.password = "admin";
  182. //login();
  183. readRecords();
  184.  
  185. while(isLoggedIn) {
  186. system("cls");
  187. cout<<"**************Student Record Database"<<endl;
  188. cout << "1. Enter Record"<< endl;
  189. cout << "2. Display Records"<< endl;
  190. cout << "3. Search Record"<< endl;
  191. cout << "4. Delete Record"<< endl;
  192. cout << "5. Total number of students"<< endl;
  193. cout << "6. Exit"<< endl;
  194. cout << "Select An Option : ";
  195. cin >> input;
  196.  
  197. switch (input)
  198. {
  199. case 1:
  200. userAccount();
  201. break;
  202.  
  203. case 2:
  204. displayRecords();
  205. break;
  206.  
  207. case 3:
  208. searchRecord();
  209. break;
  210.  
  211. case 4:
  212. deleteRecord();
  213. break;
  214.  
  215. case 5:
  216. showNumberOfUsers();
  217. break;
  218.  
  219. case 6:
  220. isLoggedIn = false;
  221.  
  222. default:
  223. break;
  224. }
  225. }
  226.  
  227. return 0;
  228. }
Add Comment
Please, Sign In to add comment