Here is an assignment that i have.
1) 1. Improve the Mad Lib game from Chapter 5 by using references to make
the program more efficient.
Here is the original Mad Lib game.
// Mad-Lib
// Creates a story based on user input
#include <iostream>
#include <string>
using namespace std;
string askText(string prompt);
int askNumber(string prompt);
void tellStory(string name, string noun, int number, string bodyPart, 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(string prompt)
{
string text;
cout << prompt;
cin >> text;
return text;
}
int askNumber(string prompt)
{
int num;
cout << prompt;
cin >> num;
return num;
}
void tellStory(string name, string noun, int number, string bodyPart, 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.";
}
And here is the 1 i worked on.
// Mad-Lib
// Creates a story based on user input
#include <iostream>
#include <string>
using namespace std;
string askText(string prompt);
int askNumber(string prompt);
void tellStory(string& name, string& noun, int number, string& bodyPart, 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;
string& rName = askText("Please enter a name: ");
string noun;
string& rNoun = askText("Please enter a plural noun: ");
int number = askNumber("Please enter a number: ");
string bodyPart;
string& rBodyPart = askText("Please enter a body part: ");
string verb;
string& rVerb = askText("Please enter a verb: ");
tellStory(name, noun, number, bodyPart, verb);
return 0;
}
string askText(string prompt)
{
string text;
cout << prompt;
cin >> text;
return text;
}
int askNumber(string prompt)
{
int num;
cout << prompt;
cin >> num;
return num;
}
void tellStory(string name, string noun, int number, string bodyPart, 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.";
}
Evertime i try to run the program it gives this error.
1>------ Build started: Project: chapter 5, Configuration: Debug Win32 ------
1>Mad lib re-written.obj : error LNK2019: unresolved external symbol "void __cdecl tellStory(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?tellStory@@YAXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0H00@Z) referenced in function _main
1>C:\Users\Twigz\Documents\Visual Studio 2010\Projects\chapter 5\Debug\chapter 5.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
And when i highlight tellStory, it says error: more than one instance of overloaded function "tellStory" matches the argument list.
So i'm not sure whether i was going in the right direction or i'm totally off.
Care to help out?

New Topic/Question
Reply



MultiQuote






|