0

I'm having an issue with my code where the output will print statements earlier than I want it to and I was advised that it was due to input buffering and that adding a blank space before the "%" in my scanf would solve the problem but it is still a recurring issue.

printf("Please enter the contact's first name: "); scanf(" %31s", name.firstName);

printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf(" %c", &option);


if (option == 'y' || option == 'Y')
{
printf("Please enter the contact's middle initial(s): ");
scanf("  %7s", name.middleInitial);
}

printf("Please enter the contact's last name: ");
scanf(" %36s", name.lastName);


// Contact Address Input:

printf("Please enter the contact's street number: ");
scanf(" %d", &address.streetNumber);

printf("Please enter the contact's street name: ");
scanf(" %41s", address.street);

printf("Do you want to enter an apartment number? (y or n): ");
scanf(" %c", &option);

if (option == 'y' || option == 'Y')
{
    printf("Please enter the contact's apartment number: ");
    scanf(" %d", &address.apartmentNumber);
}

printf("Please enter the contact's postal code: ");
scanf(" %8s", address.postalCode);

printf("Please enter the contact's city: ");
scanf(" %41s", address.city);

// Contact Numbers Input:
printf("Do you want to enter a cell phone number? (y or n): ");
scanf(" %c", &option);

if (option == 'y' || option == 'Y')
{
    printf("Please enter the contact's cell phone number: ");
    scanf(" %11s", numbers.cell);
}

printf("Do you want to enter a home phone number? (y or n): ");
scanf(" %c", &option);

if (option == 'y' || option == 'Y')
{
    printf("Please enter the contact's home phone number: ");
    scanf(" %11s", numbers.home);
}

printf("Do you want to enter a business phone number? (y or n): ");
scanf(" %c", &option);

if (option == 'y' || option == 'Y')
{
    printf("Please enter the contact's business phone number: ");
    scanf(" %11s", numbers.business);
}

Here's the output

  • Your example shows the input to be scanned by `scanf(" %41s", address.street);` is "Keele Street". But the `%s` format specifier stops at the first whitespace so this isn't going to work. I suggest `scanf(" %41[^\n]", address.street);`. You will only need to place the leading space before this, and `%c`, because the other formats automatically filter leading whitespace. Similarly with any other string that might be more than one word e.g. the city "New York" and last name "Da Silva" etc. – Weather Vane Mar 09 '20 at 00:09
  • ...also, you should *always* check the return value from `scanf`, which is the number of items that were successfully scanned. Returning to the previous comment, I don't think that clearing the input buffer is the problem, but multiple words in one string. – Weather Vane Mar 09 '20 at 00:13
  • I think the wrong duplicate was posted. The problem isn't about clearing the input buffer (OP took steps to clear leading whitespace anyway) but about not reading the whole input of multiple-word strings. – Weather Vane Mar 09 '20 at 00:19

0 Answers0