473,325 Members | 2,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

how can i come out of the loop : while(getchar()!=EOF);

Hello everyone..
I am doing C Programming in Ubuntu G++ compiler

in one program i need to come out out the code


#include<stdio.h>
char c;
while( (c=getchar())!= EOF)
printf("something\n");
printf("end of file\n");


while running this program, which word to type to get out of while loop.
Thanks
Aug 4 '07 #1
5 13770
Meetee
931 Expert Mod 512MB
Hello everyone..
I am doing C Programming in Ubuntu G++ compiler

in one program i need to come out out the code


#include<stdio.h>
char c;
while( (c=getchar())!= EOF)
printf("something\n");
printf("end of file\n");


while running this program, which word to type to get out of while loop.
Thanks
Hi,

You can compare (c=getchar()) with some character like
while( (c=getchar())!= '\n')
If you press ENTER in this case, it will come out of while loop.

Regards
Aug 4 '07 #2
JosAH
11,448 Expert 8TB
Hi,

You can compare (c=getchar()) with some character like
while( (c=getchar())!= '\n')
If you press ENTER in this case, it will come out of while loop.

Regards
True, but the OP's problem is that 'c' is a char in his program. If he'd change
it to an 'int' type the test for EOF would work as expected. The -1 value (which
is the EOF) value is truncated to 8 bits for the char and most likely his char
type is unsigned, so that -1 value comes out again as 255 which definitely isn't
equal to -1. Making 'c' an int eliminates the truncation.

kind regards,

Jos
Aug 4 '07 #3
Thanks.
I came to know the last day one more fact that in linux Ctrl+D also work as EOF.
Aug 5 '07 #4
Hello everyone..
I am doing C Programming in Ubuntu G++ compiler

in one program i need to come out out the code


#include<stdio.h>
char c;
while( (c=getchar())!= EOF)
printf("something\n");
printf("end of file\n");


while running this program, which word to type to get out of while loop.
Thanks
i think break statment na or not my friend, good prog u wrote
Aug 5 '07 #5
@amit1702
hey this does work but when we press it twice else it only work for exiting while loop and not main. But the solution is really gr8 thanks a lot. could you also tell the alternative for <conio.h> available in linux.
Mar 1 '09 #6

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

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
47
by: Mountain Bikn' Guy | last post by:
Take some standard code such as shown below. It simply loops to add up a series of terms and it produces the correct result. // sum numbers with a loop public int DoSumLooping(int iterations) {...
5
by: !TG | last post by:
I currently use Do while loop, but I'd rather use a For Loop though I have never gotten the hang of them. Would some one please be so kind as to show me how to loop through a recordset.
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.