Thread: sTRING ... BUT WHY IT IS STRANGE....

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Unhappy sTRING ... BUT WHY IT IS STRANGE....

    Hi all,
    can you all look at this ...
    I need to count the words that I got by the input stream..
    why it doesn't count them.... it count just the first word...
    and the first leters.... so Please tell me why it is strange...
    This is the program:
    #include<iostream.h>
    #include<string>

    int main()
    {
    string str1;
    int j=0;
    int counterwords =1;

    cout<< "Please input your text string... \n";
    cin>> str1;
    string::iterator str1_ptr = str1.begin();
    int i=0;
    while (str1_ptr <= str1.end()){
    i++; // increase one visit... to the loop
    if (*str1_ptr == ' ')
    {
    counterwords++; // how many words we have here
    }
    str1_ptr++;// shift one to the righ.. Read one char.
    }
    cout<< " the number of words that you have is ...";
    cout << counterwords<< endl;
    cout<< " I did vist the loop for ..."<< i<< " times" << endl;

    return 0;
    }

    GOOD LUCK!!!
    C++
    The best

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    That is the resulte...

    the resulte is
    [NANO0@****] (~) ./counter2
    Please input your text string...
    can you beleive this... I can't
    the number of words that you have is ...1
    I did vist the loop for ...4 times

    ----------------
    in thetime that I want it to tell me that
    the number of words that you have is ... 6 words
    the number of visits is 31...
    ---------
    from this I can read that it read the first word not all hte string ,,,
    and that is not FAAAAAAAAR


    I am really
    DO you know why... it is like this,,,
    Do you have a sol. Please help me...
    C++
    The best

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    the problem is relatively simple to fix...

    instead of using a input stream cin like this:

    cin >> str1;

    this stopes the input as soon as the space is encountered... ' '

    use this....

    getline(cin, str1);

    in this particular case the string will read in untill you hit "enter" and will also read in white spaces, just swap those lines and you should be fine....otherwise if you remain with your original line it will only read ONE WORD......

    good luck

    matheo917

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    let me try it ///

    I will try it right now...
    That is COOOOOOL
    C++
    The best

  5. #5
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Glad I could help.


    Regards,
    matheo917

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    You did

    Thank you ... and thanks for this website...
    it is very helpfull for beginner , in my case, inorder not to give up studying programming languages ... espically C++

    Thanx
    C++
    The best

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Ok I am done with it...

    here is the program....

    #include<iostream.h>
    #include<string>

    int main(){

    string str1;
    int j=0;
    int counterwords =1;

    cout<< "Please input your text string... \n";
    getline(cin, str1);
    cout<< "It is in \n";
    int a = str1.length();
    cout<< "the length of this string is ... "<< a<< endl;

    string::iterator str1_ptr = str1.begin();
    int i=0;
    while (str1_ptr <= str1.end())
    {
    i++;
    if (*str1_ptr == ' ')
    {
    counterwords++; // how many words we have here
    }
    // if (*str1_ptr != ( ' '|| ',' || ';' || '.' ||'!' || '?' ))
    if (*str1_ptr != ','|| *str1_ptr != ' '|| *str1_ptr != '.' || * str1_ptr != '!'|| str1_ptr != '?')
    {
    j++;// how many charactors that the lines have
    }
    str1_ptr++;
    }// while
    int ave =0;
    j--;// to shift j one because it has 1more char

    ave = j/ counterwords ;// what is the avarage of the words in this line...
    cout<< " the number of words that you have intered is :" << counterwords<<endl; cout<< "the number of the char that you have
    is :"<< j<< endl;
    cout<< " the avarage of the word lingth is :" << ave << endl;
    cout<< endl;
    cout<< "take care"<<endl;
    cout<<"the program interd the loop for :"<<i<< " times"<<endl;
    cout<<"thanks for your help guys ";
    return 0;
    }

    --------------------------
    Thanks for your help guys
    --------------------------
    C++
    The best

  8. #8
    Unregistered
    Guest

    I thought it was cin.getline... oh well, I'm probably wrong.
    I'm reasonably new, there's probably a whole heap of different ways.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Cool I Would like to ask everybody here

    I would like everybody to past their program after it run, inorder to understand what is going on... and what is the right Sol.

    : I did start that by myself ... putting the right Sol. at the end of every thread that I asked you to help me with .....

    Please do the same... We are here to learn from each other...

    Thanks
    C++
    The best

  10. #10
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    Excellent.

    Just one thing......

    Please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please please
    USE CODE TAGS (please).

    THANKYOU
    Visit entropysink.com - It's what your PC is made for!

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Wink Do you think it is better

    I know it is good ...

    But what about if I want to change in the input output stream...
    Can I use it....


    CAn you Please explane Why to me... ?
    and I really thank you for your advice ... But tell me why.. ?

    Please...
    Thanks
    C++
    The best

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    Question Now I can close this one...

    I will close this thread now..
    C++
    The best

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I tried to close it but I could not..

    Can you help me please in closeing this thread...
    Thanx
    C++
    The best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM