Thread: string help.

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    48

    string help.

    I am trying to find the number of words in a line via getline(istream,line,'\n') by counting white spaces. What I am using to count white spaces is this:

    int pos = Line.find(' ',0);
    while(pos != string::npos)
    {
    pos = Line.find(' ',pos);
    nwords++;
    cout << pos << endl;
    }

    Is what im doing with find() legal in regard to white spaces? When this loop is executed,
    cout for pos = 6, which is where my first white space is, however I cannot get it to proceed....

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    So you mean a line like this will have
    10 words
    Code:
    a          a

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    48
    hmmm... dont understand... here is void main, and the calling function:

    void analyzeLine( string Line, string & longest, int & nwords, int & nchars)
    {
    int length = Line.size();
    int pos = Line.find(' ',0);
    while(pos != string::npos)
    {
    pos = Line.find(' ',pos);
    nwords++;
    cout << pos << endl;
    }
    nchars = length + nchars;
    cout << length << " " << nchars << endl;
    }

    void main ()
    {
    WritePoem();
    ifstream poem;
    poem.open("poem.txt");
    const int NPOS = string::npos;
    int nchars = 0, nwords = 0;
    string Line, longest;
    while(!poem.eof())
    {
    getline(poem,Line,'\n');
    analyzeLine(Line, longest, nwords, nchars);
    //cout << Line << endl;
    //cout << nwords << endl;
    }
    }


    What I am trying to do is read each line of the file with getline() and storing it into string Line, then counting the white spaces in Line via find(), where the result should give me the amount of words(if I add 1 to the result), however I cant seem to get it to find any white spaces other than the first one when find() is called.

  4. #4
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    however I cant seem to get it to find any white spaces other than the first one when find() is called.
    I think the reason why the code doesn't work
    the way you want it to is that you have to skip
    over the space just found. Try find(' ', pos + 1).

    Code:
    while(!poem.eof())
    {
          getline(poem,Line,'\n');
          analyzeLine(Line, longest, nwords, nchars);
    //cout << Line << endl;
    //cout << nwords << endl;
    }
    getline(poem, Line, '\n') could reach the eof and
    then analyzeLine would be given invalid data I think.

    It's better to have your functions do one thing and void
    main is bad.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    What if you had many spaces after each other:
    "This is me"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM