Trouble with while loop counting repeated words

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1122 Views - Last Post: 02 February 2019 - 03:09 PM Rate Topic: -----

#16 albert003   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 669
  • Joined: 15-December 14

Re: Trouble with while loop counting repeated words

Posted 29 January 2019 - 08:56 PM

That makes a lot of sense. Ok, I'll find a different way to solve the issue I have with counting the words in a string. Thanks for your examples they made a lot of sense.
Was This Post Helpful? 0
  • +
  • -

#17 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

Re: Trouble with while loop counting repeated words

Posted 29 January 2019 - 09:27 PM

Quote

I don't want to use that function in my program without understanding how exactly it works.

I suggest you don't even consider using this function in C++, use C++ strings and stringstreams instead.

Jim
Was This Post Helpful? 0
  • +
  • -

#18 albert003   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 669
  • Joined: 15-December 14

Re: Trouble with while loop counting repeated words

Posted 30 January 2019 - 12:44 PM

I found a way to get the program to work. Thank you for all of your suggestions I was able to find a way to use stringstream to split a string into individual words and print them one at a time. The program works perfectly!!!.

This is the program...
#include <iostream>
#include <sstream>
// write a program to count how many times each
//distinct word appears in its input
//using namespace std;
class WordStuff{
public:
    int counting = 0;
    std::string word;//the string entered
    std::string lookup;//the word you want to see the times its repeated
    std::string EnterWord();
    std::string WhichWord();
    void Results();

};
std::string WordStuff::EnterWord()
{
    std::cout << "Enter a sentence:" <<std::endl;
    std::getline(std::cin,word);
}
std::string WordStuff::WhichWord()
{
    std::cout << "\nWhich word do you want to see the number of times" <<std::endl;
    std::cout <<"it was repeated in the sentence.\n" <<std::endl;
    std::getline(std::cin,lookup);
}
void WordStuff::Results()
{
    std::istringstream ss(word);
    std::cout <<"\n"<< word << " has " << word.size() << " characters in it.\n" <<std::endl;

    while(std::getline(ss,word,' '))
    {
        std::cout << word << std::endl;
        if(word == lookup)
        {
            counting++;
        }
    }
    std::cout << "\nThe word " << lookup <<" was repeated " << counting << " times" <<std::endl;
    std::cout << "in the sentence you inputted." <<std::endl;
}
int main()
{
    WordStuff w;
    w.EnterWord();
    w.WhichWord();
    w.Results();
}


Was This Post Helpful? 0
  • +
  • -

#19 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

Re: Trouble with while loop counting repeated words

Posted 30 January 2019 - 01:16 PM

Quote

The program works perfectly!!!.

Ah, not really. Any program that doesn't compile cleanly is not perfect!

||=== Build: Debug in homework (compiler: gcc-8) ===|
/main.cpp||In member function ‘std::__cxx11::string WordStuff::EnterWord()’:|
/main.cpp|20|warning: no return statement in function returning non-void [-Wreturn-type]|
/main.cpp||In member function ‘std::__cxx11::string WordStuff::WhichWord()’:|
/main.cpp|26|warning: no return statement in function returning non-void [-Wreturn-type]|
||=== Build finished: 0 error(s), 2 warning(s) (0 minute(s), 1 second(s)) ===|



Also you would have been better off using the extraction operator>> instead of getline() in your results() function, then it would stop processing the stringstream when it encounters any whitespace character not just the space character.

Also, IMO, that function is doing too much. It should just be either doing the parsing of the sentence or printing the results not both.

Also why is everything in your class public?

Again, IMO, your two "get" functions should probably be global functions that pass the retrieved values to your class using functions provided within the class. You should strive to keep the User Interface separate from the actual program logic.


Jim
Was This Post Helpful? 1
  • +
  • -

#20 albert003   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 669
  • Joined: 15-December 14

Re: Trouble with while loop counting repeated words

Posted 30 January 2019 - 02:47 PM

D'oh!!!! I forgot the get functions need a return function. I split what the Results function was doing into two separate functions so that each one is doing a specific task.

I also changed the getline in the while loop in the results function to cin>>.

It works
I have a question for you and I want to make sure I understand what you mean before I do anything to the program. You suggested I should keep the User Interface separate from the actual program logic. Do you mean I should make the class in a header file and main in the cpp file?.

This is the program with the changes per your suggestions minus my last question.

#include <iostream>
#include <sstream>
// write a program to count how many times each
//distinct word appears in its input
//using namespace std;
class WordStuff{
private:
    int counting = 0;
    std::string word;//the string entered
    std::string lookup;//the word you want to see the times its repeated
public:
    std::string EnterWord();
    std::string WhichWord();
    void FindWords();
    void Results();
};
std::string WordStuff::EnterWord()
{
    std::cout << "Enter a sentence:" <<std::endl;
    std::getline(std::cin,word);

    return word;
}
std::string WordStuff::WhichWord()
{
    std::cout << "\nWhich word do you want to see the number of times" <<std::endl;
    std::cout <<"it was repeated in the sentence.\n" <<std::endl;
    std::getline(std::cin,lookup);

    return lookup;
}
void WordStuff::FindWords()
{
    std::istringstream ss(word);
    while(ss >> word)//std::getline(ss,word,' ')
    {

        std::cout << word << std::endl;
        if(word == lookup)
        {
            counting++;
        }
    }
}
void WordStuff::Results()
{
    std::cout <<"\n"<< word << " has " << word.size() << " characters in it.\n" <<std::endl;
    std::cout << "\nThe word " << lookup <<" was repeated " << counting << " times" <<std::endl;
    std::cout << "in the sentence you inputted." <<std::endl;
}
int main()
{
    WordStuff w;
    w.EnterWord();
    w.WhichWord();
    w.FindWords();
    w.Results();
}


Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

Re: Trouble with while loop counting repeated words

Posted 30 January 2019 - 03:59 PM

Quote

I have a question for you and I want to make sure I understand what you mean before I do anything to the program. You suggested I should keep the User Interface separate from the actual program logic. Do you mean I should make the class in a header file and main in the cpp file?.


No I meant that you should separate the UI from the program logic as much as possible.

For example:

class WordStuff{
private:
    int counting = 0;
    std::string word;//the string entered
    std::string lookup;//the word you want to see the times its repeated
public:
    void get_sentence()
//    std::string EnterWord();
    std::string WhichWord();
    void FindWords();
    void Results();
};

std::string enterWord();

void WordStuff::get_sentence()
{
    word = enterWord()
}

int main()
{
    WordStuff w;
    w.get_sentence();
...

std::string enterWord()  // Note this is not part of the class.
{
    std::string sentence;
    std::cout << "Enter a sentence:" <<std::endl;
    std::getline(std::cin,sentence);

    return sentence;
}



This way if you decide you want to change to a GUI type of program the only thing you will need to change is the enterWord() function, you won't need to alter anything in your class. Also you

By the way I still think your variable naming is a bit wonky. You ask the user to enter a sentence yet you store the value in a variable named word.

Lastly for now your FindWord() function is still doing too much or the function is misnamed, after all it is just counting occurrences of a word in a sentence.

Jim
Was This Post Helpful? 0
  • +
  • -

#22 albert003   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 669
  • Joined: 15-December 14

Re: Trouble with while loop counting repeated words

Posted 01 February 2019 - 11:29 AM

I completed the program, but there's a problem and I can't figure out why its doing it. When I use sentence.size() its suppose to show the total number of characters in the string inputted but instead it only counts the number of characters in the last word inputted. I checked and double checked the program and I can't find what the problem is. Would you please tell give me a hint?

This is the program...


#include <iostream>
#include <sstream>
// write a program to count how many times each
//distinct word appears in its input

class WordStuff{
private:
    int counting = 0;
    std::string sentence;
    std::string lookup;
public:
    void get_sentence();
    std::string WhichWord();
    void FindWords();
    void Results();
};
std::string enterWord();

void WordStuff::get_sentence()
{
    sentence = enterWord();
}
std::string WordStuff::WhichWord()
{
    std::cout << "\nWhich word do you want to see the number of times" <<std::endl;
    std::cout <<"it was repeated in the sentence.\n" <<std::endl;
    std::getline(std::cin,lookup);
    std::cout <<"\n" << std::endl;
    return lookup;
}
void WordStuff::FindWords()
{
    std::istringstream ss(sentence);
    while(ss >> sentence)
    {
        std::cout << sentence << std::endl;
        if(sentence == lookup)
        {
            counting++;
        }
    }
}
void WordStuff::Results()
{
    std::cout <<"\n"<< sentence << " has " << sentence.size() << " characters in it.\n" <<std::endl;
    std::cout << "\nThe word " << lookup <<" was repeated " << counting << " times" <<std::endl;
    std::cout << "in the sentence you inputted." <<std::endl;
}
int main()
{
    WordStuff w;
    w.get_sentence();
    w.WhichWord();
    w.FindWords();
    w.Results();
}
std::string enterWord()
{
    std::string sentence;
    std::cout << "Enter a sentence:" << std::endl;
    std::getline(std::cin,sentence);

    return sentence;
}


Was This Post Helpful? 0
  • +
  • -

#23 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,832
  • Joined: 13-June 14

Re: Trouble with while loop counting repeated words

Posted 01 February 2019 - 11:40 AM

What have you done to debug the program? Have you, say, stepped through with a debugger or added some more print statements to see what it's doing (e.g. to see the variables have the values you expect, or that the right branches in if/else statements are being taken)? You've got to start being able to narrow down where the problem is.
Was This Post Helpful? 1
  • +
  • -

#24 jimblumberg   User is offline

  • member icon

Reputation: 5678
  • View blog
  • Posts: 17,446
  • Joined: 25-December 09

Re: Trouble with while loop counting repeated words

Posted 01 February 2019 - 12:20 PM

Quote

What have you done to debug the program?

This is very important! We need to know how you're trying to debug this problem.

Quote

Have you, say, stepped through with a debugger

Don't forget to "step into" your functions watching the values of the class variables as you step!

Jim
Was This Post Helpful? 0
  • +
  • -

#25 albert003   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 669
  • Joined: 15-December 14

Re: Trouble with while loop counting repeated words

Posted 02 February 2019 - 03:09 PM

I solved the problem and this is how I did it.

When I ran the program, I didn't get any errors. So I started going through each line in the program and checked to make sure I didn't make any clerical errors. From there I realized that the problem started from the function FindWords() and its because of the line where I have this:
std::istringstream ss(sentence);
The reason why was only showing the number of characters in the last word of the string was because that function was breaking down the string into individual words, so logically it would only count the characters in the last word entered. From there I tried to use other functions in c++ (const and tallp) neither worked so I simplified the program and make a new variable called sizesent just before the isstringstream. The function Results() printed the results of the sentence entered, the number of characters and the number of times the specific word was entered perfectly.

This is the program...

#include <iostream>
#include <sstream>
// write a program to count how many times each
//distinct word appears in its input

class WordStuff{
private:
    int counting = 0;
    std::string sentence;
    std::string lookup;
    std::string sizesent;
public:
    void get_sentence();
    std::string WhichWord();
    void FindWords();
    void Results();

};
std::string enterWord();

void WordStuff::get_sentence()
{
    sentence = enterWord();
}
std::string WordStuff::WhichWord()
{
    std::cout << "\nWhich word do you want to see the number of times" <<std::endl;
    std::cout <<"it was repeated in the sentence.\n" <<std::endl;
    std::getline(std::cin,lookup);
    std::cout <<"\n" << std::endl;

    return lookup;
}
void WordStuff::FindWords()
{
    sizesent = sentence;
    std::istringstream ss(sentence);

    while(ss >> sentence)
    {
        std::cout << sentence << std::endl;
        if(sentence == lookup)
        {
            counting++;
        }
    }
}
void WordStuff::Results()
{
    std::cout << "\n" << sentence << " has " <<sizesent.size() <<" characters in it.\n" <<std::endl;
    std::cout << "\nThe word " << lookup <<" was repeated " << counting << " times" <<std::endl;
    std::cout << "in the sentence you inputted." <<std::endl;
}
int main()
{
    WordStuff w;
    w.get_sentence();
    w.WhichWord();
    w.FindWords();
    w.Results();
}
std::string enterWord()
{
    std::string sentence;
    std::cout << "Enter a sentence:" << std::endl;
    std::getline(std::cin,sentence);

    return sentence;
}


This post has been edited by albert003: 03 February 2019 - 12:44 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2