Help needed with program - urgent thanks!

This is a discussion on Help needed with program - urgent thanks! within the C Programming forums, part of the General Programming Boards category; When the user is asked to input the choice and if it's out of the range, it asks again but ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    Unhappy Help needed with program - urgent thanks!

    When the user is asked to input the choice
    and if it's out of the range, it asks again
    but the second time if the user input is incorrect,
    the whole program hangs.

    Can anyone please help me?
    I need the program to keep asking for the choice until it's within the range.

    Thanks. I'm just a beginner

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void admin(void);
    void student(void);
    
    
    char loginid[10], password[10], userid[10],userpassword[10],role[10];
    
    void main() {
    
       FILE *fd;
       int found=0; // 0 means not found; 1 means found
       int attempt=0;
       char login[10];
    
       clrscr();
       printf("\n\n\n\n\n");
       printf("%20s----Welcome to the Database System!----\n\n");
       printf("%20sPlease login to enter\n\n");
    
       do{
    
         printf("Enter Username:");
         gets(userid);
         fflush(stdin);
         printf("Enter Password:");
         gets(userpassword);
    
    
        if ((fd = fopen("login.txt", "r")) == NULL){
          printf("\n\nerror>>> Could not found login.txt");
          exit(0);
    
        } // end login.txt
    
    
          do{
    
    	fscanf(fd, "%[^$]%*c%[^$]%*c%[^$]%*c\n", loginid, password,role);
    	if (strcmp (loginid,userid)==0 && strcmp (password,userpassword)==0)
    	{
    	strcpy(login,role);
    	found=1;
    	break;
    	}
    
    
          }while( !feof(fd) );
          fclose(fd);
    
    
       if (found==1)
       break;
    
       else if (attempt>1){
         printf("");
         printf("Sorry you only have three attempts\n");
         printf("Sorry, Program terminated!");
         exit(0);
        }
       else
        attempt=attempt+1;
        printf("Incorrect Username or Password!\n\n");
        printf("Please Try Again:\n\n");
    
    
        }while(attempt<=2);
    
    
    
        if (strcmp(login,"student")==0)
        student();
        if (strcmp(login,"admin")==0 )
        admin();
    
     }
    
    
    void admin(){
    
    
      FILE *fd;
      char name[10], studentid[10], grade[10], gender[10], birthday[10], address[35], contactnumber[10],  coursecode[10], coursename[15], user[10], mark[10], passorfail[10], coursecodeinput[10];
    
      int num;
      int choice;
      char returnchoice;
      char returnchoice2;
    
      do{
        clrscr();
        fflush(stdin);
        printf("%25s----Admin Menu----\n\n\n");
        printf("what would you like to do?\n");
        printf("1. Show Student's Details\n");
        printf("2. Show Examination Marks\n");
        printf("3. Exit\n");
        printf("Enter Your Choice:\n\n");
        scanf("%d", &choice);
    
      if (choice <1 || choice>3){
    
        printf("\n");
        printf("Invalid input, please try again\n");
        printf("");
        printf("\nEnter Your Choice:\n");
        printf("1. Show Student's Details\n");
        printf("2. Show Examination Marks\n");
        printf("3. Exit\n");
        scanf("%d", &choice);
       }
    
    
       }while (choice<1 && choice >4);
    
      do { //for checking input
    
      if (choice == 1){
    
    
         if ((fd = fopen("user_rec.txt", "r")) == NULL){
         printf("\n\nerror>>> Could not found user_rec.txt");
         exit(0);
         }//end user_rec.txt
    
        clrscr();
        fflush(stdin);
        printf("1. Show Student's Details\n\n");
        printf("Enter Student ID:");
        gets(userid);
        fflush(stdin);
    
    
        do{
    
          fscanf(fd, "%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c\n",name,studentid,grade,gender,birthday,address,contactnumber);
    
          if (strcmp (studentid,userid)==0){
    	clrscr();
    	printf("Name: %s\n", name);
    	printf("Student ID: %s\n", studentid);
    	printf("Class: %s\n", grade);
    	printf("Gender: %s\n", gender);
    	printf("Birthday: %s\n", birthday);
    	printf("Address: %s\n", address);
    	printf("Contact Number: %s\n", contactnumber);
    	break;
          } // end if
    
        }while( !feof(fd) );
        fclose(fd);
    
        do {
    	fflush(stdin);
    	printf("\n\n");
    	printf("Press R to exit to admin menu!\n");
    	scanf("%c", &returnchoice2);
    	}while (returnchoice2 != 'R');
    
      if (returnchoice2 == 'R'){
        admin();
      }//end if
    
      }
    
    
       else if (choice == 2){
         clrscr();
         printf("2. Student Examination Marks\n\n");
         fflush(stdin);
    
         printf("Enter the Course Code:");
         gets(coursecodeinput);
    
         if ((fd = fopen("exam_rec.txt", "r")) == NULL){
    	    printf("\n\nerror>>> Could not found login.txt");
    	    exit(0);
           }//end exam_rec.txt
    
    	   clrscr();
    	   printf("%15s%15s%15s%15s\%15s\n\n","Student ID", "Mark", "Pass\Fail");
    
    	 do{
    
    	   fscanf(fd, "%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c\n",coursecode,coursename,studentid,mark,passorfail);
    
    	   if (strcmp (coursecodeinput,coursecode)==0){
    
    	   printf("\n");
    	   printf("%15s", studentid);
    	   printf("%15s", mark);
    	   printf("%15s", passorfail);
    
    	   }
    
    	 }while( !feof(fd) );
    	 fclose(fd);
    	 break;
           }
    
      else if (choice == 3)
          exit(0);
    
    
     }while(choice<1 || choice>3);   // for checking input
    
     do {
       fflush(stdin);
       printf("\n\n");
       printf("Press R to exit to admin menu!\n");
       scanf("%c", &returnchoice);
     }while (returnchoice != 'R');
    
     if (returnchoice == 'R'){
       admin();
     }
    
    
    
    
    
    }
    
    
    
    
    
    
    void student() {
    
      FILE *fd;
      char name[10], studentid[10], grade[10], gender[10], birthday[10], address[35], contactnumber[10],  coursecode[10], coursename[15], user[10], mark[10], rank[10], passorfail[10];
    
      int num;
      int choice;
      char returnchoice;
      char returnchoice2;
    
      do{
        clrscr();
        fflush(stdin);
        printf("%25s----Student Menu----\n\n\n");
        printf("1. View Personal Information\n");
        printf("2. View Examination Marks\n");
        printf("3. Exit\n\n");
        printf("Enter Your Choice:\n");
        scanf("%d", &choice);
    
      if (choice <1 || choice>3){
        printf("\n");
        printf("Invalid input, please try again\n");
        printf("");
        printf("\nEnter Your Choice:\n");
        printf("1. View Personal Information\n");
        printf("2. View Examination Marks\n");
        printf("3. Exit\n");
        scanf("%d", &choice);
       }
    
    
       }while (choice<1 && choice >4);
    
      do { //for checking input
    
      if (choice == 1){
    
    
          if ((fd = fopen("user_rec.txt", "r")) == NULL){
          printf("\n\nerror>>> Could not found user_rec.txt");
          exit(0);
          }//end user_rec.txt
    
    
        do{
    
          fscanf(fd, "%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c\n",name,studentid,grade,gender,birthday,address,contactnumber);
    
          if (strcmp (studentid,userid)==0){
    	clrscr();
    	printf("1. View Personal Information\n\n");
    	printf("Name: %s\n", name);
    	printf("Student ID: %s\n", studentid);
    	printf("Class: %s\n", grade);
    	printf("Gender: %s\n", gender);
    	printf("Birthday: %s\n", birthday);
    	printf("Address: %s\n", address);
    	printf("Contact Number: %s\n", contactnumber);
    	break;
          } // end if
    
        }while( !feof(fd) );
        fclose(fd);
    
        do {
    	fflush(stdin);
    	printf("\n\n");
    	printf("Press R to exit to student menu!\n");
    	scanf("%c", &returnchoice2);
    	}while (returnchoice2 != 'R');
    
      if (returnchoice2 =='R'){
        student();
      }//end if
    
      }
    
    
       else if (choice == 2){
    
    
         if ((fd = fopen("exam_rec.txt", "r")) == NULL){
    	    printf("\n\nerror>>> Could not found login.txt");
    	    exit(0);
           }//end exam_rec.txt
    
    	   clrscr();
    	   printf("%25sYour Examination Marks!\n\n\n");
    	   printf("%15s%15s%15s%15s\%15s\n\n","Course Code", "Course Name", "Mark", "Pass\Fail");
    
    	 do{
    
    	   fscanf(fd, "%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c%[^$]%*c\n",coursecode,coursename,studentid,mark,passorfail);
    
    	   if (strcmp (studentid,userid)==0){
    
    	   printf("\n");
    	   printf("%15s", coursecode);
    	   printf("%15s", coursename);
    	   printf("%15s", mark);
    	   printf("%15s", passorfail);
    
    	  }
    	 }while( !feof(fd) );
    	 fclose(fd);
    	 break;
           }
    
      else if (choice == 3)
          exit(0);
    
    
    }while(choice<1 || choice>3);   // for checking input
    
     do {
       fflush(stdin);
       printf("\n\n");
       printf("Press R to exit to student menu!\n");
       scanf("%c", &returnchoice);
     }while (returnchoice != 'R');
    
     if (returnchoice == 'R'){
       student();
     }
    
    
    
     }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Code:
    scanf("&#37;d", &choice);
    I cant be bothered to go through all your code trying to figure out whats going wrong where; theres too much of it and im feeling lazy. However entering a string to a scanf that wants an integer will send the prog into an infinite loop, so if your second input was not a number the prog would hang. A safer way of getting input would be to use fgets to take a string, then convert it to an int using strtol.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help needed. Simple C++ program.
    By Sridar in forum C++ Programming
    Replies: 20
    Last Post: 01-28-2005, 06:57 PM
  2. PROGRAM MENU, urgent, please help!
    By Anfernee001 in forum C Programming
    Replies: 13
    Last Post: 09-27-2002, 04:29 AM
  3. Urgent help needed!!!
    By gibs21 in forum C Programming
    Replies: 2
    Last Post: 09-26-2002, 02:45 PM
  4. Help Needed: Borland C++ 5.5 Installation - URGENT!
    By Linette in forum C++ Programming
    Replies: 12
    Last Post: 03-09-2002, 06:44 PM
  5. Program Ideas Needed
    By pkananen in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 10:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21