Detecting EOF

J

jobo

Hello,

I'm trying to detect the EOF for scanf. But for some reason I can't get
it to trigger. What am I doing wrong? Thanks.

int puzzle[9][9]; // Puzzle data structure
int i, j, count; // Iteration variables
int temparr[81];
char x;
int test;
count = 0;
while (count < 82){

scanf("%c", &x);
if (x == EOF) {
printf("****!");
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
test = test - 48;
temparr[count]= test;
count++;
}
}
 
P

Papastefanos Serafeim

jobo said:
Hello,

I'm trying to detect the EOF for scanf. But for some reason I can't get
it to trigger. What am I doing wrong? Thanks.

int puzzle[9][9]; // Puzzle data structure
int i, j, count; // Iteration variables
int temparr[81];
char x;
int test;
count = 0;
while (count < 82){

scanf("%c", &x);
if (x == EOF) {
printf("****!");
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
test = test - 48;
temparr[count]= test;
count++;
}
}

From the manpage of scanf:

These functions return the number of input items assigned,
which can be fewer than provided for, or even zero, in the
event of a matching failure. Zero indicates that, while
there was input available, no conversions were assigned;
typically this is due to an invalid input character, such
as an alphabetic character for a `%d' conversion. The
value EOF is returned if an input failure occurs before
any conversion such as an end-of-file occurs. If an error
or end-of-file occurs after conversion has begun, the num-
ber of conversions which were successfully completed is
returned.

Also, EOF cannot be represented with a char, so the
return value of scanf has to be assigned to an int.

So, to check for EOF you should try something like this

char x;
int r ;
r = scanf("%c", &x);
if (r == EOF) {
....
}
 
A

Andrew Poelstra

Hello,

I'm trying to detect the EOF for scanf. But for some reason I can't get
it to trigger. What am I doing wrong? Thanks.

First off, excellent job formatting. One minor issue: you shouldn't
use // comments on Usenent because they'll eventually wrap over and
become syntax errors. I've changed your code in that respect.
int puzzle[9][9]; /* Puzzle data structure */

Hey, hey hey! Where did you start main()? You need to post
a /compilable/ snippit.
int i, j, count; /* Iteration variables */
int temparr[81];

Magic numbers make me nervous. (At the very least, comment in that it's
an array as large as puzzle[][]).
char x;
int test;
count = 0;
while (count < 82){

There's that magic number again! Use <= 81 to be consistent, but you
should really define some constants at the start of your program.
scanf("%c", &x);

Just use getchar() for this:
x = getchar();
Much simpler, no?
if (x == EOF) {

Here's your problem: EOF is not necessarily in the range of char.
(Specifically, if char is unsigned, it is not, but if char is signed
[which is likely], it may be.) Define x as int so that you are
guaranteed to be able to hold EOF.
printf("****!");

Having a little trouble with this one, eh? ;-)
printf("%d", count);
return 0;
}
test = x;
if (test >= 48 && test <= 57) {
What is this test? It looks like some character test dependant on ASCII.
#include said:
test = test - 48;

Ditto here. I think that you're trying to convert a character digit to a
numberic one. In that case, try
test -= '0';
Your intent will be much clearer.
temparr[count]= test;
count++;
}
}

Where were you using the puzzle[][] array? Or i and j? You should have
just gotten rid of them if they were unnecessary to your post.
 
R

Richard Heathfield

Andrew Poelstra said:
On Wed, 2006-10-25 at 00:46 -0700, jobo wrote:
int temparr[81];
char x;
int test;
count = 0;
while (count < 82){

There's that magic number again! Use <= 81 to be consistent, but you
should really define some constants at the start of your program.

I think you meant < 81 or possibly <= 80

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top