Having some problems with homework.

This is a discussion on Having some problems with homework. within the C++ Programming forums, part of the General Programming Boards category; Ok We are doing the basic I/O problems where you count the words, line and characters in a text file ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32

    Having some problems with homework.

    Ok We are doing the basic I/O problems where you count the words, line and characters in a text file along with some other stuff.

    With counting words I have it checking for spaces but it doesn't find a space at the end of the line so it doesn't count that last word.

    Code:
    if(myInFile.is_open()){
    			
    			while(getline(myInFile, theText)){			
    				//myInFile >> word;
    				lineCount++;
    				for(int i = 0; i < theText.length(); i++){
    					ch = theText[i];
    					if(isspace(ch))
    						wordCount++;
    					cout << "char: " << ch << endl;
    				}
    				
    				
    			}
    		}
    So if I have something like

    The Dog ran away
    Yesterday he came back.

    It will only count 6 words. It will not count the words away and back.

    Thanks for any help.

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    well, a cheap way of doing it is to concatenate a space on teh string before you count the words. A better way woudl be to have it see the end of teh string as whitespace as well. You could also count the beginnings of the words instead of the whitespace.
    Last edited by abachler; 12-12-2008 at 01:54 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    How would you go about doing that?

    I tried to see if ch == '\0' but that doesnt seem to work.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You have another problem as well. What if the line looks like this:
    Code:
            once    upon     a      time
    If you count a word each time you find a space, this line will give you 8 words before you get to "once". Instead, count 1 word when you find the first letter on the line, then just keep reading and printing characters until you find the next blank space, and then continue reading and printing until you find the first non-blank character, and only then do you record the 2nd word, and so on...

    Thus you will increase your count at the beginning of each word, not the end. Then, if you want to get fancy, check if the final character on the line is a hyphen, so you'll know not to count the first "word" on the next line because it is actually a continuation of the last word on the previous line.
    Last edited by R.Stiltskin; 12-12-2008 at 02:00 AM. Reason: Added additional info

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    What is isn't doing is reading the last word of a line because there is no space after it.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Djanvk View Post
    What is isn't doing is reading the last word of a line because there is no space after it.
    Are you saying that the output to the screen after reading "The dog ran away" is just
    Code:
    t
    h
    e
     
    d
    o
    g
     
    r
    a
    n
    ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Common Problems
    By WolfPack in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 06:38 PM
  2. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  3. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 05:49 PM
  5. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 04:16 PM

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