- May 21, 2013
- 3,656
- 60
- 91
I am working on a class project, so this an error of my own making.
I am working on a basic mad-lib maker. The user is presented a menu which contains: two example mad libs, a create your own option, an open from file option, and quit.
After an example is run through (the user actually fills in the requested words), another menu is presented asking if they want the story printed to the screen or to a file. This sub menu is its own function, and it also acts on the user's choice. This function is where I'm running into issues elsewhere.
So, when a user creates a story (enters the prompts they want and the story chunks), I give them the option to run the story right now, or save the template to a file. That sub-menu is its own function as well. The issue I'm having is that if the user chooses to run the story now, I want to able to present the sub-menu from above (print to screen or to file) for consistency. But I can't call that sub menu function from within my current sub menu.
Here's my main.cpp. The interaction I want is to call sub_for_ex() from within sub_for_create()
If I try to include that call, passing just t, I get a compile error that the overloaded function is ambiguous, and I haven't found a way around it.
As it stands, if the user wants to immediately run a story they created, it will just print to screen. I can get around that by replicating code, but the whole point of this course (Intro to OOP) is that replicating code shouldn't be a thing.
I think main.cpp is enough to figure this out, as my issue isn't with my classes or objects directly, but if you guys want to see more of the project, I'll happily upload a tar of my Eclipse project.
Always appreciate the assistance. You guys have helped me a ton in the past.
I am working on a basic mad-lib maker. The user is presented a menu which contains: two example mad libs, a create your own option, an open from file option, and quit.
After an example is run through (the user actually fills in the requested words), another menu is presented asking if they want the story printed to the screen or to a file. This sub menu is its own function, and it also acts on the user's choice. This function is where I'm running into issues elsewhere.
So, when a user creates a story (enters the prompts they want and the story chunks), I give them the option to run the story right now, or save the template to a file. That sub-menu is its own function as well. The issue I'm having is that if the user chooses to run the story now, I want to able to present the sub-menu from above (print to screen or to file) for consistency. But I can't call that sub menu function from within my current sub menu.
Here's my main.cpp. The interaction I want is to call sub_for_ex() from within sub_for_create()
Code:
* main.cpp
#include <iostream>
#include <string>
#include <vector>
#include "main.hpp"
#include "Story.hpp"
#include "space_story.hpp"
#include "medievalstory.hpp"
#include "CreateStory.hpp"
#include "utils.hpp"
using namespace std;
int main (void) {
bool flag = true;
int choice;
string tale;
Story* sStory = new SpaceStory;
Story* mStory = new MedievalStory;
Story* cStory = new CreateStory;
cout << "Welcome to Mad Lib Maker!" << endl;
do {
// Main menu is handled in its own function
choice = show_main_menu();
// The user's choice is handled here
switch (choice) {
case 1:
sStory->get_story();
sub_for_ex_menu(sStory);
break;
case 2:
mStory->get_story();
sub_for_ex_menu(mStory);
break;
case 3:
cStory->get_story();
sub_for_create(cStory);
break;
case 4:
case 5:
flag = false;
break;
}
} while (flag == true);
delete sStory;
delete mStory;
delete cStory;
return 0;
}
int show_main_menu()
{
int choice;
// Menu persists until a valid choice is made
do {
cout << endl << endl;
cout << "***Main menu***" << endl;
cout << " 1. Example: Space Story" << endl;
cout << " 2. Example: Medieval Story" << endl;
cout << " 3. Create Your Own!" << endl;
cout << " 4. Open a Custom Story" << endl;
cout << " 5. Quit" << endl;
choice = get_int("Please enter the number of your choice: ");
// get_int makes sure a number is entered, this makes sure an appropriate number is entered
if (choice < 1 || choice > 5) {
cout << endl << "Please enter a number from 1 to 5." << endl;
}
} while (choice < 1 || choice > 5);
cin.ignore(); // Clear carriage return for getline()
return choice;
}
void sub_for_ex_menu(Story* &s)
{
int choice;
string sto;
string fname;
// Persistent sub-menu after completing an example mad lib
do {
cout << "Would you like the story printed to the screen, or to a file?" << endl;
cout << " 1. Print to screen" << endl;
cout << " 2. Save to file" << endl;
choice = get_int("Please enter the number of your choice: ");
if (choice < 1 || choice > 2) {
cout << "Please enter either 1 or 2." << endl;
}
} while (choice < 1 || choice > 2);
cin.ignore(); // Clear carriage return for getline()
sto = s->make_story();
if (choice == 1) {
// Print story to screen was chosen
s->print_story(sto);
}
else {
// Completed story is saved to a file
cout << "Please enter a file name (NO spaces): ";
getline(cin, fname);
fname = clean_spaces(fname);
s->save_story(fname, sto);
}
}
void sub_for_create(Story* &t)
{
int choice;
string fname, sto;
// Persistent menu run after a Story is created
do {
cout << endl << "Would you like to run the story now, or save it for later?" << endl;
cout << " 1. Run the story now" << endl;
cout << " 2. Save it for later" << endl;
choice = get_int("Please enter the number of your choice: ");
if (choice < 1 || choice > 2) {
cout << "Please select either 1 or 2." << endl;
}
} while (choice < 1 || choice > 2);
cin.ignore(); // Clear carriage return for getline()
if (choice == 1) {
// The words are got and the story is printed to the screen
cout << endl << endl;
t->Story::get_story();
sto = t->make_story();
/*** WANT TO CALL SUB_FOR_EX HERE ***/
t->print_story(sto); // Don't want to use this
}
else {
// A template file is saved that will allow the story to be run later
cout << "Please enter a file name (NO spaces): ";
getline(cin, fname);
fname = clean_spaces(fname);
t->save_story(fname);
}
}
If I try to include that call, passing just t, I get a compile error that the overloaded function is ambiguous, and I haven't found a way around it.
As it stands, if the user wants to immediately run a story they created, it will just print to screen. I can get around that by replicating code, but the whole point of this course (Intro to OOP) is that replicating code shouldn't be a thing.
I think main.cpp is enough to figure this out, as my issue isn't with my classes or objects directly, but if you guys want to see more of the project, I'll happily upload a tar of my Eclipse project.
Always appreciate the assistance. You guys have helped me a ton in the past.