I picked up the "C Programming Language" and have been learning it bit by bit but Im alittle confused about EOF. I example 1-8, we need to write a program that outputs the number of tabs, blanks, and newlines entered into the program, using a while loop to control when the program performs the outputs. I know that EOF is a negative value (-1 apparently), but how do I enter it into the program so that the while loop ends since every keystroke is interpretted as a character. Here's the code: - #include <stdio.h>
-
-
main(){
-
-
int c, tab = 0, blank = 0, nl = 0;
-
printf("EOF = %d", EOF);
-
printf("\n\n");
-
while ((c = getchar()) != EOF)
-
if (c == '\n')
-
++nl;
-
else if(c == '\t')
-
++tab;
-
else (c == ' ');
-
++blank;
-
printf("\nNew Lines: %d", nl);
-
printf("\nTabs: %d", tab);
-
printf("\nBlanks: %d", blank);
-
}
4 3080
If you're typing input from the command prompt you type CTRL+Z for windows or CTRL+D for linux
If you're reading from a file it will just get an EOF when the end is reached automatically
Ok, when I do Ctrl-Z, the program automatically exits instead of just exiting the loop and printing out the output statements, any suggestions?
Banfa 9,065
Recognized Expert Moderator Expert
The problem you are encountering is because the stdin and stdout streams normally connected to the keyboard and console are continuous, they do not end so you never get an EOF like you do say for a file where you get an EOF at the end of the file.
Either make the program look out for some sentinel character you can easily detect to end it or make the program read an actual file that will result in an EOF.
BTW while you code is correct it would generally be considered best practice today to use rather more braces {} than you have to delineate the blocks of code executed by the while statement and all the if statements.
I got it to run perfectly with the Ctrl-Z advice. I modified the code slightly to to include printing tabs, new lines, blanks, and the total number of key presses. -
#include <stdio.h>
-
#include <conio.h>
-
-
main(){
-
-
int c, tab = 0, blank = 0, nl = 0, character = 0;
-
printf("EOF = %d", EOF);
-
printf("\n\n");
-
while ((c = getchar()) != EOF){
-
if (c == '\n')
-
++nl;
-
else if(c == '\t')
-
++tab;
-
else if(c == ' ')
-
++blank;
-
else (c == ' ');
-
++character;
-
}
-
printf("\nNew Lines: %d", nl);
-
printf("\nTabs: %d", tab);
-
printf("\nBlanks: %d", blank);
-
printf("\nTotal Keyboard Presses: %d", character);
-
getch();
-
}
-
Quick question though, am I basically a few steps away from making a word count program? I can modify it to basically enter a file name (as suggested above) where it prints the information once it reaches the end of the document. I'll try it out but i dont see why it cant work. Programming is fun isnt it?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Redduck |
last post by:
Hello everyone. I am frustrated, I have written the simple program
below for a class and I am having problems with the DO-WHILE loop. On
the first run through the loop, everything works well, the...
|
by: obdict |
last post by:
Hello,
I used scanf() in a while loop, which ensures that user input is
valid (must be an integer no greater than 21 or less than 3).
If user enters a number out of the range, or enters...
|
by: DaVinci |
last post by:
I am writing a pong game.but met some problem.
the ball function to control the scrolling ball,
void ball(int starty,int startx)
{
int di ,i;
int dj,j;
di = 1;
dj = 1;
i = starty;
|
by: kelharis |
last post by:
I am trying to write a program using a while loop, and a nested for loop inside. The program needs to run once, and then after completion, ask if the user would like to run the program again. The...
|
by: rmijares |
last post by:
I am sure you guys have seen this before. I am working on the famous Employee Payroll program for a class assignment. In any case, the while loop at the end of the Employee class just keeps looping;...
| |
by: metalinc |
last post by:
i'm trying to do a do while loop, to keep printing the data that matches the key of mymap.
my while statement printed an endless loop....
What should the while statement be??
do
{
...
|
by: bmerlover |
last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
|
by: gsuns82 |
last post by:
Hi all,
Is it possible to read the input value given by the user by ctrl+v,how do we use java script to read the input?
example: If the user selects a value using ctrl+c from some where...
|
by: kadeous |
last post by:
Greetings,
Currently my code is arranged in this nature, and please excuse me for being so new, I'm learning! :) So no laughing at my echo's as I don't know much about tables yet.
The issue I...
|
by: John Hutchison |
last post by:
Hey everybody, this is my first post, so I hope I'm doing this right. I'm just starting to learn how to program, so I'm sorry if my error is obvious, but I can't seem to find the answer.
I...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |