Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C++
  1  #include <stdio.h>
  2  #include <string.h>
  3  #include <stdlib.h>
  4  
  5  typedef struct Student
  6  {
  7      char studentName[50];
  8      char studentId[15];
  9      char studentDept[10];
 10      char studentAddress[100];
 11      char studentContactNum[15];
 12      float studentCGPA;
 13      float studentattendance;
 14      float studentmarks;
 15  } Student;
 16  
 17  void username_password();
 18  void displayincorrect();
 19  void editviewadmin();
 20  void studentmanagement();
 21  void createAccount();
 22  void displayInfo();
 23  void updateInfo();
 24  void deleteInfo();
 25  void searchInfo();
 26  
 27  int main()
 28  {
 29      username_password();
 30  
 31      return 0;
 32  }
 33  
 34  
 35  void username_password()
 36  
 37  {
 38      int selection,counter=0;
 39      char username[20];
 40      char password[20];
 41  
 42  
 43      do
 44      {
 45          printf("\n\t1. ADMIN\n\t2. STUDENT\n\t3. EXIT\n\n\tKINDLY MAKE YOUR SELECTION: ");
 46          scanf("%d",&selection);
 47  
 48          switch(selection)
 49          {
 50          case 1:
 51              {
 52                  printf("\n\tHELLO THERE ADMIN!\n\t");
 53  
 54                  do
 55                  {
 56                  printf("\n\tPLEASE ENTER YOUR USERNAME: ");
 57                  scanf("%s",&username);
 58  
 59                  printf("\n\tPLEASE ENTER YOUR PASSWORD: ");
 60                  scanf("%s",&password);
 61  
 62                  if(strcmp(username,"easwarangmi")==0)
 63                  {
 64                      if(strcmp(password,"easwaran2107")==0)
 65                      {
 66                          sleep(1);
 67                          system("cls");
 68                          printf("\n\tWelcome Sir Easwaran!\n\t");
 69                          sleep(1);
 70                          system("cls");
 71                          editviewadmin();
 72                      }
 73  
 74                      else
 75                      {
 76                         displayincorrect();
 77                      }
 78                  }
 79  
 80                  else if(strcmp(username,"yasmingmi")==0)
 81                  {
 82                      if(strcmp(password,"yasmin2107")==0)
 83                      {
 84                          sleep(1);
 85                          system("cls");
 86                          printf("\n\tWelcome Ms Yasmin!\n\t");
 87                          sleep(1);
 88                          system("cls");
 89                          editviewadmin();
 90                      }
 91  
 92                      else
 93                      {
 94                         displayincorrect();
 95                      }
 96                  }
 97  
 98                  else if(strcmp(username,"fatehagmi")==0)
 99                  {
100                      if(strcmp(password,"fateha2107")==0)
101                      {
102                          sleep(1);
103                          system("cls");
104                          printf("\n\tWelcome Ms Fateha!\n\t");
105                          sleep(1);
106                          system("cls");
107                          editviewadmin();
108                      }
109  
110                      else
111                      {
112                         displayincorrect();
113                      }
114                  }
115  
116                  else if(strcmp(username,"lindagmi")==0)
117                  {
118                      if(strcmp(password,"linda2107")==0)
119                      {
120                          sleep(1);
121                          system("cls");
122                          printf("\n\tWelcome Ms Linda!\n\t");
123                          sleep(1);
124                          system("cls");
125                          editviewadmin();
126                      }
127  
128                      else
129                      {
130                         displayincorrect();
131                      }
132                  }
133  
134                  else
135                  {
136                      displayincorrect();
137                  }
138  
139                  ++counter;
140                  }
141  
142                  while(counter<5);
143                  {
144                      system("cls");
145                      printf("\n\tFORGOT YOUR USERNAME OR PASSWORD? PLEASE MAKE A REPORT!\n\t");
146                      return 0;
147                  }
148              }
149  
150          case 2:
151              {
152                  printf("\n\tHELLO THERE STUDENT!\n\t");
153  
154                  do
155                  {
156                  printf("\n\tPLEASE ENTER YOUR USERNAME: ");
157                  scanf("%s",&username);
158  
159                  printf("\n\tPLEASE ENTER YOUR PASSWORD: ");
160                  scanf("%s",&password);
161  
162                  if(strcmp(username,"aniqgmi")==0)
163                  {
164                      if(strcmp(password,"aniq2107")==0)
165                      {
166                          sleep(1);
167                          system("cls");
168                          printf("\n\tWelcome Aniq Idris!\n\t");
169                          sleep(1);
170                          system("cls");
171  
172                      }
173  
174                      else
175                      {
176                         displayincorrect();
177                      }
178                  }
179  
180                  else if(strcmp(username,"fahmigmi")==0)
181                  {
182                      if(strcmp(password,"fahmi2107")==0)
183                      {
184                          sleep(1);
185                          system("cls");
186                          printf("\n\tWelcome Aniq Fahmi!\n\t");
187                          sleep(1);
188                          system("cls");
189  
190                      }
191  
192                      else
193                      {
194                         displayincorrect();
195                      }
196                  }
197  
198                  else if(strcmp(username,"irfangmi")==0)
199                  {
200                      if(strcmp(password,"irfan2107")==0)
201                      {
202                          sleep(1);
203                          system("cls");
204                          printf("\n\tWelcome Ahmad Irfan!\n\t");
205                          sleep(1);
206                          system("cls");
207  
208                      }
209  
210                      else
211                      {
212                         displayincorrect();
213                      }
214                  }
215  
216                  else if(strcmp(username,"haziqgmi")==0)
217                  {
218                      if(strcmp(password,"haziq2107")==0)
219                      {
220                          sleep(1);
221                          system("cls");
222                          printf("\n\tWelcome Haziq Sani!\n\t");
223                          sleep(1);
224                          system("cls");
225  
226                      }
227  
228                      else
229                      {
230                         displayincorrect();
231                      }
232                  }
233  
234                  if(strcmp(username,"zafrangmi")==0)
235                  {
236                      if(strcmp(password,"zafran2107")==0)
237                      {
238                          sleep(1);
239                          system("cls");
240                          printf("\n\tWelcome Zafran Iman!\n\t");
241                          sleep(1);
242                          system("cls");
243  
244                      }
245  
246                      else
247                      {
248                         displayincorrect();
249                      }
250                  }
251                  else
252                  {
253                      displayincorrect();
254                  }
255                  ++counter;
256                  }
257                  while(counter<5);
258                  {
259                      system("cls");
260                      printf("\n\tFORGOT YOUR USERNAME OR PASSWORD? PLEASE MAKE A REPORT!\n\t");
261                      return 0;
262                  }
263              }
264  
265          case 3:
266              {
267                  printf("\n\tALRIGHT, GOODBYE AND TAKE CARE!\n\t");
268                  return 0;
269              }
270  
271          default:
272              {
273                  sleep(1);
274                  system("cls");
275                  printf("WRONG ENTRY!");
276                  sleep(1);
277                  system("cls");
278              }
279          }
280      }
281      while (selection==0||selection>3);
282  }
283  
284  void editviewadmin()
285  {
286      int selectionadmin;
287  
288      do
289      {
290      printf("\n\t1. EDIT\n\t2. VIEW\n\t3. EXIT\n\n\tKINDLY MAKE YOUR SELECTION: ");
291      scanf("%d",&selectionadmin);
292  
293      switch(selectionadmin)
294      {
295      case 1:
296          {
297              studentmanagement();
298          }
299  
300      case 2:
301          {
302              displayInfo();
303          }
304  
305      case 3:
306          {
307              sleep(1);
308              system("cls");
309              printf("\n\tPLEASE CHOOSE ONE OF THE FOLLOWING AGAIN");
310              username_password();
311              return 0;
312          }
313  
314  
315      default:
316          {
317              sleep(1);
318              system("cls");
319              printf("WRONG ENTRY!");
320              sleep(1);
321              system("cls");
322          }
323      }
324      }
325      while(selectionadmin==0||selectionadmin>3);
326  }
327  
328  void displayincorrect()
329  {
330      sleep(1);
331      system("cls");
332      printf("\n\tWRONG USERNAME OR INCORRECT PASSOWRD!\n\t");
333      sleep(1);
334      system("cls");
335      printf("\n\tPLEASE TRY AGAIN!\n\t");
336      sleep(1);
337      system("cls");
338  }
339  
340  
341  
342  void studentmanagement()
343  {
344      char option;
345  
346      while (option != '0')
347      {
348          system("cls");
349          printf("\t\t====== Student Management Database System ======\n");
350          printf("\n\t\t\t1. Create Student Account");
351          printf("\n\t\t\t2. Display All Students Information");
352          printf("\n\t\t\t3. Update Student Information");
353          printf("\n\t\t\t4. Delete Student Information");
354          printf("\n\t\t\t5. Search Student Information");
355          printf("\n\t\t\t0. Exit");
356  
357          printf("\n\n\n\t\t\tEnter Your Option: ");
358          scanf(" %c", &option);
359  
360          switch (option)
361          {
362          case '1':
363              createAccount();
364              break;
365          case '2':
366              displayInfo();
367              break;
368          case '3':
369              updateInfo();
370              break;
371          case '4':
372              deleteInfo();
373              break;
374          case '5':
375              searchInfo();
376              break;
377          case '0':
378              printf("\n\t\t\t====== Thank You ======");
379              break;
380          default:
381              printf("\n\t\t\tInvalid Option, Please Enter Right Option !\n");
382          }
383      }
384  }
385  
386  void createAccount()
387  {
388      FILE *fileOne = fopen("studentInfo.bin", "ab+");
389  
390      if (fileOne == NULL)
391      {
392          printf("\n\t\t\tError !\n");
393      }
394  
395      Student stundentInformation;
396  
397      system("cls");
398  
399      printf("\t\t\t====== Create Student Account ======\n");
400  
401      printf("\n\t\t\tEnter Student's Name : ");
402      getchar();
403      gets(stundentInformation.studentName);
404      printf("\t\t\tEnter Student's ID : ");
405      gets(stundentInformation.studentId);
406      printf("\t\t\tEnter Student's Department : ");
407      gets(stundentInformation.studentDept);
408      printf("\t\t\tEnter Student's Address : ");
409      gets(stundentInformation.studentAddress);
410      printf("\t\t\tEnter Student's Contact Number : ");
411      gets(stundentInformation.studentContactNum);
412      printf("\t\t\tEnter Student's Current CGPA : ");
413      scanf("%f", &stundentInformation.studentCGPA);
414  
415      fwrite(&stundentInformation, sizeof(stundentInformation), 1, fileOne);
416  
417      printf("\n\n\t\t\tInformations have been stored successfully\n");
418      printf("\n\n\t\t\tEnter any keys to continue.......");
419      getch();
420  
421      fclose(fileOne);
422  }
423  
424  void displayInfo()
425  {
426      FILE *fileOne = fopen("studentInfo.bin", "rb");
427  
428      if (fileOne == NULL)
429      {
430          printf("\n\t\t\tError !\n");
431      }
432  
433      Student stundentInformation;
434  
435      system("cls");
436  
437      printf("\t\t\t\t====== All Students Information ======\n");
438  
439      printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-s\n", "Name", "ID", "Dept.", "Address", "Contact", "CGPA");
440      printf("\t\t----------------------------------------------------------------------------------------");
441  
442      while (fread(&stundentInformation, sizeof(stundentInformation), 1, fileOne) == 1)
443      {
444          printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-.2f", stundentInformation.studentName, stundentInformation.studentId, stundentInformation.studentDept, stundentInformation.studentAddress, stundentInformation.studentContactNum, stundentInformation.studentCGPA);
445      }
446  
447      printf("\n\n\t\tEnter any keys to continue.......");
448      getch();
449  
450      fclose(fileOne);
451  }
452  
453  void updateInfo()
454  {
455      FILE *fileOne = fopen("studentInfo.bin", "rb");
456      FILE *temp = fopen("temp.bin", "wb");
457  
458      Student studentInformation, tempInformation;
459  
460      int choice, flag = 0;
461  
462      if (fileOne == NULL || temp == NULL)
463      {
464          printf("\n\t\t\tError !\n");
465      }
466  
467      system("cls");
468  
469      printf("\t\t\t\t====== Update Students Information ======\n");
470  
471      printf("\n\t\t\tEnter Student's ID : ");
472      getchar();
473      gets(tempInformation.studentId);
474  
475      while (fread(&studentInformation, sizeof(studentInformation), 1, fileOne) == 1)
476      {
477          if (strcmp(studentInformation.studentId, tempInformation.studentId) == 0)
478          {
479              flag++;
480              printf("\n\t\t\tChoose your option :\n\t\t\t1. Update Student Name\n\t\t\t2. Update Student Dept.\n\t\t\t3. Update Student Address\n\t\t\t4. Update Student Contact No.\n\t\t\t5. Update Student CPGA");
481              printf("\n\n\t\t\tEnter Your Option : ");
482              scanf("%d", &choice);
483              if (choice == 1)
484              {
485                  printf("\n\t\t\tEnter Student's Name to Update: ");
486                  getchar();
487                  gets(tempInformation.studentName);
488                  strcpy(studentInformation.studentName, tempInformation.studentName);
489  
490                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
491                  printf("\n\n\t\t\tUpdated successfully!\n\n");
492              }
493              else if (choice == 2)
494              {
495                  printf("\n\t\t\tEnter Student's Department to Update: ");
496                  getchar();
497                  gets(tempInformation.studentDept);
498                  strcpy(studentInformation.studentDept, tempInformation.studentDept);
499  
500                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
501                  printf("\n\n\t\t\tUpdated successfully!\n\n");
502              }
503              else if (choice == 3)
504              {
505                  printf("\n\t\t\tEnter Student's Address to Update: ");
506                  getchar();
507                  gets(tempInformation.studentAddress);
508                  strcpy(studentInformation.studentAddress, tempInformation.studentAddress);
509  
510                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
511                  printf("\n\n\t\t\tUpdated successfully!\n\n");
512              }
513              else if (choice == 4)
514              {
515                  printf("\n\t\t\tEnter Student's Contact Number to Update: ");
516                  getchar();
517                  gets(tempInformation.studentContactNum);
518                  strcpy(studentInformation.studentContactNum, tempInformation.studentContactNum);
519  
520                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
521                  printf("\n\n\t\t\tUpdated successfully!\n\n");
522              }
523              else if (choice == 5)
524              {
525                  printf("\n\t\t\tEnter Student's CGPA to Update: ");
526                  scanf("%f", &tempInformation.studentCGPA);
527                  studentInformation.studentCGPA = tempInformation.studentCGPA;
528  
529                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
530                  printf("\n\n\t\t\tUpdated successfully!\n\n");
531              }
532              else
533              {
534                  printf("\n\t\t\tInvalid Option.");
535                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
536              }
537          }
538          else
539          {
540              fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
541          }
542      }
543  
544      fclose(fileOne);
545      fclose(temp);
546  
547      remove("studentInfo.bin");
548      rename("temp.bin", "studentInfo.bin");
549  
550      if (flag == 0)
551      {
552          printf("\n\t\t\tStudent Id is not found");
553      }
554  
555      printf("\n\n\t\t\tEnter any keys to continue.......");
556      getch();
557  }
558  
559  void deleteInfo()
560  {
561      FILE *fileOne = fopen("studentInfo.bin", "rb");
562      FILE *temp = fopen("temp.bin", "wb");
563  
564      Student studentInformation, tempInformation;
565  
566      int choice, flag = 0;
567  
568      if (fileOne == NULL || temp == NULL)
569      {
570          printf("\n\t\t\tError !\n");
571      }
572  
573      system("cls");
574  
575      printf("\t\t\t\t====== Delete Student Information ======\n");
576  
577      printf("\n\t\t\tEnter Student's ID : ");
578      getchar();
579      gets(tempInformation.studentId);
580  
581      while (fread(&studentInformation, sizeof(studentInformation), 1, fileOne) == 1)
582      {
583          if (strcmp(studentInformation.studentId, tempInformation.studentId) == 0)
584          {
585              flag++;
586              printf("\n\t\t\tAre you sure to delete ??\n\t\t\t\t1. Yes\n\t\t\t\t2. Back\n\t\t\t\tEnter Your Option : ");
587              scanf("%d", &choice);
588              if (choice == 1)
589              {
590                  printf("\n\n\t\t\tInformation has been deleted successfully!\n\n");
591              }
592              else if (choice == 2)
593              {
594                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
595              }
596              else
597              {
598                  printf("\n\t\t\tInvalid Option");
599                  fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
600              }
601          }
602          else
603          {
604              fwrite(&studentInformation, sizeof(studentInformation), 1, temp);
605          }
606      }
607  
608      fclose(fileOne);
609      fclose(temp);
610  
611      remove("studentInfo.bin");
612      rename("temp.bin", "studentInfo.bin");
613  
614      if (flag == 0)
615      {
616          printf("\n\t\t\tStudent Id is not found");
617      }
618  
619      printf("\n\n\t\t\tEnter any keys to continue.......");
620      getch();
621  }
622  
623  void searchInfo()
624  {
625      FILE *fileOne = fopen("studentInfo.bin", "rb");
626  
627      Student studentInformation;
628  
629      int choice, flag = 0;
630      char studentID[20], studentDept[10];
631  
632      if (fileOne == NULL)
633      {
634          printf("\n\t\t\tError !\n");
635      }
636  
637      system("cls");
638  
639      printf("\t\t\t\t====== Search Student Information ======\n");
640  
641      printf("\n\t\t\tChoose your option :\n\t\t\t1. Search by Student ID\n\t\t\t2. Search by Student Department");
642      printf("\n\n\t\t\tEnter Your Option : ");
643      scanf("%d", &choice);
644  
645      if (choice == 1)
646      {
647          system("cls");
648          printf("\t\t\t\t====== Search Student Information ======\n");
649          printf("\n\n\t\t\tEnter Student ID : ");
650          getchar();
651          gets(studentID);
652          while (fread(&studentInformation, sizeof(studentInformation), 1, fileOne) == 1)
653          {
654              if (strcmp(studentInformation.studentId, studentID) == 0)
655              {
656                  flag++;
657                  printf("\n\t\t\tStudent Name : %s\n\t\t\tStudent ID : %s\n\t\t\tStudent Department : %s\n\t\t\tStudent Address : %s\n\t\t\tStudent Contact No. : %s\n\t\t\tStudent CGPA : %.2f\n", studentInformation.studentName, studentInformation.studentId, studentInformation.studentDept, studentInformation.studentAddress, studentInformation.studentContactNum, studentInformation.studentCGPA);
658              }
659          }
660          if (flag == 0)
661          {
662              printf("\n\t\t\tStudent ID is not found");
663          }
664      }
665      else if (choice == 2)
666      {
667          system("cls");
668          printf("\t\t\t\t====== Search Student Information ======\n");
669          printf("\n\n\t\t\tEnter Student Department : ");
670          getchar();
671          gets(studentDept);
672          printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-s\n", "Name", "ID", "Dept.", "Address", "Contact", "CGPA");
673          printf("\t\t----------------------------------------------------------------------------------------");
674          while (fread(&studentInformation, sizeof(studentInformation), 1, fileOne) == 1)
675          {
676              if (stricmp(studentInformation.studentDept, studentDept) == 0)
677              {
678                  flag++;
679                  printf("\n\n\t\t%-20s%-13s%-10s%-25s%-15s%-.2f", studentInformation.studentName, studentInformation.studentId, studentInformation.studentDept, studentInformation.studentAddress, studentInformation.studentContactNum, studentInformation.studentCGPA);
680              }
681          }
682          if (flag == 0)
683          {
684              printf("\n\t\t\tStudent Id is not found");
685          }
686      }
687      else
688      {
689          printf("\n\t\t\tInvalid Option");
690      }
691  
692      fclose(fileOne);
693  
694      printf("\n\n\n\t\t\tEnter any keys to continue.......");
695      getch();
696  }


What I have tried:

basically my code is to create a 'program' for student management
but it won't end at line 311.
help
Posted
Updated 17-Nov-21 22:17pm
v2
Comments
[no name] 17-Nov-21 13:46pm    
Guess what? We don't see any "line numbers"? Or stars, depending on the time of day. etc.
Rick York 17-Nov-21 22:27pm    
I have suggested that line numbers be added a few times but there seems to be little interest. Almost other code-oriented site I visit has it but this one does not.
Richard Deeming 18-Nov-21 4:17am    
You can already add line numbers to a code block; they're just not added by default, since they probably wouldn't match up with the real line numbers.
<pre lang="..." linecount="true">

<pre lang="..." linecount="true" countstart="10" countincrement="10">
0x01AA 17-Nov-21 13:53pm    
void editviewadmin() means the method does not return a value. So simply replace return 0; by return; in line 311 ;)

Take a look at your code:
C
void username_password()
{
   /* lots of code omitted */
   edit_viewadmin();
   /* lots more code omitted */
}

void edit_viewadmin()
{
   /* lots of code omitted */
   case 3:
   {
        sleep(1);
        system("cls");
         printf("\n\tPLEASE CHOOSE ONE OF THE FOLLOWING AGAIN");
         username_password();
         return 0;   /* <== this is your line 311 */
    }
    /* lots more code omitted */
}
So we can see that before we get to the return on line 311, we make a call to username_password() again, which creates a recursive loop. so after a few iterations we get something like this
main
  call to username_passsword
      call to edit_viewadmin
           call to username_password
                call to edit_viewadmin
                     call to username_password 
As you can see, you are creating a never ending nest of mutually recursive calls. Maybe what I would do is modify the functions to return a status code, and then decide what do in the calling function For example
C
#define COMPLETE 1

int main()
{
    int status = 0;
    do {
        status = username_password();
    } while ( status != COMPLETE );
}
. Similarly for the other functions you have.
Also, turn up the warnings on your compilation. You should be getting warnings at least about functions declared as returning void where you try to return 0. There are probably other things that the compiler will tell you are issues that need to be addressed.
 
Share this answer
 
Sorry, compilers are rarely wrong. Here you are the culprit:
Quote:
void editviewadmin()
{
int selectionadmin;

do
{
printf("\n\t1. EDIT\n\t2. VIEW\n\t3. EXIT\n\n\tKINDLY MAKE YOUR SELECTION: ");
scanf("%d",&selectionadmin);

switch(selectionadmin)
{
case 1:
{
studentmanagement();
}

case 2:
{
displayInfo();
}

case 3:
{
sleep(1);
system("cls");
printf("\n\tPLEASE CHOOSE ONE OF THE FOLLOWING AGAIN");
username_password();
return 0; //<================== the function is declared 'void'
//...

replacing return 0; with return; should make the compiler happy.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900