tags:

views:

237

answers:

4

I have a program written in C and it calls gets() from a switch when a user chooses the option of 3. Here is my code. It does not seem to wait to wait for the user to input something. 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");
}
A: 

hi phil, can you please post your o/p ?

AnandBabu
What? (No, I didn't downvote, I just don't understand this at all.)
Thomas Padron-McCarthy
@AnandBabu: such question belongs to comments, not to answers which should only give... answers!You are not in yet another forum... :-)
PhiLho
A: 

Add a "\n" to the scanf line! gets reads the empty string and the CR after your choose.

    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");
}
Denes Tarjan
+7  A: 

When you input an option with the scanf() call, you type 2 keys on your keyboard, for example, 3 and ENTER.
The scanf() consumes the '3' but leaves the ENTER hanging in the input buffer.
When, later, you do gets() that ENTER is still in the input buffer and that's what gets() gets.

You have two options:

  • clear 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;
}

Oh! And stop using gets(). gets() is impossible to use safely. Use fgets() instead.

pmg
+1 for warning about `gets`. So dangerous that the C standard actually deprecated it. (They could stand to deprecate a few more, however...)
Andrew Keeton
+3  A: 

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

Don't use fflush(stdin), since that is not guaranteed by the standard to work. Instead you can just read characters in a loop until you've skipped the newline:

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

There are also some other problems with your code, among them that you really shouldn't use gets at all, since it doesn't check that the line you read actually fits in the variable. Use fgets instead.

Thomas Padron-McCarthy