-1

The program below doesn't let the user to enter the student's name. Originally, I used scanf() instead of fgets() to store the input since scanf() doesn't store spaces. (Original program here)

I have tried the solution suggested here but using scanf("%d *[^\n]", &option); in menu() doesn't seem to work for me. Could it be the ID number generator part which is causing this problem?

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAX 200
    #define ID_SIZE 20
    #define NAME_SIZE 50
    #define ADDRESS_SIZE 80
    #define TEL_SIZE 15

    int i = 0;
    int total = 0;
    int result;
    char ID[ID_SIZE];

    int menu();
    int add_ID();
    ...         

    struct Student{
        char stud_ID[ID_SIZE];
        char name[NAME_SIZE];
        char address[ADDRESS_SIZE];
        char tel_no[TEL_SIZE];
        float marks; 
    } stud [MAX];

    int main() {
        menu();
        getch();
        return 0;
    }

    int menu() {

        int option;
            printf ("1. Insert new student\n\n");
            printf ("2. Search for a particular student by ID number\n\n");
...
            printf ("Please enter your option: ");
            scanf ("%d", &option);

            while ((option < 1 ) || (option > 6)){
                printf("Please reenter your option: ");
                scanf ("%d", &option);
            }

            switch (option) {
                case 1 : add_ID();
                break;
                case 2 : search_ID();
                break;
...
                case 6 : printf("Exit\n");
                         exit(0);
                break;
                getch();
                break;
            }
        return 0;
    }

    int add_ID () {
        char next;

        printf("\n>> Add new student <<\n\n");

        do {
            sprintf(ID, "%d", i+1);
            strcpy(stud[i].stud_ID, ID);
            printf("Student ID: %s\n", stud[i].stud_ID);

            printf("Enter Name: ");
            fgets(stud[i].name, NAME_SIZE, stdin) ;

            printf("Enter address: ");
            fgets(stud[i].address, ADDRESS_SIZE, stdin) ;

            printf("Enter telephone number: ");
            fgets(stud[i].tel_no, TEL_SIZE, stdin) ;

            printf("Enter marks: "); 
            scanf("%f", &stud[i].marks);

            printf("Do you want to add another student? (y/n):\n"); 
            scanf("%s", &next);

                i++;            
                total = i;

            } while (((next == 'y') || (next == 'Y')) && (i < MAX));

            if ((next == 'n') || (next == 'N')){
                printf("Total students: %d\n\n", total);
                menu();
                getch();
            }

        return 0;
    }

    int search_ID() {
        printf("\n>> Search for student using ID <<\n\n");
        printf("Please enter the student ID: ");
        scanf("%s", ID);
        display_Header();           

        for (i = 0; i < total; i++) {
            result = strcmp(stud[i].stud_ID, ID);

            if (result == 0){
                display_Info(i);
                break;
            } 
        }
        if (i == total) {
                printf ("Not found\n");
        }

        menu();
        getch();        
        return 0;
    }
...

The following is the output of the program after option 1 is selected:

Student ID: 1
Enter name: Enter address:

The program doesn't wait for keyboard input and immediately execute the subsequent statement.

Community
  • 1
  • 1
littlepony
  • 23
  • 1
  • 6
  • use `scanf` like tis: `scanf("%[^\n]", input);` – Zoltán Mar 17 '16 at 14:10
  • @ameyCU As stated in the second paragraph the answer to that question did not work for me. – littlepony Mar 17 '16 at 15:13
  • I have tried the solution for this question [Issue in C language using 'fgets' after 'printf' as 'fgets' runs before 'printf'](http://stackoverflow.com/questions/11575102/issue-in-c-language-using-fgets-after-printf-as-fgets-runs-before-printf?rq=1) but `fflush(stdout)` did not work either. – littlepony Mar 17 '16 at 15:20

1 Answers1

0

try to use fflush(stdin); after you scanf

Elina
  • 64
  • 3