0

I am a newbee writing a C program for school where the input is redirected to a file. I am to use getchar() only to retrieve the information. I am using Windows Visual 2008 and I cannot figure out why my code will not exit the loop. Can anyone help me out? Thanks.

while (rec != 'EOF') { while (rec != '\n') { variable=getchar; printf ("this is variable %c"); } } 

3 Answers 3

3
while (rec != EOF) { rec=getchar(); if((rec != '\n') && (rec != EOF)){ printf ("this is variable %c\n",rec); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

You left the mistakes on getchar without () and printf that doesn't bind the %c to rec. You've set the value of rec in a better place though.
0
int c = 0; while (c != EOF) { c = getchar(); if (c == '\n') break; printf("c:%c\n", c); } 

1 Comment

That stops at the first \n. I'm not sure that is what is needed.
0

The answer depends on what is really needed. If you want to print every character except the new lines, you want something like:

int c = getchar(); // Note c is defined as an int otherwise the loop condition is broken while (c != EOF) { if (c != `\n`) { printf("c:%c\n", c); } c = getchar(); } 

If you just want the characters on the first line:

int c = getchar(); while (c != EOF && c != `\n`) { printf("c:%c\n", c); c = getchar(); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.