hey I had this program written without using classes, but I wanted to manipulate it to do the same thing using classes. Everything seemed to be alright until I declared private string arrays in the class and wrote a constructor to initialize them. Now the string array to be initialized doesnt get initialized. I dont know if has to do with any syntax I am missing. Any input is appreciated.
#include <iostream>
#include <string>
using namespace std;
class Madlib
{
private:
string questions[10];
string user_input[10];
public:
Madlib(); //constructor
//Precondition:A set of words and numbers are predetermined to be asked from the user for the madlib.
//Postcondition:The words and number choices to be asked from the user are put into an array.
void set_input(); //mutator funtion
//Precondition:The user has made a choice of words he/she wants to input for the madlib project.
//Postcondition: The word and nuber choices enteres by the user are stored in an array of base type string.
void get_story(); //accessor function
//Precondition: User has entered his/her choices for the madlib project and the choices have been stored in
// an array of type string.
//Postcondition: The madlib story is printed out to the screen with the user-entered input filling in the
// required spaces.
/*for (int i = 0; i < 10; i++)
{
cout << "Please enter a(an) " << questions[i] <<":"<< endl;
getline(cin, user_input[i]);
}
return questions[10];*/
}
void Madlib::set_input()
{
for (int i = 0; i < 10; i++)
{
cout << "Please enter a(an) " << questions[i] <<":"<< endl;
getline(cin, user_input[i]);
}
}
void Madlib::get_story()
{
cout << user_input[0] <<": Welcome! This is \"The Perfect Day Show\"! On this show," << endl
<< "contestants describe how they would spend their perfect day."<< endl
<< "Then the audience votes on whose perfect day sounds like the most fun."<< endl
<< "The contestant with the most votes will win a free trip to "<< user_input[1] <<"," << endl
<< "along with "<< user_input[2] <<" "<< user_input[3] <<"." << endl
<< "Okay, let's get started. Contestant Number One, what is your perfect day?" << endl
<< "Contestant Number One: Well, "<< user_input[0] <<", first I would" << endl
<< "watch "<< user_input[4] <<" "<< user_input[2] <<" times." << endl
<< "Then I would make "<< user_input[5] <<" soup for lunch. In the afternoon," << endl
<< "my friends and I would go "<< user_input[6] << "." << endl
<< user_input[0] <<": Wow! Contestant Number One, that does sound like fun." << endl
<< "Okay, Contestant Number Two?" << endl
<< "Contestant Number Two: First I would put on my "<< user_input[7] << endl
<< user_input[8] <<"."<< endl
<< "Then I would gather all my favorite "<< user_input[3] <<"."<< endl
<< "Together, we would take a trip to the Gray Arrow Adventure Park," << endl
<< "where we would go "<< user_input[6] <<"."<< endl
<< user_input[0] <<": And there you have it, TV audience." << endl
<< "Let's count the votes. It looks like Contestant" << endl
<< "Number Two is our winner. Congratulations!" << endl
<< "Is there anything you would like to say, Contestant Number Two?"<< endl
<< "Contestant Number Two: My "<< user_input[9] <<" is spinning!" <<endl
<< "I've dreamed of going to "<< user_input[1] <<"!" << endl;
}
This line declares and initializes an array of strings local to the constructor. It doesn't initialize the array you already have.
questions doesn't really need to be a class member. You could declare it as const *questions[]={/*...*/}; local to Madlib::set_input(), since it's not used anywhere else.