This is my second time taking C++ course and I'm about to fail again since I'm barely hanging on with a C-D grade percentage. Here is the problem. I'm using the book "Beginning C++ Through Game Programming - 2nd Edition" and Dev-C++ to compile code.

I'm not even majoring in Computer Science so Programming is just not me. If anyone could make this work I'd appreciate it in advance, thanks. :'(

Rewrite the Mad Lib Game, so that there is a new object, a storyWordManager object, instantiated from a StoryWordManager class. The madLib object will be given a pointer to the storyWordManager object as a parameter that is passed into the MadLib constructor. The storyWordManager object's constructor method will prompt the user for the necessary story words (or numbers) and will accept input from the cin object. The madLib object will invoke public "get" methods to access the necessary story words as the story is built and "told".

#include <iostream>
#include <string>

using namespace std;

// pass constant reference instead of copy of string object
string askText(const string& prompt);
int askNumber(const string& prompt);
void tellStory(const string& name, 
               const string& noun, 
               int number, 
               const string& bodyPart, 
               const string& verb);

int main()
{
    cout << "Welcome to Mad Lib.\n\n";
    cout << "Answer the following questions to help create a new story.\n";
    
    string name = askText("Please enter a name: ");
    string noun = askText("Please enter a plural noun: ");
    int number = askNumber("Please enter a number: ");
    string bodyPart = askText("Please enter a body part: ");
    string verb = askText("Please enter a verb: ");
    
    tellStory(name, noun, number, bodyPart, verb);

    return 0;
}

string askText(const string& prompt)
{
    string text;
    cout << prompt;
    cin >> text; 
    return text;
}

int askNumber(const string& prompt)
{
    int num;
    cout << prompt;
    cin >> num;
    return num;
}

void tellStory(const string& name, 
               const string& noun, 
               int number, 
               const string& bodyPart, 
               const string& verb)
{
    cout << "\nHere's your story:\n";
    cout << "The famous explorer ";
    cout << name;
    cout << " had nearly given up a life-long quest to find\n";
    cout << "The Lost City of ";
    cout << noun;
    cout << " when one day, the ";
    cout << noun;
    cout << " found the explorer.\n";
    cout << "Surrounded by ";
    cout << number;
    cout << " " << noun;
    cout << ", a tear came to ";
    cout << name << "'s ";
    cout << bodyPart << ".\n";
    cout << "After all this time, the quest was finally over. ";
    cout << "And then, the ";
    cout << noun << "\n";
    cout << "promptly devoured ";
    cout << name << ". ";
    cout << "The moral of the story? Be careful what you ";
    cout << verb;
    cout << " for.";
    
    system("pause");
    
}

Dani AI

Generated

As noted, the assignment specifically requires a StoryWordManager class that gathers the words in its constructor and exposes public "get" methods, and a MadLib class that receives a pointer to that manager in its constructor and uses those getters. The posted snippets (and 's tiny skeleton) show the idea but miss constructors, getters, input robustness and pointer-safety. The short example below implements those pieces and shows a safe, minimal layout that meets the brief.

#include <iostream>
#include <string>
#include <sstream>
#include <stdexcept>

class StoryWordManager {
    std::string name_, noun_, bodyPart_, verb_;
    int number_;
public:
    StoryWordManager() {
        std::string line;
        std::cout << "Please enter a name: ";
        std::getline(std::cin, name_);
        std::cout << "Please enter a plural noun: ";
        std::getline(std::cin, noun_);
        while (true) {
            std::cout << "Please enter a number: ";
            std::getline(std::cin, line);
            std::stringstream ss(line);
            if (ss >> number_) break;
            std::cout << "Invalid number; try again.\n";
        }
        std::cout << "Please enter a body part: ";
        std::getline(std::cin, bodyPart_);
        std::cout << "Please enter a verb: ";
        std::getline(std::cin, verb_);
    }
    const std::string& getName() const { return name_; }
    const std::string& getNoun() const { return noun_; }
    int getNumber() const { return number_; }
    const std::string& getBodyPart() const { return bodyPart_; }
    const std::string& getVerb() const { return verb_; }
};

class MadLib {
    const StoryWordManager* mgr_;
public:
    MadLib(const StoryWordManager* mgr) : mgr_(mgr) {
        if (!mgr_) throw std::invalid_argument("null manager");
    }
    void tellStory() const {
        std::cout << "\nHere's your story:\n"
                  << "Explorer " << mgr_->getName() << " nearly gave up the hunt for the Lost City of "
                  << mgr_->getNoun() << ". Surrounded by " << mgr_->getNumber() << " " << mgr_->getNoun()
                  << ", a tear appeared on " << mgr_->getName() << "'s " << mgr_->getBodyPart()
                  << ". Then the " << mgr_->getNoun() << " unexpectedly devoured " << mgr_->getName()
                  << ". Moral: be careful what one " << mgr_->getVerb() << ".\n";
    }
};

int main() {
    StoryWordManager swm;
    MadLib story(&swm);
    story.tellStory();
    return 0;
}

Notes and troubleshooting:

  • Use std::getline for multi-word answers; mixing operator>> and getline causes leftover-newline problems.
  • Validate numeric input (shown with string + stringstream) instead of trusting >>.
  • Make getters const and return const& for strings to avoid copies.
  • Ensure the StoryWordManager instance outlives the MadLib that holds its pointer (the sample creates both in main).
  • Avoid system-dependent pauses; declare int main() and return properly.
  • For real projects, split declarations into headers and implementations and consider using references or smart pointers instead of raw pointers; the pointer requirement here is implemented but checked for null.

Forming a small team and splitting tasks (as suggested) can also help salvage late projects: one member handles I/O and validation, another implements classes, another tests edge cases.

Recommended Answers

All 8 Replies

class StoryWordManager {
string askText(const string& prompt);
int askNumber(const string& prompt);

};
int main(int argc,char *argv[]) {

}

That is an easy homework.

This is my second time taking C++ course and I'm about to fail again since I'm barely hanging on with a C-D grade percentage. Here is the problem. I'm using the book "Beginning C++ Through Game Programming - 2nd Edition" and Dev-C++ to compile code.

I'm not even majoring in Computer Science so Programming is just not me. If anyone could make this work I'd appreciate it in advance, thanks. :'(

Rewrite the Mad Lib Game, so that there is a new object, a storyWordManager object, instantiated from a StoryWordManager class. The madLib object will be given a pointer to the storyWordManager object as a parameter that is passed into the MadLib constructor. The storyWordManager object's constructor method will prompt the user for the necessary story words (or numbers) and will accept input from the cin object. The madLib object will invoke public "get" methods to access the necessary story words as the story is built and "told".

What is the question? You have explained your situation and your desire for some one to "make this work", and you have posted your code and the assignment, but you have not asked a specific question and you have not said what works, what doesn't, what you've tried, what the results are, etc. If your hope is that someone will read the assignment, change your code to reflect the assignment criteria, then post the completed program, that isn't going to happen here. You need to ask a more specific question. People will help you, but they won't do the whole thing for you.

class StoryWordManager {
string askText(const string& prompt);
int askNumber(const string& prompt);

};
int main(int argc,char *argv[]) {

}

That is an easy homework.

Ok Boss this is what I have. Now here is the thing I got now, is this correct or is there something else I'm missing here? Please Boss help me! :icon_confused:

#include <iostream>
#include <string>

using namespace std;
              
string askText(const string* const prompt);
int askNumber(const string* const prompt);
void tellStory(const string* const name, const string* const noun, int number, const string* const bodyPart, const string* const verb);


main()
{
    cout << "Welcome to Mad Lib.\n\n";
    cout << "Answer the following questions to help create a new story.\n";       
    string prompt = "Please enter a name: ";
    string name = askText(&prompt);   
    prompt = "Please enter a plural noun: ";
    string noun = askText(&prompt);    
    prompt = "Please enter a number: ";
    int number = askNumber(&prompt);   
    prompt = "Please enter a body part: ";
    string bodyPart = askText(&prompt);
    prompt = "Please enter a verb: ";
    string verb = askText(&prompt);    
    tellStory(&name, &noun, number, &bodyPart, &verb);
    int x; cin >> x;
    return 0;
}

string askText(const string* const prompt)
{
    string text;
    cout << *prompt;
    cin >> text; 
    return text;
}

int askNumber(const string* const prompt)
{
    int num;
    cout << *prompt;
    cin >> num;
    return num;
}

void tellStory(const string* const name, const string* const noun, int number, const string* const bodyPart, const string* const verb)

{
    cout << "\nHere's your story:\n";
    cout << "The famous explorer ";
    cout << *name;
    cout << " had nearly given up a life-long quest to find\n";
    cout << "The Lost City of ";
    cout << *noun;
    cout << " when one day, the ";
    cout << *noun;
    cout << " found the explorer.\n";
    cout << "Surrounded by ";
    cout << number;
    cout << " " << *noun;
    cout << ", a tear came to ";
    cout << *name << "'s ";
    cout << *bodyPart << ".\n";
    cout << "After all this time, the quest was finally over. ";
    cout << "And then, the ";
    cout << *noun << "\n";
    cout << "promptly devoured ";
    cout << *name << ". ";
    cout << "The moral of the story? Be careful what you ";
    cout << *verb;
    cout << " for.";
}

Yes, there is something missing:

Rewrite the Mad Lib Game, so that there is a new object, a storyWordManager object, instantiated from a StoryWordManager class. The madLib object will be given a pointer to the storyWordManager object as a parameter that is passed into the MadLib constructor. The storyWordManager object's constructor method will prompt the user for the necessary story words (or numbers) and will accept input from the cin object. The madLib object will invoke public "get" methods to access the necessary story words as the story is built and "told".

You don't have a StoryWordManager class or a MadLib class and you don't have any constructors and you don't have any "get" methods. Are these classes something you are supposed to code from scratch or were they provided for you?

A fellow mate from class gave me that code, he's been working on. I posted it here to see if it was correct or not.

A fellow mate from class gave me that code, he's been working on. I posted it here to see if it was correct or not.

So, you wanted to hand in some else's piece of crap as your own ?? Don't you think your instructor will notice the resemblence?

Nope he doesn't mind. He actually encourages us to work together on projects, the classmates has partnered up but weeks gone by and many people drop the course so its getting harder and harder to work alone now. There is only 5 people left and only 1 out of 5 is probable to passing out of all of us. -__-

In that case why don't all 5 of you form a group and do the project as a group? You could each code the part you know how to code. But it might be too late for you all to do that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.