Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9025109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:11:25+00:00 2026-06-16T06:11:25+00:00

Possible Duplicate: scanf: %[^\n] skips the 2nd input but %[^\n] does not. why? Basically

  • 0

Possible Duplicate:
scanf: “%[^\n]” skips the 2nd input but “ %[^\n]” does not. why?

Basically I have a customer struct where I am to enter customer details, one of which is the address. Of course the address is a sentence since this is a basic text program without graphics.

I’m trying to use scanf("%[^\n]",&VARIABLE) method because that worked in previous programs.
However here , the input is being skipped. I tried to flush the buffer before I take this input but it did not make any difference. I also tried to create another string and pass my input to that and then copy the data to my struct and that did not work either.

Here is my code – The problem is occuring at the 4th scanf("%[^\n]",&myCust.address) : (NB: this is a work in progress so you might see some extra prints and stuff for now)

void addNewCustomer()
{
    struct customer myCust;
    printf("\n\nNEW CUSTOMER ADDITION\n");
    printf("\nEnter customer id : ");
    scanf("%s",&myCust.idNumber);
    printf("\nEnter customer name : ");
    scanf("%s",&myCust.name);
    printf("\nEnter customer surname : ");
    scanf("%s",&myCust.surname);
    fflush(stdin);
    printf("\nEnter customer address : ");
    scanf("%[^\n]",&myCust.address);
    printf("\nEnter customer telephone : ");
    scanf("%s",&myCust.telephone);
    printf("\nEnter customer mobile : ");
    scanf("%s",&myCust.mobile);
    printf("\nEnter customer e-mail : ");
    scanf("%s",&myCust.email);

    FILE *fp;

    fp = fopen("/Users/alexeidebono/Dropbox/Customer_Application/customers.dat","a");
    if (fp == NULL) {
        printf("The File Could Not Be Opened.\n");
        exit(0);
    }
    else{
        printf("File Successfully Open\n");
   fprintf(fp,"%s*%s*%s*%s*%s*%s*%s#\n",myCust.idNumber,myCust.name,myCust.surname,myCust.address,myCust.telephone,myCust.mobile,myCust.email);
    fclose(fp);
    printf("Writing successfully completed and the file is closed!!\n");
   }
}

if you want my struct code here it is ( although i dont think that the struct itself is the cause of this problem )

struct customer
{
    char idNumber[11]; 
    char name[11];
    char surname[15];
    char address[30];
    char telephone[14];
    char mobile[14];
    char email[21];


};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:11:26+00:00Added an answer on June 16, 2026 at 6:11 am
    scanf("%s",&myCust.surname);
    

    This scanf leaves a newline in the input buffer

    fflush(stdin);
    

    is undefined behaviour by the standard, and doesn’t work reliably in my experience even if the library promises it would.

    printf("\nEnter customer address : ");
    scanf("%[^\n]",&myCust.address);
    

    This finds the newline immedaitely. So it doesn’t read anything in, because it first encounters a newline. Make it skip whitespace first by including a space in the format,

    scanf(" %[^\n]",&myCust.address);
    

    Or use fgets or getline (if you’re on a POSIX system) to read in an entire line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Simple C scanf does not work? Why does scanf(%c, &letter); not working.
Possible Duplicate: Dynamic String Input - using scanf(%as) strcmp with pointers not working in
Possible Duplicate: How to scanf only integer and repeat reading if the user enter
Possible Duplicate: Using fflush(stdin) My code is: scanf(%d, &_choice); fflush(stdin); gets(input); I use fflush(stdin);
Possible Duplicate: Program doesn’t wait for user input with scanf(%c,&yn); I've just started programming
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there
May be possible duplicate but couldn't have found the same. Suppose I have following
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: JavaScript: why does parseInt(1/0, 19) return 18? Why does parseInt(1/0, 19) evaluate
Possible Duplicate: confusion in scanf() with & operator Why we need a & in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.