Advertisement
Guest User

Untitled

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