473,479 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How can I input an EOF value to end a while loop?

5 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main(){
  4.  
  5.        int c, tab = 0, blank = 0, nl = 0;
  6.        printf("EOF = %d", EOF);
  7.        printf("\n\n");
  8.        while ((c = getchar()) != EOF)
  9.              if (c == '\n')
  10.                 ++nl;
  11.              else if(c == '\t')
  12.                 ++tab;
  13.              else (c == ' ');
  14.                 ++blank;
  15.        printf("\nNew Lines: %d", nl);
  16.        printf("\nTabs: %d", tab);
  17.        printf("\nBlanks: %d", blank);
  18.        }
Apr 20 '11 #1
4 3080
mac11
256 Contributor
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
Apr 20 '11 #2
Haris Radoncic
5 New Member
Ok, when I do Ctrl-Z, the program automatically exits instead of just exiting the loop and printing out the output statements, any suggestions?
Apr 20 '11 #3
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.
Apr 21 '11 #4
Haris Radoncic
5 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. main(){
  5.  
  6.        int c, tab = 0, blank = 0, nl = 0, character = 0;
  7.        printf("EOF = %d", EOF);
  8.        printf("\n\n");
  9.        while ((c = getchar()) != EOF){
  10.              if (c == '\n')
  11.                 ++nl;
  12.              else if(c == '\t')
  13.                 ++tab;
  14.              else if(c == ' ')
  15.                 ++blank;
  16.              else (c == ' ');
  17.              ++character;
  18.              }
  19.        printf("\nNew Lines: %d", nl);
  20.        printf("\nTabs: %d", tab);
  21.        printf("\nBlanks: %d", blank);
  22.        printf("\nTotal Keyboard Presses: %d", character);
  23.        getch();
  24.        }
  25.  
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?
Apr 21 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
2161
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...
6
26206
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...
7
3178
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;
4
2364
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...
3
1736
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;...
3
1754
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 { ...
3
2484
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....
6
3737
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...
14
7086
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...
2
3388
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...
0
6903
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...
0
7027
Oralloy
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,...
1
6726
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...
0
5318
agi2029
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,...
0
4468
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...
0
2987
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...
0
2974
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
557
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
170
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.