Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. //This is my code:
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. /* Constants used in the application */
  7. const char * PASSWORD_FILE = "passwords.txt";
  8. const int MAX_STUDENTS = 1000;
  9. const int MAX_GRADES = 100;
  10. const int MAX_CHARS = 25;
  11.  
  12. //Function to print Author Info
  13. void printAuthorInfo();
  14.  
  15. //Function to display initial Menu and Get the User's Selection
  16. int showMainMenu();
  17.  
  18. //Function to Login to the System
  19. int login();
  20.  
  21. //member menu
  22. int memberMenu();
  23.  
  24. //Enter GPA for current Student
  25. void enterGPA(char ids[1000][25], float grades[1000][100], int[], int);
  26.  
  27. //Display student records to stdout
  28. void print(char[1000][25], char[1000][25], char[1000][25], float[1000][100],
  29. int[], int);
  30.  
  31. //Save Records to File
  32. void save(char[1000][25], char[1000][25], char[1000][25], float[1000][100],
  33. int[], int);
  34.  
  35. //Load Records from File
  36. void load(char fn[1000][25], char ln[1000][25], char ids[1000][25], float
  37. grades[1000][100], int counts[], int num);
  38.  
  39. int main()
  40. {
  41.  
  42.  
  43. int loggedIn = 0;
  44. int choice, subChoice;
  45. char firstNames[1000][25];
  46. char lastNames[1000][25];
  47. char ids[1000][25];
  48. float grades[1000][100];
  49. int numGrades[1000];
  50. char fn[25];
  51. char ln[25];
  52. int numStudents = 0;
  53.  
  54. //Top Level Loop
  55. do {
  56. //display menu and get the user's selection
  57. choice = showMainMenu();
  58.  
  59. if (choice == 0)
  60. {
  61. printf("Thank you for using our Application! GoodBy!");
  62. }
  63. else if (choice == 1)
  64. {
  65. printAuthorInfo();
  66. }
  67. else if (choice == 2)
  68. {
  69. loggedIn = login();
  70.  
  71. if (loggedIn == 1)
  72. {
  73. printf("Welcome! You are now Logged Inn");
  74.  
  75. //Interact with the user
  76. do
  77. {
  78. subChoice = memberMenu();
  79.  
  80. if (subChoice == 1)
  81. {
  82. printAuthorInfo();
  83. }
  84. else if (subChoice == 2)
  85. {
  86. printf("Enter First Name: ");
  87. scanf("%s", firstNames[numStudents]);
  88. printf("Enter Last Name: ");
  89. scanf("%s", lastNames[numStudents]);
  90. printf("Enter Student ID: ");
  91. scanf("%s", ids[numStudents]);
  92. numStudents++;
  93. printf("Student Has been added to System");
  94. }
  95. else if (subChoice == 3)
  96. {
  97. enterGPA(ids, grades, numGrades, numStudents);
  98. }
  99. else if (subChoice == 4)
  100. {
  101. print(firstNames, lastNames, ids, grades, numGrades,
  102. numStudents);
  103.  
  104. }
  105. else if (subChoice == 5)
  106. {
  107. void save(firstNames, lastNames, ids, grades, counts,
  108. numStudents);
  109. }
  110. else if (subChoice == 6)
  111. {
  112. load(firstNames, lastNames, ids, grades, numGrades,
  113. &numStudents);
  114. }
  115. else if (subChoice == 7)
  116. {
  117. printf("nYou are logged Out Now");
  118. }
  119. printf("n");
  120. } while (subChoice != 7);
  121. }
  122. else
  123. {
  124. printf("Login Failed - Try again");
  125. }
  126. }
  127. printf("nn");
  128. } while (choice != 0);
  129.  
  130.  
  131. system("pause");
  132. return 0;
  133. }
  134.  
  135. void printAuthorInfo()
  136. {
  137. printf("Author Informationn");
  138. printf("Author Name: %sn", "myinfo ");
  139. printf("Student ID: %sn", "12345");
  140. }
  141.  
  142. int showMainMenu()
  143. {
  144. //User Selection
  145. int choice;
  146.  
  147. do
  148. {
  149. //Display menu
  150. printf("Press 0 to exitn");
  151. printf("Press 1 for Author Infon");
  152. printf("Press 2 for Loginn");
  153. printf("Enter choice? ");
  154.  
  155. //Read the selection
  156. scanf("%d", &choice);
  157. printf("n");
  158. } while (choice < 0 || choice > 2);
  159.  
  160. //Return selection
  161. return choice;
  162. }
  163.  
  164. int login()
  165. {
  166. //Get User's username and password
  167. char username[30];
  168. char password[30];
  169. char usernameInFile[30];
  170. char passwordInFile[30];
  171. int valid = 0;
  172.  
  173. //Prompt and get Username
  174. printf("Enter Username: ");
  175. scanf("%s", username);
  176.  
  177. //Prompt and get Password
  178. printf("Enter Password: ");
  179. scanf("%s", password);
  180.  
  181. //Open the input file
  182. FILE * fptr = fopen(PASSWORD_FILE, "r");
  183.  
  184. while (fscanf(fptr, "%s %s", usernameInFile, passwordInFile) == 2)
  185. {
  186. if (strcmp(username, usernameInFile) == 0 && strcmp(password,
  187. passwordInFile) == 0)
  188. {
  189. valid = 1;
  190. }
  191. }
  192.  
  193. fclose(fptr);
  194. return valid;
  195. }
  196.  
  197.  
  198. int memberMenu()
  199. {
  200. int choice = 0;
  201.  
  202. do
  203. {
  204. printf("Press 1 for author infon");
  205. printf("Press 2 to Enter new studentn");
  206. printf("Press 3 to enter grade for existing studentn");
  207. printf("Press 4 to print student recordsn");
  208. printf("Press 5 to save student recordsn");
  209. printf("Press 6 to load student recordsn");
  210. printf("Press 7 to logoutnEnter Selection? ");
  211. scanf("%d", &choice);
  212. printf("n");
  213.  
  214. } while (choice < 1 || choice > 7);
  215.  
  216. return choice;
  217. }
  218.  
  219. void print(char fn[1000][25], char ln[1000][25], char ids[1000][25], float
  220. grades[1000][100], int counts[], int num)
  221. {
  222. int i, j;
  223. float sum, avg;
  224. sum = 0;
  225. avg = 0;
  226.  
  227. printf("%-15s %-15s %-10s %-8s %-8sn", "First Name",
  228. "Last Name", "Std ID", "GPA", "Grades");
  229.  
  230. for (i = 0; i < num; i++)
  231. {
  232. printf("%-15s %-15s %-10s", fn[i], ln[i], ids[i]);
  233.  
  234. for (j = 0; j < counts[i]; j++)
  235. {
  236. sum += grades[i][j];
  237. }
  238.  
  239. avg = sum / counts[i];
  240. sum = 0;
  241. printf("%-8.2f", avg);
  242. for (j = 0; j < counts[i]; j++)
  243. {
  244. printf("%-6.2f", grades[i]);
  245. }
  246. printf("n");
  247. }
  248. }
  249.  
  250. void save(char fn[1000][25], char ln[1000][25], char ids[1000][25], float
  251. grades[1000][100], int counts[], int num)
  252. {
  253. int i, j;
  254. float sum, avg;
  255. char fname[100];
  256. sum = 0;
  257. avg = 0;
  258.  
  259. //Get file name
  260. printf("Enter Output File Name: ");
  261. scanf("%s", fname);
  262.  
  263. FILE * fptr = fopen(fname, "w");
  264.  
  265. fprintf(fptr, "%dn", num);
  266. for (i = 0; i < num; i++)
  267. {
  268. fprintf(fptr, "%s %s %s %d ", fn[i], ln[i], ids[i], counts[i]);
  269.  
  270. for (j = 0; j < counts[i]; j++)
  271. {
  272. fprintf(fptr, "%.2f ", grades[i]);
  273. }
  274. fprintf(fptr, "n");
  275. }
  276.  
  277. fclose(fptr);
  278. }
  279.  
  280. void load(char fn[1000][25], char ln[1000][25], char ids[1000][25], float
  281. grades[1000][100], int counts[], int * num)
  282. {
  283. char fname[100];
  284. int cur = *num;
  285. int i, j, n, numStudents = 0;
  286. float grade;
  287.  
  288. //prompt and get input file
  289. printf("Enter Input File Name: ");
  290. scanf("%s", fname);
  291.  
  292. //open file to read
  293. FILE * fptr = fopen(fname, "r");
  294. fscanf(fptr, "%d", &numStudents);
  295. for (i = 0; i < numStudents && cur < 1000; i++)
  296. {
  297. fscanf(fptr, "%s %s %s %d", fn[cur], ln[cur], ids[cur], &n);
  298. for (j = 0; j < n && counts[i] < 100; j++)
  299. {
  300. fscanf(fptr, "%f", &grade);
  301. grades[i][counts[i]] = grade;
  302. counts[i] += 1;
  303. }
  304. cur += 1;
  305. }
  306.  
  307. *num = cur;
  308. fclose(fptr);
  309.  
  310. }
  311.  
  312. void enterGPA(char ids[1000][25], float grades[1000][100], int counts[], int
  313. num)
  314. {
  315. char id[25];
  316. int i, idx;
  317. float gpa;
  318.  
  319. idx = -1;
  320.  
  321. printf("Enter Student ID: ");
  322. scanf("%s", id);
  323.  
  324. //find student
  325. for (i = 0; i < num && idx == -1; i++)
  326. {
  327. if (strcmp(id, ids[i]) == 0)
  328. {
  329. idx = i;
  330. }
  331. }
  332.  
  333. if (idx >= 0)
  334. {
  335. printf("Enter Grade: ");
  336. scanf("%f", &gpa);
  337. grades[idx][counts[idx]] = gpa;
  338. counts[idx] += 1;
  339. }
  340. else
  341. {
  342. printf("NO Such Student Exist In The System");
  343. }
  344.  
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement