What's Wrong With My Code?

I just started making a text-based RPG zombie game. I am getting strange errors that make no sense to me. I get the errors with Visual C++ Express 2012 and Visual Studio 2012 Express. Here's my 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
#include <iostream>
#include "stdafx.h"
#include "windows.h"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string gender;
string name;
int health;
string weapon;
char choice1;
char choice2;
char choice3;
char choice4;
char choice5;
char choice6;
char choice7;
char choice8;
char choice9;
char choice10;
char choice11;
char choice12;
char choice13;
char choice14;
char choice15;
char choice16;
char choice17;
char choice18;
char choice19;
char choice20;
cout<<"Welcome to Zombie Horde Survival!  In this world, zombies have infected\n";
cout<<"the entire world, aside from certain few survivors.  You are one of them.\n";
Sleep(4000);
system("CLS");
cout<<"Before you set off to survive, let's learn a bit about you.\n";
cout<<"Are you\n";
cout<<"A. Male or\n";
cout<<"B. Female?\n";
cin>>choice1;
if (choice1="a" || "A")
{
gender="male";
}
else if (choice1="b" || "B")
{
gender="female";
}
else 
{
cout<<"That is not a valid choice.  Please try again.\n";
cin>>choice1;
if (choice1="a" || "A")
{
gender="male";
}
else if (choice1="b" || "B")
{
gender="female";
}
cout<<"Now that we've got that settled, what's is your name?  If you have played before and have a \n";
cout<<"spawn code, you can type it here as well.\n";
cin>>name;
	return 0;
}


Here's my errors:

1>------ Build started: Project: Zombie Horde Survival Game, Configuration: Debug Win32 ------
1> Zombie Horde Survival Game.cpp
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(32): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(33): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(36): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(37): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(38): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(39): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(40): error C2065: 'cin' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(51): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(52): error C2065: 'cin' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(61): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(62): error C2065: 'cout' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(63): error C2065: 'cin' : undeclared identifier
1>c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(67): fatal error C1075: end of file found before the left brace '{' at 'c:\users\michael\documents\visual studio 2010\projects\zombie horde survival game\zombie horde survival game\zombie horde survival game.cpp(7)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Please tell me why this won't compile. Thanks in advance!
When using precompiled headers (#incude "stdafx.h") you must include stdafx.h before anything else.
Hi michael,

Change:
1
2
3
4
#include <iostream>
#include "stdafx.h"
#include "windows.h"
#include "string" 

to
1
2
3
4
#include "stdafx.h"
#include <iostream>
#include "windows.h"
#include "string" 


stdafx.h is a pre-compiled header which is created by default in VS if you don't manually select "Empty Project" when you create your project. It must come first.

If you want to remove it all togeather, Right-click on your project, select properties then go to: Configuration Properties > C/C++ > Precompiled Headers and select "Not using precompiled headers" in the dropdown menu to the right of the Precompiled Header field. Then remove that include.
Last edited on
It's a good thing you posted now before you got too deep into this.

First, the simple problem. There's a closing brace missing for the last if/else.

The includes should use angle brackets, like this:
1
2
3
#include <iostream>
#include <windows.h>
#include <string> 


Whenever you see a series of variables like this, it is almost certainly better done using an array
1
2
3
4
5
6
7
char choice1;
char choice2;
char choice3;

...
char choice19;
char choice20;


Use this instead:
char choices[20];

This and similar lines is wrong in more than one way:
if (choice1="a" || "A")

Firstly, the = operator is used to assign the value on the right to the variable on the left. What you really need here is the equality comparison operator ==.

The variable choice1 is of type char. You are trying to compare it with a string "a". That should be a char 'a' instead.

In addition, the use of the || operator is incorrect. It should look like this:
if (choice1=='a' || choice1=='A')

though if you were to use an array (not sure whether it is even necessary here), that code would look like this:
if (choices[0]=='a' || choices[0]=='A')

That's quite a lot of issues in just a short program. I'd be wary of taking on a major project until you have a better grasp of the basics. Just my opinion of course.
Last edited on
Thanks! That seemed to solve my problems! Sorry if it was a lot of errors, but I am just getting back to C++ after working in HTML for a while!
Topic archived. No new replies allowed.