Thread: How to count characters, but not spaces?

  1. #16
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Prelude's code with modification
    Code:
    #include <cctype>
    ...
    while ( inFile.get ( c ) ) {
      cout<< c;
      if ( c == '\n' ) {
        linecount++;
        charcount = 0;  //  it's a new line so let's start counting from zero again 
      }
      else if ( !isspace ( c ) )
        charcount++;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Just count the characters in one line, when you reach \n simply output whatever message you see fit, and set charCount = 0; ready for the next line.

    If you're having trouble, post your latest code.

    Tip: Don't use this code to control a loop:
    while (!inFile.eof())
    It's a bad habbit. There's an explanation of why here:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    That's a C FAQ, but the principle still stands. You can code your way out of it and still use eof(), but its better not to.

    [edit]
    Doh! Didn't see alphaoide's last message
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Jan 2004
    Posts
    49
    Yeah!!! I think I can do this. Thanks a tera-bunch for helping me out.

    Fudd's law states: "What goes in must come out." Aside from being patently untrue, Fudd's law neglects to mention that what comes out need not bear any resemblence to what went in.

    -V. Orehck III (ficticious)
    Semper Fi!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  2. consecutive number
    By stewie1986 in forum C Programming
    Replies: 3
    Last Post: 12-03-2007, 03:53 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  5. Characters in a txt file.
    By tay_highfield in forum C Programming
    Replies: 3
    Last Post: 01-31-2003, 09:19 AM