Guest User

Untitled

a guest
Oct 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.85 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <process.h>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9.  
  10. class Login
  11. {
  12. public:
  13. char username[10], password[10];
  14.  
  15. }log1,log2;
  16.  
  17. int login()
  18. {
  19. cout << "ttt Login Panel" << endl;
  20. getchar();
  21. cout << "Enter username: ";
  22. cin.getline(log1.username,10);
  23. cout << "Enter password: ";
  24. cin.getline(log1.password,10);
  25. ifstream fin ("db", ios::binary | ios::in);
  26. if (!fin)
  27. {
  28. cout<< "Database cannot be accessed" << endl;
  29. return 0;
  30. }
  31. int i=0;
  32. while(!fin.eof())
  33. {
  34. fin.read((char*)&log2 , sizeof(Login));
  35. if (strcmp(log1.username,log2.username)==0 && strcmp(log1.password,log2.password)==0)
  36. {
  37. cout << "Logged in" << endl;
  38. i=1;
  39. break;
  40. }
  41.  
  42. }
  43. fin.close();
  44. if(!i)
  45. cout << "Wrong Credentials";
  46. return i;
  47. }
  48. void signup()
  49. {
  50. cout << "ttt Signup Panel" << endl;
  51. cout << "Enter username: ";
  52. cin.getline(log1.username,10);
  53. cout << "Enter password: ";
  54. cin.getline(log1.password,10);
  55. if( !strlen(log1.username) || !strlen(log1.password) )
  56. {
  57. cout << "Error, unsupported characters" << endl;
  58. signup();
  59. }
  60. ifstream fin ("db", ios::binary | ios::in);
  61. while(!fin.eof())
  62. {
  63. fin.read((char*)&log2 , sizeof(Login));
  64. if (!fin)
  65. break;
  66. if (strcmp(log1.username,log2.username)==0 )
  67. {
  68. cout << "User already exists!" << endl;
  69. system("PAUSE");
  70. system("CLS");
  71. signup();
  72. }
  73.  
  74. }
  75. fin.close();
  76. ofstream fout ("db", ios::binary | ios::app);
  77. fout.write((char*)&log1 , sizeof(Login));
  78. fout.close();
  79. }
  80. class stu
  81. {
  82. uint32_t rollno;
  83. char Name[20];
  84. char Class[4];
  85. float marks;
  86. char grade;
  87. char calcgrade(float t1, char t2)
  88. {
  89. if (t1 >= 75) t2='A';
  90. else if (t1 >= 60) t2 = 'B';
  91. else if (t1 >= 50) t2 = 'C';
  92. else if (t1 >= 40) t2 = 'D';
  93. else t2 = 'E' ;
  94. return t2;
  95. }
  96.  
  97. public:
  98. void getdata()
  99. {
  100. cout<<"Rollno :"; cin>>rollno;
  101. cout<<"Class :"; cin>>Class;
  102. cout<<"Name :"; getchar(); cin.getline(Name, 20);
  103. cout<<"Marks :"; cin>>marks;
  104. grade=calcgrade(marks,grade);
  105. }
  106. void putdata()
  107. {
  108. cout<< "Rollno : " << rollno << "t Name : " << Name
  109. << "n Marks : " << marks << "t Grade : " << grade << endl;
  110. }
  111. int getrno() { return rollno; }
  112. } s1, stud ;
  113.  
  114. int data_append(char* neim)
  115. {
  116. ifstream fi (neim, ios::in | ios::binary);
  117. if (!fi)
  118. {
  119. return -1;
  120. }
  121. ofstream fo ("temp.dat", ios::out | ios::binary);
  122. char last ='y';
  123. std::cout << " Enter details of student whose record is to be inserted n ";
  124. s1.getdata();
  125. while (!fi.eof())
  126. {
  127. fi.read((char*)&stud, sizeof(stu));
  128. if ( s1.getrno()<= stud.getrno())
  129. {
  130. fo.write((char*)&s1, sizeof(stu));
  131. last = 'n';
  132. break;
  133. }
  134. else
  135. fo.write((char*)&stud, sizeof(stu));
  136. }
  137. if (last == 'y')
  138. fo.write((char*)&s1, sizeof(stu));
  139. else if (!fi.eof())
  140. {
  141. while (!fi.eof())
  142. {
  143. fi.read((char*)&stud, sizeof(stu));
  144. fo.write((char*)&stud, sizeof(stu));
  145. }
  146. }
  147. fi.close(); fo.close();
  148. remove(neim);
  149. rename("temp.dat",neim);
  150. return 0;
  151. }
  152.  
  153. int data_delete(char* neim)
  154. {
  155. ifstream fi (neim, ios::in | ios::binary);
  156. if (!fi)
  157. {
  158. cout << "No such file in database" << endl;
  159. system("PAUSE");
  160. return -1;
  161.  
  162. }
  163. ofstream file ("temp.dat", ios::out | ios::binary);
  164. int rno; char found = 'f' , confirm = 'n' ;
  165. cout << " Enter rollno of student whose record is to be deleted n";
  166. cin >> rno;
  167. while (!fi.eof())
  168. {
  169. fi.read((char*)&s1,sizeof(stu));
  170. if ( s1.getrno() == rno )
  171. {
  172. s1.putdata();
  173. found = 't';
  174. cout << " Are you sure, you want to delete this record? (y/n).. ";
  175. cin>> confirm ;
  176. if (confirm == 'n')
  177. file.write((char*)&s1,sizeof(stu));
  178. }
  179. else
  180. file.write((char*)&s1,sizeof(stu));
  181. }
  182. if ( found == 'f' )
  183. cout << " Record not found ;__; n";
  184. fi.close(); file.close();
  185. remove(neim); rename("temp.dat",neim);
  186. return 0;
  187. }
  188.  
  189. int data_modify(char* neim)
  190. {
  191. fstream fio(neim,ios::in|ios::out|ios::binary);
  192. int rno ; long pos ; char found = 'f';
  193. cout << " Enter rollno of student whose record is to be modified n";
  194. getchar();
  195. cin >> rno;
  196. while ( !fio.eof())
  197. {
  198. pos = fio.tellg();
  199. fio.read((char*)&s1, sizeof(stu));
  200. if ( s1.getrno() == rno )
  201. {
  202. fio.seekg(pos);
  203. fio.write((char*)&s1, sizeof(stu));
  204. found = 't';
  205. break;
  206. }
  207. }
  208. if ( found == 'f')
  209. return -1;
  210. fio.seekg(0);
  211. cout << "Now the file contains n";
  212. while(!fio.eof())
  213. {
  214. fio.read((char*)&stud, sizeof(stu));
  215. stud.putdata();
  216. }
  217. fio.close();
  218. return 0;
  219. }
  220.  
  221. int data_search(char* neim)
  222. {
  223.  
  224. ifstream fi (neim, ios::in | ios::binary);
  225. if (!fi)
  226. {
  227. return -1;
  228.  
  229. }
  230. int rno; char found = 'f' ;
  231. cout << " Enter rollno of student whose record is to be searched n";
  232. cin >> rno;
  233. while (!fi.eof())
  234. {
  235. fi.read((char*)&s1,sizeof(stu));
  236. if ( s1.getrno() == rno )
  237. {
  238. s1.putdata();
  239. found = 't';
  240. fi.close();
  241. }
  242. }
  243. if ( found == 'f' )
  244. return -2;
  245. return 0;
  246. }
  247. int data_new(char* Class)
  248. {
  249. ifstream tmp1 (Class,ios::in);
  250. if (tmp1)
  251. {
  252. cout << "Class already exists!!!" << endl;
  253. tmp1.close();
  254. return -1;
  255. }
  256. ofstream newclass (Class,ios::out);
  257. newclass.close();
  258. return 0;
  259. }
  260. int data_remove(char* ClassDel)
  261. {
  262. ifstream tmp1 (ClassDel,ios::in);
  263. if ( !tmp1 )
  264. return -1;
  265. tmp1.close();
  266. remove(ClassDel);
  267. return 0;
  268. }
  269. int main()
  270. {
  271. loginpanel:
  272. if(!login())
  273. {
  274. system("pause");
  275. signup();
  276. }
  277. system("pause");
  278. menu:
  279. system("cls");
  280. cout << "ttt Enter the number to proceed to corresponding operation" << endl;
  281. cout << "1. Create Class" << endl
  282. << "2. Append Data" << endl
  283. << "3. Delete Data" << endl
  284. << "4. Modify Data" << endl
  285. << "5. Search Record" << endl
  286. << "6. Delete Class" << endl
  287. << "7. Logout" << endl
  288. << "8. Exit" << endl;
  289. int *op = new int;
  290. cin >> *op;
  291. switch (*op)
  292. {
  293. case 1 : { char Class[10];
  294. cout << "Enter new class name :";
  295. getchar();
  296. cin.getline(Class,10);
  297. cout << "nCreating Class files.." << endl;
  298. *op=data_new(Class);
  299. if (*op==(-1))
  300. {
  301. cout << "Class already exists!!!" << endl;
  302. break;
  303. }
  304. cout << "Class creation successful" << endl;
  305. system("pause");
  306. delete op;
  307. break;
  308. }
  309. case 2 : {
  310. system("cls");
  311. char neim[8];
  312. cout << "Enter class (use numerals only)"; //implemented for school project
  313. getchar();
  314. cin.getline(neim,8);
  315. *op=data_append(neim);
  316. if (*op==(-1))
  317. {
  318. cout << "No such file in database" << endl;
  319. break;
  320. }
  321. ifstream fi(neim, ios::in);
  322. cout << "File now contains : n";
  323. while (!fi.eof())
  324. {
  325. fi.read((char*)&stud, sizeof(stu));
  326. if (fi.eof()) break;
  327. stud.putdata();
  328. }
  329. fi.close();
  330. delete op;
  331. break;
  332. }
  333. case 3 : {
  334. cout << "Enter class"; //implemented for school project
  335. getchar();
  336. char neim1[8];
  337. cin.getline(neim1,8);
  338. *op=data_delete(neim1);
  339. if (*op==(-1))
  340. {
  341. cout << "No such file in database" << endl;
  342. system("PAUSE");
  343. }
  344. ifstream fi;
  345. fi.open(neim1, ios::in);
  346. cout << "File now contains : n";
  347. while (!fi.eof())
  348. {
  349. fi.read((char*)&stud, sizeof(stu));
  350. if (fi.eof()) break;
  351. stud.putdata();
  352. }
  353. fi.close();
  354. delete op;
  355. break;
  356. }
  357. case 4 : {
  358. cout << "Enter class (use numerals only)"; //implemented for school project
  359. char neim[8];
  360. cin.getline(neim,8);
  361. *op=data_modify(neim);
  362. if ( *op==(-1))
  363. {
  364. cout << "Record not found ;__; n";
  365. }
  366. delete op;
  367. break;
  368. }
  369. case 5 : {
  370. cout << "Enter class (use numerals only)"; //implemented for school project
  371. char neim[8];
  372. cin.getline(neim,8);
  373. *op=data_search(neim);
  374. if(*op==(-1))
  375. {
  376. cout << "No such file in database" << endl;
  377. system("PAUSE");
  378. }
  379. else if(*op==(-2))
  380. {
  381. cout << "Record not found" << endl;
  382. system("PAUSE");
  383. }
  384. delete op;
  385. break;
  386. }
  387. case 6 : {
  388. char ClassDel[10];
  389. cout << "Enter class name to be deleted:" << endl;
  390. cin.getline(ClassDel,10);
  391. cout << "Deleting Class files.." << endl;
  392. *op=data_remove(ClassDel);
  393. if(*op==(-1))
  394. {
  395. cout << "No such class as '" << ClassDel << "'" << endl;
  396. }
  397. delete op;
  398. break;
  399. }
  400. case 7 : {
  401. delete op;
  402. goto loginpanel;
  403. }
  404. case 8 : {
  405. delete op;
  406. exit(0);
  407. }
  408. default : cout << "Wrong input!";
  409. }
  410. system("pause");
  411. goto menu;
  412. return 0;
  413. }
Add Comment
Please, Sign In to add comment