Coder's Guild Mailing List

Scanf

Posted by Darren Varndell on 1999-09-20

You gotta love scanf.  Not sure about you guys but ive never got my programs
to work quite as expected when using scanf unless the buffer is flushed,
worth a try perhaps?

Regards

Remmie

>
>> the above program accepts input from user and writes them to a file.
>> However, to the death of me, I dont understand why it skips the "scanf"
>> commands!!!! without reading the user input.
>> I am aware that when a character entered, we also have to account for
>> the carriage return ("\n") as well. I did account for that in line right
>> after reading the letter and at the very end of the loop. Still doesnt
>> work, can anyone help?
>>
>> Program is as follows :
>> **************************
>> #include <stdio.h>
>> void main()
>> {
>>  FILE *fout;
>>  float fnum;
>>  int inum,i;
>>  char ch,cr;
>>  char string[10];
>>
>>  fout=fopen("d:\\myfile3.txt","w");
>>  if (fout==NULL) printf("Cannot create the file\n");
>>  else
>>  {
>>   for (i=0;i<2;++i)
>>   {
>>    printf("Enter a letter:");
>>    scanf("%c",&ch);
>>    scanf("%c",&cr);
>>    printf("Enter an integer:");
>>    scanf("%d",&inum);
>>    printf("Enter a string:");
>>    scanf("%s",string);
>>    printf("Enter a float:");
>>    scanf("%f",&fnum);
>>    fprintf(fout,"%c %d %s %f\n",ch,inum,string,fnum);
>>    scanf("%c",&cr);
>>   }
>>   if (fclose(fout)==EOF) printf("Cannot close the file\n");
>>  }
>> }
>> ************************************
>>
>>