0

whenever I call this function it fails to give the use a chance to enter the string. Not sure why pretty sure my syntax is spot on. think I has something to do with the newline but I don't know how to get rid of it

 void serchName(dealers_t *ptr, int numDealers)
{
    char dealerName[NAME_LEN];
    int index;

    printf("please enter the dealer's name:");
    fgets(dealerName, sizeof(dealerName), stdin);
    system("PAUSE");

    for (index = 0; index < numDealers; index++, ptr++)
    {
        if (strcmp(dealerName, ptr->name) == 0)
        {
            printf("MATCH FOUND:%s\n%s\n%s\n%i\n%s\n", ptr->name,ptr->city,ptr->state,ptr->zip,ptr->phone);
        }
    }
}

1 Answers1

1

You certainly have some '\n' left-over from previous I/O activity.

It is best to use fgets() and not mixed with scanf() in the same program.

But since I can not see the other code, suggest the following to consume a left-over '\n'.

printf("please enter the dealer's name:");
int ch;
ch = fgetc(stdin);
if (ch != '\n') ungetc(ch, stdin);
if (fgets(dealerName, sizeof(dealerName), stdin) == NULL) Handle_EOForIOError();
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256