Thread: Find my mistake!

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Find my mistake!

    Hi, im a beginner of programming c and this is an exercise from the book The C Programming Language by Bran W. Kernighan and Dennis M. Ritchie, exercise 1-8.
    I am trying to simply count the spaces, tabs, new lines of an input.

    Code:
    #include<stdio.h>
    
    main(){
           
        int line=0,tab=0,space=0;
        int input;
        
        while ((input=getchar())!=EOF){
              if (input=='\t'){
              ++tab;
              }
              
              if (input==' '){
              ++space;
              }
              
              if (input=='\n'){
              ++line;
              }
        }
        
        printf ("%2d%2d%2d",space,tab,line);
    
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Make input a char and try again.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    No, getchar() returns an int.

    It works well for me. What's your problem with it?

    Edit:
    Didn't see that this was double posted:
    Find my mistake!
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Found the mistake...

    It's a double post error!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,683
    See NeonBlack's link - double post closed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. Please help! - Who find mistake in the program??
    By nivek in forum C++ Programming
    Replies: 4
    Last Post: 01-06-2008, 01:28 AM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM