Gets () doesn't work

I have a program written in C and it calls gets () from the switch when the user selects option 3. Here is my code. He didn't seem to wait for the user to enter anything. Rather, the program continues in the switch.

void getField();

#include <stdio.h>
#include <string.h>
/*#include "stubs.c"
#include "record.h" */

int debugMode;

void getField(){
    char name[25];
    char address[80];
    int yearofbirth;
    char telno[15];
    int counter = 0;

    if(debugMode == 1){
        printf("***DEBUG*** Entering getField function \n");
    }

    printf("Enter your name:");
    gets(name);

    printf("Name: %s \n", name);
    printf("\n");
}

void main(int argc, char * argv[])
{
    struct record* start = NULL;
    int userChoice;
    debugMode = 0;

    if(argv[1] != NULL){
        if( (strcmp(argv[1], "debug") == 0) && (argv[2] == NULL) ){
            debugMode = 1;
            printf("Welcome, to the personal address book application \n");
        }
        else{
            int i = 0;
            while(argv[i] != NULL){
                printf(argv[i]);
                printf(" ");
                i++;
            }
            printf(": Command not found \n");
            userChoice = 6;
        }
    }

    if(argv[1] == NULL){
        printf("Welcome, to the personal address book application \n");
        userChoice = 0;
    }


    while(userChoice != 6)
    {
        if(debugMode == 1){
            printf("***DEBUG*** Entering do-while loop \n");
        }

        printf("Enter number corresponding number to option below \n\n");   

        printf("1) Add a new record in the database \n");
        printf("2) Modify a record in the database \n");
        printf("3) Print information about a record in the database \n");
        printf("4) Print all information in the database \n");
        printf("5) Delete an existing record from the database \n");
        printf("6) Quit program \n\n >");


        scanf("%d", &userChoice);

        switch(userChoice){

            case 1:
                /*addRecord(start, arrayHolder, arrayHolder, 0, arrayHolder);
                */userChoice = 0;
                break;
            case 2:
                /*modifyRecord(start, arrayHolder, arrayHolder, arrayHolder);
                */userChoice = 0;
                break;
            case 3:
                /*printRecord(start, arrayHolder);
                */userChoice = 0;
                getField();
                break;
            case 4:
                /*printAllRecords(start);
                */userChoice = 0;
                break;
            case 5:
                /*deleteRecord(start, arrayHolder);
                */userChoice = 0;
                break;
            case 6:
                printf("case 6 \n");
                break;
            default:
                printf("default \n");
                userChoice = 0;
                break;
        }

    }
    printf("\n");
}

      

+2


source to share


3 answers


When you enter an option with a call scanf()

, you type 2 keys on the keyboard, for example 3 and ENTER.
scanf()

consumes "3" but leaves ENTER dangling in the input buffer.
When you later do gets()

that ENTER is still in the input buffer and that's what it gets gets()

.

You have two options:

  • flush the input buffer after each scanf()

  • clear the input buffer before each gets()



To clear the input buffer use this code:

int clear_input_buffer(void) {
    int ch;
    while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
    return ch;
}

      

ABOUT! And stop usinggets()

. gets()

not safe to use. Usefgets()

instead.

+9


source


When you read the numbers using the scanf ( "% d", ....) , a new line that you enter after the number is still there, waiting in the input buffer, when your program gets the value later received . The line that reads will be very short, consisting of just that line.

Don't use fflush (stdin) as it is not guaranteed by the standard to work. Instead, you can just read characters in a loop until you skip a new line:



while (getchar() != '\n')
    ;

      

There are some other problems with your code, among which you really shouldn't use get at all, since it doesn't check if the line you are reading actually fits into a variable. Use fgets instead .

+2


source


Add the line "\ n" to the scanf line! gets the empty string read and CR after your choice.

    scanf("%d\n", &userChoice);

      

And in GetField (), after printf, say "fflush":

void getField(){
    char name[25];
    char address[80];
    int yearofbirth;
    char telno[15];
    int counter = 0;

    if(debugMode == 1){
        printf("***DEBUG*** Entering getField function \n");
    }

    printf("Enter your name:");
    fflush(stdout);
    gets(name);

    printf("Name: %s \n", name);
    fflush(stdout);
    printf("\n");
}

      

-1


source







All Articles