Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Simple C Problem--Help, Please?

Name: Anonymous 2008-06-07 13:57

Hey, I've just started learning C and am going through The C Programming Language, reading it and doing the exercises.  Up till now everything had been all right, but Exercise 1-8 has been giving me some trouble.  If someone could help me out, that'd be very much appreciated.  Here's the exercise and my code:

Exercise 1-8: Write a program to count blanks, tabs, and newlines.

code:

#include <stdio.h>
int main()
{
     int nl, t, b = 0;
     int c;

     while ((c = getchar() != EOF)
     {
          if (c == ' ')
               ++b;
          if (c == '\n')
               ++nl;
          if (c == '\t')
               ++t;
     }

     printf("newlines: %d\ntabs: %d\nblanks: %d\n", nl, t, b);

     return 0;
}

Now, the problem is this: whenever I give input, the blanks and tab characters are counted properly, but the number of newlines is always some gastronomically high number. 

For example, if I give "blah blah blah(tab)dah(new line)" and then give the EOF, I get this:

newlines: 2423969
tabs: 1
blanks: 2

So yeah, that's it.  I feel pretty stupid about it, and I get the feeling there's just some really simple, stupid thing that I fucked up, but for the life of me, I can't figure out what it is.  Help, please?

Name: Anonymous 2008-06-07 14:03

int nl, t, b = 0;
There's your problem.

Name: Anonymous 2008-06-07 14:10

>>2
Oh. 

Fuck. 

Thank you very much!

Name: HAX MY ANUS MEME FAN 2008-06-07 17:45

Wow, I didn't expect a thread like this on my /prog/. Nice.

Name: Anonymous 2008-06-07 20:03

>>4
Not yours, gtfo. Also >>1-3 are the same person. Anyone who is able to recognize the problem explained by >>2 knows already what do those huge numbers mean. WHBTC

Name: Anonymous 2010-12-28 0:24

Name: Sgt.Kabukiman촐簌 2012-05-23 15:10

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Name: bampu pantsu 2012-05-29 3:44

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List