Beginning c++ Through Game Programming - Stuck with pointer errors!

Hello,

I am fairly new to c++ and am currently working through the book above. I am stuck on Chr 7 Ex 2 "Rewrite the final project from Chapter 5, the Mad Lib game, so that no
string objects are passed to the function that tells the story. Instead, the
function should accept pointers to string objects."

A previous question on this was raised in http://www.cplusplus.com/forum/general/53882/
but my problems seem to be different. I feel like I've searched the errors I get (35 in total, I will list them if requested), but can't find where I'm going wrong.

Many thanks in advance!

The code:

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
// Mad-Lib
// Creates a story based on user input

#include <iostream>
#include <string>
#include "pch.h"

using namespace std;

string askText(string prompt);
int askNumber(string prompt);
void tellStory(string* const pname, string* const pnoun, int* const pnumber, string* const pbodyPart, 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: ");

	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* const pname, string* const pnoun, int* const pnumber, string* const pbodyPart, string* const pverb)
{
	cout << "\nHere's your story:\n";
	cout << "The famous explorer ";
	cout << *pname;
	cout << " had nearly given up a life-long quest to find\n";
	cout << "The Lost City of ";
	cout << *pnoun;
	cout << " when one day, the ";
	cout << *pnoun;
	cout << " found the explorer.\n";
	cout << "Surrounded by ";
	cout << *pnumber;
	cout << " " << *pnoun;
	cout << ", a tear came to ";
	cout << *pname << "'s ";
	cout << *pbodyPart << ".\n";
	cout << "After all this time, the quest was finally over. ";
	cout << "And then, the ";
	cout << *pnoun << "\n";
	cout << "promptly devoured ";
	cout << *pname << ". ";
	cout << "The moral of the story? Be careful what you ";
	cout << *pverb;
	cout << " for.";
}

Last edited on
Your program just compiles and works. So what is the issue?
It must be something wrong I'm doing in visual studio then?

i get the following:

Severity Code Description Project File Line Suppression State
Error C3861 'askNumber': identifier not found ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 21
Error C3861 'askText': identifier not found ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 19
Error C3861 'askText': identifier not found ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 20
Error C3861 'askText': identifier not found ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 22
Error C3861 'askText': identifier not found ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 23
Error C2065 'bodyPart': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 22
Error C2065 'bodyPart': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 25
Error C2065 'cout': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 16
Error C2065 'cout': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 17
Error C2086 'int string': redefinition ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 30
Error C2065 'name': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 19
Error C2065 'name': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 25
Error C2065 'noun': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 20
Error C2065 'noun': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 25
Error C3861 'tellStory': identifier not found ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 25
Error C2065 'verb': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 23
Error C2065 'verb': undeclared identifier ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 25
Error C2447 '{': missing function header (old-style formal list?) ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 31
Error C2447 '{': missing function header (old-style formal list?) ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 39
Error C2447 '{': missing function header (old-style formal list?) ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 47
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 10
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 30
Error C2059 syntax error: 'const' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 12
Error C2059 syntax error: 'const' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 46
Error C2146 syntax error: missing ')' before identifier 'prompt' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 11
Error C2146 syntax error: missing ')' before identifier 'prompt' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 38
Error C2143 syntax error: missing ';' before '{' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 31
Error C2143 syntax error: missing ';' before '{' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 39
Error C2143 syntax error: missing ';' before '{' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 47
Error C2146 syntax error: missing ';' before identifier 'askText' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 10
Error C2146 syntax error: missing ';' before identifier 'askText' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 30
Error C2146 syntax error: missing ';' before identifier 'bodyPart' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 22
Error C2146 syntax error: missing ';' before identifier 'name' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 19
Error C2146 syntax error: missing ';' before identifier 'noun' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 20
Error C2146 syntax error: missing ';' before identifier 'verb' ConsoleApplication3 c:\users\dale\source\repos\consoleapplication3\consoleapplication3\consoleapplication3.cpp 23
Update:

FIXED!
I switched the #include"pch.h" to the top (ie swapped lines 4 and 6) and it worked fine!

Not sure why really, I guess I'll have to look into what that line actually does. For now I've kind of been told to just type it and not worry about it.
"pch.h" is most likely the precompiled header. There is usually no need for it. You should be able to remove it at all and switch off using it in the project settings of visual studio.

If you use it it has to be the first include.
Thank you very much.
Topic archived. No new replies allowed.