-1

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];


};
Community
  • 1
  • 1
alexeidebono
  • 99
  • 2
  • 6
  • 12

3 Answers3

2
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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
  • The fgets didnt work as it was still skipped ( I forgot to mention that in my question since I tried it aswell - sorry about that) However, leaving a space in the address scanf did the trick. Thanks for that answer - it all makes sense now. Cheers mate – alexeidebono Dec 20 '12 at 22:06
  • If you use `fgets`, you should use it for all entries. Since that consumes the newline, the next input doesn't find one immediately. Mixing `scanf` and `fgets` is rarely a good idea, that causes terrible headaches because of the different treatment of newlines. – Daniel Fischer Dec 20 '12 at 22:09
  • I'm gonna take your opinion actually since fgets seems like the best way to go. – alexeidebono Dec 20 '12 at 22:11
  • Just don't forget that `fgets` stores the newline in the buffer (unless the entered line is too long, in which case you would have to clear the input buffer before the next input). – Daniel Fischer Dec 20 '12 at 22:14
1

The title:

Scanf() is being skipped when trying to read including whitespaces

Yes. That's not an error. That's how scanf() works (please read the documentation of the functions you're trying to use more carefully next time). If you want to get a whole line, regardless of its contents, use fgets():

char buf[1024];
fgets(buf, sizeof(buf), stdin);
  • just tried this method - it is still being skipped unfortunately i tried two ways 1. instead of "buf" in the above case i passed my struct variable 2. i used another char (named it test for this sake) and tried to just input data to that (without passing it further to my struct) and it still skipped it – alexeidebono Dec 20 '12 at 22:02
  • @alexeidebono How about a bit of a code cleanup? Are you sure you're doing this at the right place? `fgets()` really should get an entire line, whether or not it contains whitespaces. –  Dec 20 '12 at 22:03
  • this worked though - scanf(" %[^\n]",&myCust.address); (left a space in the beginning ) – alexeidebono Dec 20 '12 at 22:07
0

if a scanf() command is being skipped, that's usually because you have junk from previous scans entered.

try adding another scanf(&junk) before the one that is skipped. junk should be char.

bladefist
  • 148
  • 1
  • 12