The assignment was to take the existing code and change all strings passed or returned by all the functions to be passed and returned as string pointers instead of by value. This is my first assignment using pointers. I found others who have posted on here about this and with almost the same exact code got it to work?
When I try to compile it in Visual Studio 2015 I get the following 2 errors:
LNK2001 unresolved external symbol "void __cdecl ptellStory(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const *,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const *)" (?ptellStory@@YAXPBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0H00@Z) ConsoleApplication1 F:\C++\ConsoleApplication1\ConsoleApplication1\mad-lib.obj 1
and
LNK1120 1 unresolved externals ConsoleApplication1 F:\C++\ConsoleApplication1\Release\ConsoleApplication1.exe 1
This is the code you start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
// 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.";
}
|
Here is the changes I have made:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
// Mad-Lib
// Creates a story based on user input
// rewrite mablib game using pointers for the strings
#include <iostream>
#include <string>
using namespace std;
string askText(string prompt);
int askNumber(string prompt);
// declare pointers for the strings and change to constants
void ptellStory(string const *pNAME, string const *pNOUN, int number, string const *pBODY_PART, string const *pVERB);
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: ");
ptellStory(&name, &noun, number, &bodyPart, &verb); // point to the addresses of the strings
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 const *pNAME, string const *pNOUN, int number, string const *pBODY_PART, string const *pVERB)
{
cout << "\nHere's your story:\n";
cout << "The famous explorer ";
cout << *pNAME; // dereference pointer
cout << " had nearly given up a life-long quest to find\n";
cout << "The Lost City of ";
cout << *pNOUN; // dereference pointer
cout << " when one day, the ";
cout << *pNOUN; // dereference pointer
cout << " found the explorer.\n";
cout << "Surrounded by ";
cout << number;
cout << " " << pNOUN; // dereference pointer
cout << ", a tear came to ";
cout << *pNAME << "'s "; // dereference pointer
cout << *pBODY_PART << ".\n"; // dereference pointer
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << *pNOUN << "\n"; // dereference pointer
cout << "promptly devoured ";
cout << *pNAME << ". "; // dereference pointer
cout << "The moral of the story? Be careful what you ";
cout << *pVERB; // dereference pointer
cout << " for.";
}
|