Well technically I don't know how to make it quit. I am very new to programming. I wrote my first program three weeks ago and I must say it is a wonderful mix of highs and lows, especially when your code compiles. Anyway, I have been trying to figure out how to write a Mad-Lib game with classes. One class (StoryWordManager) for the intro, getting user input, and providing it to the other class(MadLib) using pointers, so it can put together the story. The problem is that the StoryWordManager class never stops. It asks for a name, noun, body part, verb, and a number, but then it asks for another verb, and another verb, and another verb. It is only supposed to ask for one verb and then it is supposed to create the story. Eventually the goal is to divide the code up into a StoryWordManager.h file, a StoryWordManager.cpp, and a MadLibMain.cpp, but that is just a dream until I resolve this issue. If there any other issues that you see please mention them. Here's the code:

// Mad-Lib 4.0
// Mad-Lib game with classes and pointers

#include <iostream>
#include <string>

using namespace std;

class StoryWordManager 
{
      public:
             StoryWordManager();
             
             string getName();
             string getNoun();
             string getBodyPart();
             string getVerb();
             
             int getNumber();
             
             string m_Name;
             string m_Noun;
             string m_BodyPart;
             string m_Verb;
              
             int m_Number;
             
      //private:
              string m_Prompt;
              
              string askText();
              int askNum();
              
              
};

StoryWordManager::StoryWordManager()
{
                                       
     cout << "Welcome to the Mad-Libs game.\n\n" << endl;
     cout << "Answer the questions to help create a story:\n\n" << endl;

     m_Prompt = "name";
     m_Name = askText();
     
     m_Prompt = "noun";
     m_Noun = askText();
     
     m_Prompt = "body part";
     m_BodyPart = askText();
     
     m_Prompt = "verb";
     m_Verb = askText();
              
     m_Number = askNum();
     
}
     
inline string StoryWordManager::askText()
{   
       string text;
       cout << "Please enter a " << m_Prompt << ".";
       cin >> text;
       return text;
}

int StoryWordManager::askNum()
{
    int num;
    cout << "Please enter a number.";
    cin >> num;
    return num;
}


string StoryWordManager::getName()
{
      return m_Name;
}

string StoryWordManager::getNoun()
{
      return m_Noun;
}

string StoryWordManager::getBodyPart()
{
      return m_BodyPart;
}

string StoryWordManager::getVerb()
{
      return m_Verb;
}
int StoryWordManager::getNumber()
{
      return m_Number;
}

class MadLib
{
      public:
             MadLib(StoryWordManager*);

};

MadLib::MadLib(StoryWordManager* StoryWordManagerPtr)
{
                                    
    cout << "\n\nHere's your story:\n\n";
    cout << "The famous explorer ";
    cout << (*StoryWordManagerPtr).getName();
    cout << " had nearly given up a life-long quest to find\n";
    cout << "The Lost City of ";
    cout << (*StoryWordManagerPtr).getNoun();
    cout << " when one day, the ";
    cout << (*StoryWordManagerPtr).getNoun();
    cout << " found the explorer.\n\n";
    cout << "Surrounded by ";
    cout << (*StoryWordManagerPtr).getNumber();
    cout << " " << (*StoryWordManagerPtr).getNoun();
    cout << ", a tear came to ";
    cout << (*StoryWordManagerPtr).getName() << "'s ";
    cout << (*StoryWordManagerPtr).getBodyPart() << ".\n";
    cout << "After all this time, the quest was finally over. ";
    cout << "And then, the ";
    cout << (*StoryWordManagerPtr).getNoun() << "\n";
    cout << "promptly devoured ";
    cout << (*StoryWordManagerPtr).getName() << ". ";
    cout << "\n\nThe moral of the story?... Be careful what you ";
    cout << (*StoryWordManagerPtr).getVerb();
    cout << " for.";                               
                                 
                                 
}

int main()
{
    StoryWordManager userInput;
    userInput.askText();
    userInput.askText();
    userInput.askText();
    userInput.askText();
    
    userInput.askNum();
    
    StoryWordManager * StoryWordManagerPtr;
    
     MadLib game(StoryWordManager*);
    
    
    return 0;
}

I am not sure how I could truncate this with out eliminating an important detail, and I apologize. I am pretty big on technical etiquette myself(Navy vet and Chem Eng. major), so please do not hesitate to voice any obvious rookie mistakes. No sense in making the same mistake twice.

I dint check the whole code, But here are a few to start out with.

Firstly instead of this

(*StoryWordManagerPtr).getName();

You could use the -> operator.

StoryWordManagerPtr->getName();

Next we come to the main function. and see that you have put down a set of asktext(); functions, What is your intention ? You have already filled out all of the details when you have the constructor being called, so that is prettymuch un-nessesary.

StoryWordManager * StoryWordManagerPtr;

Here you create a pointer and donot assign anything to it.

Secondly you send a temporary variable at this code

MadLib game(StoryWordManager*);

So here is what you will have to do, First.. Use the pointer to point to your userInput object. Then send that pointer into the function.

Member Avatar for jencas

You are calling askText() in the ctor of StoryWordManager as well as in the main() program.

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.