My name is Abi. I'm still an amateur on programming. I copied a code from my text-book. Here's the code:
include <stdio.h>
include <stdlib.h>
int main(void)
{
int numin;
printf ("Please enter the first number: ");
while((scanf("%d", &numin)) != EOF)
{
printf ("Please enter next number or <EOF>: ");
}
system("pause");
return 0;
}
I want to get out from the while loop by entering the <EOF> key, but I don't know which key I need to push. Can anybody tell me? I've been having this problem for a while, resulting that I always skip any code that has anything to do with <EOF> key. So, please help me.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2008-08-13
From Wikipedia:
"In computing, end-of-file, commonly abbreviated EOF, is a condition in a computer operating system where no more data can be read from a data source."
"an end-of-file indication can be sent from an interactive shell (console) by typing Ctrl+D (conventional standard). In Microsoft's DOS and Windows it is sent by pressing Ctrl+Z."
Here "+" means simultaneously (hit Ctrl key first).
As an aside note: today Wikipedia and Google must be taken as an universal manual and help guide for almost every thing, specially in IT.
HTH
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Ah, yeah! You're right! It's Ctrl+Z!
Uh, yeah. You're right again. there's an explanation about it on wikipedia.
Anyway, thanks a lot. Next time I'll try to search around first. bye.
Abi
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You have to wonder about a text-book that requires such arcane knowledge. That said, if you are going to program a computer I always claim you should also know how to use one! The problem is there are few real situations these days where you would need to know how to enter an EOF into an input stream.
If you change the while statement thus:
while((scanf("%d", &numin)) > 0 )
The loop will terminate whenever you enter something that is not a decimal numeric (or whitespace which is ignored) - including EOF. In fact because of the way scanf() works you could enter:
1 2 3 4 5 6 7 8 9 Q
as one line, and the loop would execute 9 times before quitting.
To see why this change is 'better' run your original code and type Q<enter> at the first prompt (or anything that is not a number). Now try it with the modification. Again, you have to wonder about a text book that does not consider this problem.
Finally, whenever you refer to a "text-book", please tell is the title/author/edition. People often ask the same questions and encounter the same problems, and it often turns out they are using the same book or reference material. It helps us to know what is good/bad/problematic/outdated, and it also helps in quickly identifying problems. Moreover others can find relevant posts (or we can find what we said about it last time) by searching on the book title.
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My name is Abi. I'm still an amateur on programming. I copied a code from my text-book. Here's the code:
include <stdio.h>
include <stdlib.h>
int main(void)
{
int numin;
printf ("Please enter the first number: ");
while((scanf("%d", &numin)) != EOF)
{
printf ("Please enter next number or <EOF>: ");
}
system("pause");
return 0;
}
I want to get out from the while loop by entering the <EOF> key, but I don't know which key I need to push. Can anybody tell me? I've been having this problem for a while, resulting that I always skip any code that has anything to do with <EOF> key. So, please help me.
From Wikipedia:
"In computing, end-of-file, commonly abbreviated EOF, is a condition in a computer operating system where no more data can be read from a data source."
"an end-of-file indication can be sent from an interactive shell (console) by typing Ctrl+D (conventional standard). In Microsoft's DOS and Windows it is sent by pressing Ctrl+Z."
Here "+" means simultaneously (hit Ctrl key first).
As an aside note: today Wikipedia and Google must be taken as an universal manual and help guide for almost every thing, specially in IT.
HTH
Ah, yeah! You're right! It's Ctrl+Z!
Uh, yeah. You're right again. there's an explanation about it on wikipedia.
Anyway, thanks a lot. Next time I'll try to search around first. bye.
Abi
You have to wonder about a text-book that requires such arcane knowledge. That said, if you are going to program a computer I always claim you should also know how to use one! The problem is there are few real situations these days where you would need to know how to enter an EOF into an input stream.
If you change the while statement thus:
while((scanf("%d", &numin)) > 0 )
The loop will terminate whenever you enter something that is not a decimal numeric (or whitespace which is ignored) - including EOF. In fact because of the way scanf() works you could enter:
1 2 3 4 5 6 7 8 9 Q
as one line, and the loop would execute 9 times before quitting.
To see why this change is 'better' run your original code and type Q<enter> at the first prompt (or anything that is not a number). Now try it with the modification. Again, you have to wonder about a text book that does not consider this problem.
Finally, whenever you refer to a "text-book", please tell is the title/author/edition. People often ask the same questions and encounter the same problems, and it often turns out they are using the same book or reference material. It helps us to know what is good/bad/problematic/outdated, and it also helps in quickly identifying problems. Moreover others can find relevant posts (or we can find what we said about it last time) by searching on the book title.
Clifford