silverthunder01
silverthunder01

Reputation:

getchar returns unexpected character

I try to compile this code:

#include <stdio.h>

void print(FILE *a)
{
int main();
int count=20;
int c;
int stop=0;
char answer;

while(!stop){
    while((c=getc(a))!=EOF){
            fprintf(stdout,"%c",c);
            if(c=='\n'){
                    count--;
                    if(!count){
                        printf("do you want continue:y=for continue/q=for quit");
                        fflush(stdin);
                            answer=getchar();
                        if(answer=='y' || answer=='Y')
                            count=20;
                        else if(answer=='Q' || answer=='q'){
                            printf("you quit this program,press any key and hit the enter to close");
                            stop=1;
                            break;
                            }
                        else{
                            printf("argument is unacceptable,rolling back action");
                            main();
                            }
                        }
                }
        }
    if(c==EOF)
        stop=1;
    }
}
void halt()/*do nothing just for halt and waiting for input*/
{
int a;

scanf("%d",&a);
}
int main()
{
FILE *in,*fopen();
char name1[25];
int a;

printf("enter the name of the file you want to show:");
scanf("%24s",name1);
in=fopen(name1,"r");
if(in==NULL){
    printf("the files doesnt exist or it is in another directory, try to enter again\n");
    main();
        }
else
    print(in);

fclose(in);
halt();

return 0;
}

what is really matter is this part:

if(!count){
    printf("do you want continue:y=for continue/q=for quit");
    fflush(stdin);
    answer=getchar();
    if(answer=='y' || answer=='Y')
         count=20;
    else if(answer=='Q' || answer=='q'){
         printf("you quit this program,press any key and hit the enter to close");
         stop=1;
         break;
    }
    else{
         printf("argument is unacceptable,rolling back action");
         main();
    }
}

The program is expected to show 20 line content of a file, but when i run it in terminal, something goes off when it come to if-elsepart it tend to execute the else whichever i put in the in the answervariable.did i do something wrong

NB:i have tried this code with different compiler such as lcc-win32 and it works fine.

Upvotes: 1

Views: 181

Answers (2)

Ulrich Dangel
Ulrich Dangel

Reputation: 4625

The problem is that fflush(stdin) is not standardized and behaves different on different system. For more information have a look at at http://c-faq.com/stdio/stdinflush.html and http://c-faq.com/stdio/stdinflush2.html

So what is happening here: You are using scanf("%24s",name1); to read data from stdin, e.g a user enters a filename (e.g. test.c and presses Return. stdin now contains test.c\n. scanf reads now up to 24 characters from stdin, in our case test.cand leaves \n in stdin, this means stdin contains now \n. The fflush(stdin) call does not remove \n from stdin which means your getchar() call will read \n from stdin which explains the described behavior.

Content of stdin over time

  1. Before scanf: test.c\n
  2. After scanf: \n - scanf used test.c and assigned it to name1
  3. After fflush(stdin): \n
  4. After getchar(): empty as getchar read and returned \n

For more information about this exact problem and maybe a better description have a look at the c-faq entry about fflush vs. gets

Upvotes: 2

Levon
Levon

Reputation: 143047

Not sure what the problem is, this is based on a pairded down q&d example of your code and works as expected.

#include <stdio.h>

int main(int argc, char * argv[])
{
  char  answer;

  while(1) {
    printf("\ndo you want continue:y=for continue/q=for quit: ");
    fflush(stdin);
    answer=getchar();

    if(answer=='y' || answer=='Y')
      puts("y/Y entered");
    else if(answer=='Q' || answer=='q'){
      puts("q/Q entered");
      break;
    }
    else
      puts("Something other than q/y entered.");
  }
}

Unfortunatley, I'm pressed for time right now, so I can't check if there are any substantial differences between your version and mine .. I don't recall anything.

This was tested with gcc 4.4.3 under Ubuntu.

See if this works for you.

Upvotes: 3

Related Questions