----- Original Message -----
From: "Gandalf"
To:
Sent: Saturday, June 05, 2004 5:41 PM
Subject: [c-prog] simple prog, scanf trouble
> hello,
> i dont know whether attachments are allowed or not, so i post the
> trouble-part of the code here
>
> i have the following structure
> struct det
> {
> char determiner[15];
> char type;
> int person; //1 2 3
> char gender; //m -male f -female n -neutral
> char number; //s - singular p - plural
> char kase;
> char posesor;
> } info[20];
>
>
> then i have to fill the info[20] array with some information;
> i use the following function
>
> void create(int pos)
> {
> for(int i=0;i {
> printf("enter the determiner: "); scanf("%s",&info[pos].determiner);
> //gets(info[pos].determiner);
> printf("enter the type: "); scanf("%c",&info[pos].type);
> printf("enter the person: "); scanf("%i",&info[pos].person);
> printf("enter the gender: "); scanf("%c",&info[pos].gender);
> printf("enter the number: "); scanf("%c",&info[pos].number);
> printf("enter the case: "); scanf("%c",&info[pos].kase);
> printf("enter the posesor: "); scanf("%c",&info[pos].posesor);
> printf("done w/ %i\n", i);
> }
> printf("done");
>
> }
>
Replace the above sequence of printf()'s and scanf()'s with the below one:
printf("enter the determiner: ");
scanf("%14s%*[^\n]",info[pos].determiner);
/* Note that width is given as 14 since determiner is 15 character long.
This will avoid overflowing the buffer. 15th location is reserved
for '\0' */
printf("enter the type: ");
scanf("%c%*[^\n]",&info[pos].type);
printf("enter the person: ");
scanf("%i%*[^\n]",&info[pos].person);
printf("enter the gender: ");
scanf("%c%*[^\n]",&info[pos].gender);
printf("enter the number: ");
scanf("%c%*[^\n]",&info[pos].number);
printf("enter the case: ");
scanf("%c%*[^\n]",&info[pos].kase);
printf("enter the posesor: ");
scanf("%c%*[^\n]",&info[pos].posesor);
>
> this is how the func is called from the main program
>
> printf("how many elems do you need?");
> scanf("%i",&length);
> create(length);
>
>
>
> the problem: for some reason it doesnt read all the needed
> variables. what i dont understand is why after i insert the 'determiner',
> it doesnt wait for me to insert the 'type', instead it prints the second
> message (insert 'person'), and so on...
>
> could someone explain what is happening?
Yes, why not? The problem is obvious if you do not understand how the
*scanf() family of functions work! Following is a bird's eye-view of their
working:
+ White-space characters are ignored in the beginning unless the
specifier ([]) indicates so.
+ All non-white-space characters are read and converted to format
according to the specifier mentioned.
+ The new-line character, if any, is left in the stream. It is this
new-line which was causing the trouble, which must be removed from
the stream. The specifier, %*[^\n] removes the trailing new-lines.
See the question number 76 of the FAQ page.
Refer a good book on C for more details.
               (
geocities.com/vijoeyz/faq)                   (
geocities.com/vijoeyz)