Find my mistake!

This is a discussion on Find my mistake! within the C Programming forums, part of the General Programming Boards category; Hi, im a beginner of programming c and this is an exercise from the book The C Programming Language by ...

  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. But the problem is, when i compile and run, then type something and enter, nothing comes up. just blanks.
    Please find any mistakes or possible solutions!! Thanks.

    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
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Do you know that you need to signal EOF from the keyboard? CTRL-Z in Windows, I believe, CTRL-D in Linux.

    In other words, type in some text, some blanks, some tabs, and newlines - whatever, and when you are finished, do EOF from the keyboard, and the program will print the results.
    Last edited by kermit; 02-02-2011 at 06:43 PM.

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

    Thanks, Kermit

    following your advice, it works now.

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, 12:28 AM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 07: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, 08:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21