Coder's Guild Mailing List

Entering values with C (doesn't work!!!)

Posted by Hasan on 1999-09-18

> 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");
>  }
> }
> ************************************
> 
>