counting the white space seperated words?

This is a discussion on counting the white space seperated words? within the C++ Programming forums, part of the General Programming Boards category; now. as you can all see by my join date ive been interested in programming for quite some time. and ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    196

    counting the white space seperated words?

    now. as you can all see by my join date ive been interested in programming for quite some time. and ive probably gone through this multiple tutorials to get nowhere.

    i never built a solid foundation. i was using concepts that i didnt understand at all. so im back to basics.

    ive been working on the third excercise from TICPP which states
    3# Create a program that opens a file and counts the whitespace-separated words in that file.
    and ive got. thanks to some fstream help from r.stiltskin

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(){
        int x;
        string msg = "this is a text file";
        string spc = " ";
        string msg2;
        ofstream count_space ("count_space.txt");
        count_space<<msg;
        count_space.close();
        ifstream Count_Space ("count_space.txt");
        Count_Space>>msg2;
        while (getline(Count_Space, msg2) == spc)
        {
                                 x = x++;
    };
        cout<<msg2 <<endl;
        cin.get();
    }
    and my efforts wont work. i get an error i cant even begin to understand. yet i dont know whats wrong with it?

  2. #2
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    "...counts the whitespace-separated words in that file."

    This sounds like it wants you to count the words; in your program, it looks as if you are trying to count the white-space.

    Anyways, a few things:

    Initialize x to zero.
    Add your return statement to the end of main.
    In your x=x++ statement, just make it x++ (the result is the same).

    I wouldn't start to read in anything until the while loop is started.
    Start your loop and read in your strings one at a time. You could use Count_Space >> msg; which skips white space, so you wouldn't have to make any comparisons. Just increase x every time a string is read in (which would be a single word). When it fails (Count_Space.fail()) then break from the loop.
    IDE - Visual Studio 2005
    Windows XP Pro

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    well. at first i realised it was count the words. after first trying to count spaces. but i figure right now itd be a start to count the whitespaces instead of the words. and switch it up when i can do that.

    and i understood the first two sections of what you said. but i might need some trial and error for the last haha (Y)

    thanks alot. this will definitely help

    edit: 3 seconds and the program works o.O..... thank you god! (Cpro).

    but im a bit confused about how to get Count_space.fail() worked in?

    would i do something like

    while (Count_space<<msg && Count_space != Count_space.fail()
    {
    x++
    //return x sorry. not a function. mistake
    };
    once again excuse me. this is my first time using C++ in about 6-9 months

    and also. i seen it easy to just ad cout<<x + 1;
    instead of counting each individual word. i guess this is correct. but is there a better method?

    unless i add string spc to string msg. as then it would still be counting all the words. incidently
    Last edited by lilhawk2892; 04-01-2009 at 09:39 PM.

  4. #4
    Registered User Cpro's Avatar
    Join Date
    Oct 2006
    Posts
    149
    "but im a bit confused about how to get Count_space.fail() worked in?"

    Well, you don't have to use it. It just depends on how you make your while loop. This is how I did it:

    Code:
    while(1)
    {
         Count_Space >> msg;
         if(Count_Space.fail()) break;
         x++;
    }
    So, the loop will break when Count_Space >> msg fails to read in a string (Count_Space.fail() will equal true). There are a few ways to make this loop work, and you don't have to use Count_Space.fail(). I just saw this method used in a book once, and I liked it. For example, you could also do:

    Code:
    while(Count_Space >> msg)
    {
         x++;
    }

    "and also. i seen it easy to just ad cout<<x + 1;
    instead of counting each individual word. i guess this is correct. but is there a better method?"

    I'm not sure what you mean here. You must count each individual word.
    IDE - Visual Studio 2005
    Windows XP Pro

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    "and also. i seen it easy to just ad cout<<x + 1;"

    this is what i mean by that.

    heres the last bit of code
    Code:
        while(1)
    {
         Count_Space >> msg;
         if(Count_Space.fail()) break;
         x++;
    };
    
        cout<<"string has"<<x+1 <<"words in it";
        cin.get();
        return 0;
    }
    ive put the return in.
    and i cheated by adding a 1 to whatever 'x' was. to make up for the extra word in there. but clearly this is cheating.

    and while im here. and i think itd be a more then easy question to answer. why wont my 'cin.get();' wait for the keypress? it just closes.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(){
        int x =0;
        string msg;
        string msg2;
        cout<<"please enter a string\n";
        cin>>msg;
        ofstream count_space ("count_space.txt");
        count_space<<msg;
        count_space.close();
        ifstream Count_Space ("count_space.txt");
        Count_Space>>msg2;
            while(1)
    {
         Count_Space >> msg;
         if(Count_Space.fail()) break;
         x++;
    };
        cout<<"string has"<<x<<"words in it";
        cin.get();
        return 0;
    }
    perhaps i need to close the file im working with?
    Last edited by lilhawk2892; 04-01-2009 at 11:28 PM.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    You should get in the habit of choosing better names for your variables. Naming your ifstream "count_space" and your ofstream "Count_Space" seems as if you are deliberately trying to confuse yourself. Why not ifstream in (or infile) and ofstream out (or outfile)?

    Cpro's second suggested while loop clearly seems the better of the two.

    I, too, do not understand what you mean by this
    Quote Originally Posted by lilhawk2892
    and also. i seen it easy to just ad cout<<x + 1;
    instead of counting each individual word. i guess this is correct. but is there a better method?

    unless i add string spc to string msg. as then it would still be counting all the words. incidently
    and it doesn't seem correct. String spc doesn't seem to serve any purpose and should be deleted. Remember that the exercise is to count words, not to count blank spaces. What if there are two (or more) blank spaces between a pair of words? There would still be the same number of words -- only more blanks.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    You should get in the habit of choosing better names for your variables. Naming your ifstream "count_space" and your ofstream "Count_Space" seems as if you are deliberately trying to confuse yourself. Why not ifstream in (or infile) and ofstream out (or outfile)?

    because this was an excercise i guess i didnt take care to respectfully name variables. i suppose i should have thought about that before i made a post about it.

    but the problems seemed to be all cleared up and understood except for the cin.get() problem. it just keeps closing too early

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by lilhawk2892
    because this was an excercise i guess i didnt take care to respectfully name variables. i suppose i should have thought about that before i made a post about it.
    If you do not practice to develop good habits when doing an exercise, then what is the point of the exercise?

    Quote Originally Posted by lilhawk2892
    but the problems seemed to be all cleared up and understood except for the cin.get() problem. it just keeps closing too early
    That is because a newline is left in the buffer, and it is consumed by that cin.get() call. You should ignore what is left in the buffer by cin.ignore(numeric_limits<streamsize>::max(), '\n') (or cin.ignore(1000, '\n') before using cin.get(). However, it would be simpler to just run your program from a suitable IDE or a command prompt.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    my little program works now. from all your contributions. i feel as though ive accomplished something today.

    but i used cin.ignore(); without the parameters. is this okay? or dangerous?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by lilhawk2892
    but i used cin.ignore(); without the parameters. is this okay? or dangerous?
    It will result in your earlier "problem" if there is more than one character in the buffer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2005
    Posts
    196
    alright i see now. im having the small problem of always coming up with 8. but that shouldnt take more then a few minutes to get past
    Last edited by lilhawk2892; 04-01-2009 at 11:50 PM.

  12. #12
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Your final cin.get() didn't wait for a keypress because the
    Code:
    cin >> msg
    leaves a newline in the stream, which cin.get() accepts as its input.

    You can avoid this by replacing
    cin >> msg
    with
    getline(cin, msg)
    which will extract and discard the newline from the stream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading string with white space in C
    By gep in forum C Programming
    Replies: 2
    Last Post: 05-30-2009, 05:18 PM
  2. counting words in a text file...
    By flightsimdude in forum C Programming
    Replies: 10
    Last Post: 09-19-2003, 07:02 PM
  3. help concering counting of words in one sentc..
    By Ray Thompson in forum C Programming
    Replies: 1
    Last Post: 11-11-2002, 06:55 PM
  4. counting words in string
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 05-30-2002, 04:10 AM
  5. counting the number of words in a file
    By stuartbut in forum C Programming
    Replies: 2
    Last Post: 04-13-2002, 08:19 AM

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