A simple soultion please

This is a discussion on A simple soultion please within the C++ Programming forums, part of the General Programming Boards category; Ok..i am new to C++...very new. I have a class and I have a project due....the task at hand...to make ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    A simple soultion please

    Ok..i am new to C++...very new. I have a class and I have a project due....the task at hand...to make a program that inports a smaple text file and echo prints it..then counts the number of words in the file...now I got it to do that..but if I put more than one space between a word it adds and exra one onto the count..so how do I get it to count the gaps instead of indivuduale spaces? there has to be an easy solution to this...but i cant find it anywhere..if you can offer any help..it would be greatly appritiated.

  2. #2
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I don't think you should attempt to count the gaps. Depending on how you are reading the file you can get the read to halt when it reaches whitespace (the end of a word), so everytime it halts you add one to your count of words and then you loop to read until the next whitespace is encountered and so on until you reach to end of file. If you are unsure post some code.
    zen

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    If you read until you encounter a space then add one to the count and continue reading until a non-space character is encounterer and then continue your regular loop. Something like this pseudo code:

    Code:
    while (not end of file)
    {
          Get a character
          write a character
          if ( '  ' == character )
          {
                add one to word count;
                do
                {
                     get a character
                     write a character
                }while( '  ' == character  || '\r' == character || 
                             '\n' == character )
    
           }
                
    }
    I've added another boundary condition if you run into the end of a line (carriage return ('\r') and line feed ('\n') they don't count as a word new word. This is for the case where the start of the next line is a space. You also need to put in logic to catch end of file while reading for non space characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

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