loop question

M

morpheus

Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line=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.
 
R

ranjmis

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

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line=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
 
D

David Paleino

ranjmis ha scritto:
morpheus said:
Hi Group,
May I ask the following?

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line=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
 
M

morpheus

ranjmis said:
for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line=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=c;

if (c == '\n' || c == EOF )
{
++i;
line='\n';
++i;
line='\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.
 
M

morpheus

David said:
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=c;

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

return(i);

}
 
J

Joe Wright

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

for (i=0; (c=getchar()) != EOF && c != '\n'; ++i)
line=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 = c;
line = 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.
 
M

morpheus

morpheus said:
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
 
J

Joe Wright

morpheus said:
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? ;-)
 
M

morpheus

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.
 
S

stathis gotsis

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=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='\n';
++i;
line='\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.
 
P

Peter Shaggy Haywood

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!
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=c;

if (c == '\n' || c == EOF || (i > limit-2)) /* in cases where EOF does
not have a '\n' token */
{
++i;
line='\n';
++i;
line='\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"?
 
K

Keith Thompson

++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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top