473,465 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

loop question

Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line[i]=c;
What I am finding is that when c == '\n' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you.

Apr 8 '06 #1
14 1606

morpheus wrote:
Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line[i]=c;
What I am finding is that when c == '\n' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you


can you please post a sample input-output of the program along with
your "expected" output

Apr 8 '06 #2
ranjmis ha scritto:
morpheus wrote:
Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line[i]=c;
What I am finding is that when c == '\n' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you


can you please post a sample input-output of the program along with
your "expected" output


And, I'd add, could you please post a *complete code*, not just a
snippet. So we can see what's wrong with your loop.

David

--
Linux Registered User #334216
Get FireFox! >> http://www.spreadfirefox.com/?q=affiliates&id=48183&t=1
Staff >> http://www.debianizzati.org <<
Apr 8 '06 #3

ranjmis wrote:
for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line[i]=c;

can you please post a sample input-output of the program along with
your "expected" output


My actual code is this:

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line[i]=c;

if (c == '\n' || c == EOF )
{
++i;
line[i]='\n';
++i;
line[i]='\0';
}

Test input; "test
"

Note that test is followed by 4 blank spaces and a newline token.

I was expecting "line" to contain 'test\n\0' but it contains
test'something'\n\0. Now, I do appreciate that ++i in line 5 is
causing this behaviour, but I do not fully appreciate why line 5 would
not be necessary, I guess.

Apr 8 '06 #4

David Paleino wrote:
And, I'd add, could you please post a *complete code*, not just a
snippet. So we can see what's wrong with your loop.


Here is the code in full....
int getline( char line[], int limit)
{

int c, i = 0;

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
if ( i < limit-2)
line[i]=c;

if (c == '\n' || c == EOF || (i > limit-2)) /* in cases where EOF does
not have a '\n' token */
{
++i;
line[i]='\n';
++i;
line[i]='\0';
}

return(i);

}

Apr 8 '06 #5
morpheus wrote:
Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line[i]=c;
What I am finding is that when c == '\n' is true, the loop is
terminated but i is still incremented. I was under the impression that
termination of the loop should not increment i.
Thank you.

You didn't really show us the code. I made a file, mor.txt which is two
lines..

Hello
World

...and a program, mor.c, which looks like this..

#include <stdio.h>
int main(void)
{
char line[80];
int i, c;
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
line[i] = c;
line[i] = 0;
printf("i is %d, line is %s\n", i, line);
return 0;
}

Compile the program and execute `mor < mor.txt`.

I get..

i is 5, line is Hello

The variable i was 5 when c == '\n' and is not incremented further.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 8 '06 #6

Joe Wright wrote:

i is 5, line is Hello


Strange. I took your program and ran it on Xcode. This is what I got.
i is 6, line is Hello
World

Apr 8 '06 #7

morpheus wrote:
Joe Wright wrote:

i is 5, line is Hello


Strange. I took your program and ran it on Xcode. This is what I got.
i is 6, line is Hello
World

I stand corrected....If I enter "Hello world" manually, then i=5, just
as you show, with the line being "Hello". So, my understanding is
correct, just that the code is wrong somewhere!!! :-)
Thanks

Apr 8 '06 #8
morpheus wrote:
Joe Wright wrote:
i is 5, line is Hello


Strange. I took your program and ran it on Xcode. This is what I got.
i is 6, line is Hello
World

What is Xcode? Your output indicates the "&& c != '\n'" in the for
conditional is not being evaluated.

Do you have a C compiler? ;-)
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Apr 8 '06 #9

Do you have a C compiler? ;-)
--

Whats a c-compiler? JUST KIDDING....

No...as I indicated above, it is working as it should, it was I who was
misinterpreting it.

thanks for your help.

Apr 8 '06 #10
"morpheus" <md**@comcast.net> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Here is the code in full....
There are some potential problems with this code.

int getline( char line[], int limit)
{

int c, i = 0;
I suspect limit is the number of bytes you have allocated for line outside
getline(). Your purpose is to read (limit-2) characters from stdin, then
add '\n' to line and then null-terminate it. Am i guessing right?


for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
if ( i < limit-2)
line[i]=c;
At this point if i gets bigger (or equal) than (limit-2) you do not store
extra characters into line, but i still gets incremented while you eat up
the rest of stdin. You use the final value of i later on your code.
if (c == '\n' || c == EOF || (i > limit-2)) /* in cases where EOF does
not have a '\n' token */
How about when i==limit-2?
{
++i;
line[i]='\n';
++i;
line[i]='\0';
If i got bigger (or equal) than limit-2 while it was inremented in your
previous loop, this part will acess line[] past the end of it. So you may
access memory you did not allocate. You should rewrite your code to avoid
that.
}

return(i);
Parentheses are redundant. }

Apr 9 '06 #11
Groovy hepcat morpheus was jivin' on 8 Apr 2006 08:50:04 -0700 in
comp.lang.c.
Re: loop question's a cool scene! Dig it!
David Paleino wrote:
And, I'd add, could you please post a *complete code*, not just a
snippet. So we can see what's wrong with your loop.
Here is the code in full....


Where? All I see is one function definition; and even that doesn't
have declarations of any used identifiers in scope, except local
variables. I see no headers included, no main() function, no input
data, no expected output and no actual output.
int getline( char line[], int limit)
{

int c, i = 0;

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
if ( i < limit-2)
line[i]=c;

if (c == '\n' || c == EOF || (i > limit-2)) /* in cases where EOF does
not have a '\n' token */
{
++i;
line[i]='\n';
++i;
line[i]='\0';
}

return(i);

}


This code is a mess! It's very badly formatted. That makes it a pain
to read. You can help yourself alot by formatting your code better.
It is difficult to know what you expect from this code.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Apr 12 '06 #12
++i : first increase 1;
you should use i++;

Apr 12 '06 #13
je****@56.com wrote:

++i : first increase 1;
you should use i++;


The dopeyness of your advice
is matched by the nebulosity of your post.

--
pete
Apr 12 '06 #14
je****@56.com writes:
++i : first increase 1;
you should use i++;


Read <http://cfaj.freeshell.org/google/>. Read it now.

The news server on which I'm reading this doesn't have the article to
which you're replying, so I can't tell what you're talking about (I
could look it up on groups.google.com, but I'm not going to bother).
Given the subject, "loop question", my guess is that you're wrong.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Apr 12 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Arthur | last post by:
>>>a= >>> for p in a: print p 1 2 3 >>> p 3 My naive expectation was that p would be 'not defined' from outside
0
by: John Wilson | last post by:
Hello, I have the following code which populates as table data from a SQL Server 2000 stored proc (RSByDemoID2). Below that is the view and stored procedure which takes @DemoID as input to match...
3
by: zeroDoNotYeSpamtype | last post by:
(I tried to post this earlier, but it seems without success) A similar question to this has been answered many times, to whit the question applying when the index variable of a for loop is...
3
by: Gustavo Randich | last post by:
The following seems to be a bug. The execution returns rows 1,2. It should return 1,1. In fact, if I run the code within a stored procedure alone (not in a trigger), the loop doesn't overwrite the...
22
by: Jan Richter | last post by:
Hi there, the Code below shows DJBs own implementation of strlen (str_len): unsigned int str_len(char *s) { register char *t; t = s; for (;;) { if (!*t) return t - s; ++t;
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
29
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is:...
3
by: nina297 | last post by:
Good morning, How do I set up a For each loop to loop through my records in the database? My fields are topics, question and answers. I want the one topic displayed that goes with each...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
2
by: perkykoala | last post by:
I apologize in advance for being REALLY detailed/verbose. It's the result of staring/tweaking code for too long. Using VB 2005: I need to design a multiple choice test (unfortunately, I can't...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
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
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.