Problem With Loop And Array

J

John

Hello,

I'm trying to write a program that takes a string from a file and prints
each letter on its own line adding a blank line between word.

My code so far:

/*Get each line from file*/
while ((fgets(buffer,MAX_LENGTH,input_file))!=NULL)
{

/*Loop through each character until end of line is reached*/
for(i=0; buffer=='\0'; i++)
{

/*If letter isn't a space then print it*/
if (buffer!=' ')
{
printf("%s", buffer);
}
printf("\n");
}
}

For some reason it won't until the for loop.

I've debugged it and buffer contains the first line of the file (starts
Hello) so don't understand where I'm going wrong.

Cheers.
 
L

Lew Pitcher

Hello,

I'm trying to write a program that takes a string from a file and prints
each letter on its own line adding a blank line between word.

My code so far:

/*Get each line from file*/
while ((fgets(buffer,MAX_LENGTH,input_file))!=NULL)
{

/*Loop through each character until end of line is reached*/
for(i=0; buffer=='\0'; i++) [snip]
}

For some reason it won't until the for loop.


Reread your for() statement. To me, it reads
-start by setting i = 1
- loop *while* buffer is equal to '\0'
- at the end of each loop, increment i by 1

Now, ask yourself, with the first loop (where i == 0), does buffer
contain a '\0'? If it does not, then the loop will exit.

Having said all this, you probably really want
for(i=0; buffer!='\0'; i++)
which loops while buffer is not a '\0'

HTH
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
J

John

Scott Fluhrer said:
John said:
Hello,

I'm trying to write a program that takes a string from a file and prints
each letter on its own line adding a blank line between word.

My code so far:

/*Get each line from file*/
while ((fgets(buffer,MAX_LENGTH,input_file))!=NULL)
{

/*Loop through each character until end of line is reached*/
for(i=0; buffer=='\0'; i++)

You meant:

for (i=0; buffer!='\0'; i++)

You want to loop as long as you haven't hit the terminating '\0' on the
line...
{

/*If letter isn't a space then print it*/
if (buffer!=' ')
{
printf("%s", buffer);
}
printf("\n");
}
}

For some reason it won't until the for loop.

I've debugged it and buffer contains the first line of the file (starts
Hello) so don't understand where I'm going wrong.

Cheers.


Of course! Argh.....

Cheers,
 
K

Keith Thompson

Eric Sosman said:
John wrote: [...]
printf("%s", buffer);

^^^^
I think you mean: "%c"


Or even: putchar(buffer);

Both do the same thing, and there's something to be said for using
printf() consistently rather than printf() plus the assortment of
other output functions for strings and characters. But there's also
something to be said for using the right tool for the job, and
avoiding the complexity of parsing the format string at run time just
to print a single character. (I'm not suggesting that performance is
going to be an issue; the overhead of printf is likely to be swamped
by the delay in performing the output.)
 
S

Stephen Sprunk

Keith said:
Eric Sosman said:
John wrote: [...]
printf("%s", buffer);

^^^^
I think you mean: "%c"


Or even: putchar(buffer);

Both do the same thing, and there's something to be said for using
printf() consistently rather than printf() plus the assortment of
other output functions for strings and characters. But there's also
something to be said for using the right tool for the job, and
avoiding the complexity of parsing the format string at run time just
to print a single character. (I'm not suggesting that performance is
going to be an issue; the overhead of printf is likely to be swamped
by the delay in performing the output.)


Some compilers (e.g. gcc) are smart enough to see that you've used
printf() to do something that could be more efficiently done with
another function and "fix" the call for you, so there's even less reason
now to bother using anything other than printf()...

S
 
K

Keith Thompson

blargg said:
Keith said:
Eric Sosman said:
John wrote: [...]
printf("%s", buffer);
^^^^
I think you mean: "%c"


Or even: putchar(buffer);

Both do the same thing, and there's something to be said for using
printf() consistently rather than printf() plus the assortment of
other output functions for strings and characters. But there's also
something to be said for using the right tool for the job, and
avoiding the complexity of parsing the format string at run time just
to print a single character. (I'm not suggesting that performance is
going to be an issue; the overhead of printf is likely to be swamped
by the delay in performing the output.)


Never mind the performance; with putchar, puts, etc. you get type
checking, and puts( str ) doesn't have the security risk that printf(
str ) has.


The latter is easily addressed by writing printf("%s", str) rather
than printf(str).
 
K

Keith Thompson

Stephen Sprunk said:
Keith said:
Eric Sosman said:
John wrote: [...]
printf("%s", buffer);
^^^^
I think you mean: "%c"


Or even: putchar(buffer);

Both do the same thing, and there's something to be said for using
printf() consistently rather than printf() plus the assortment of
other output functions for strings and characters. But there's also
something to be said for using the right tool for the job, and
avoiding the complexity of parsing the format string at run time just
to print a single character. (I'm not suggesting that performance is
going to be an issue; the overhead of printf is likely to be swamped
by the delay in performing the output.)


Some compilers (e.g. gcc) are smart enough to see that you've used
printf() to do something that could be more efficiently done with
another function and "fix" the call for you, so there's even less
reason now to bother using anything other than printf()...


I agree that performance is not usually a reason to use anything other
than printf(). But personally I find these:
putchar(c);
putchar('\n');
puts(s);
clearer than these, respectively:
printf("%c", c);
printf("\n");
printf("%s\n", s);

YMMV.
 

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
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top