C++ game making help?!?!?!?!

Hi I am currently working on a simple text based game for practice. However, my compiler, Visual C++ Express, keeps giving me an error.
#include "stdafx.h"
#include "iostream"
#include "windows.h"
#include "targetver.h"
#include "string"
using namespace std;
int health=15;
int inventory;
int partylimit;
int jobnum;
string job;
string party1;
string party2;
string jobpartymember;
char q1;
char q2;
char q3;
char q4;
char q5;
char q6;
char q7;
char q8;
char q9;
char q10;
char q11;
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Welcome to World Explorers!\n";
cout<<"In this game, you are a young explorer that wants to have more of an adventurous life\n";
cout<<"In your small village, what job do you have?\n";
cout<<"A. Farmer\n";
cout<<"B. Blacksmith\n";
cout<<"C. Tailor\n";
cout<<"D. I had no job\n";
cin>>q1;
if (q1="a" || "A" || "a." || "A.")
{
job="Farmer";
jobnum=1;
}
else if (q1="b" || "B" || "b." "B.")
{
job="Blacksmith";
jobnum=2;
}
else if (q1="c" || "C" || "c." || "C.")
{
job="tailor";
jobnum=3;
}
else if (q1="d" || "D" || "d." || "D.")
{
job="None";
jobnum=4;
}
cout<<"Good choice of a job.\n";
Sleep(2000);
system("CLS");
cout<<"One night, you decide that it's time to leave. So, you grab a knife and go.\n";
cout<<"You also take a loaf of bread and knapsack to carry it all in.\n";
if (jobnum=1);
{
jobpartymember="Pablo";
else if (jobnum=2);
{
jobpartymember="Mark";
}
}
return 0;
}
The error occurs near the end of the code, where there is the last "else if". Can someone please help tell me what's wrong with it?


There are a couple of lines with an unwanted semicolon which ends the statement too soon.

1
2
3
4
5
6
7
8
if (jobnum=1);
{
jobpartymember="Pablo";
else if (jobnum=2);
{
jobpartymember="Mark";
}
}


should (I think) be :

1
2
3
4
5
6
7
8
if (jobnum=1) 
{
    jobpartymember="Pablo";
    else if (jobnum=2)
    {
        jobpartymember="Mark";
    }
}
Thanks for the help!
Actually, I think it probably should be like this:
1
2
3
4
5
6
7
8
if (jobnum == 1) 
{
    jobpartymember="Pablo";
    else if (jobnum == 2)
    {
        jobpartymember="Mark";
    }
}


Note "==" is used to compare two values. But "=" on its own sets the variable on the left to have the value of the expression on the right. Easily mixed up, but the compiler may issue a warning to get you to double-check such statements.

Other lines are wrong too, such as this one:
if (q1="a" || "A" || "a." || "A.")

Might be better like this:
if (q1=='a' || q1== 'A')
Several different errors there, comparing a single char with a string, doing assignment instead of compare, as well as the "or" part being wrong. The code needs some work :)
Last edited on
In addition to that, the else-if is inside the else brackets
1
2
3
4
5
6
7
8
9
if (jobnum == 1) 
{
    jobpartymember="Pablo";
} // add this here
else if (jobnum == 2)
{
    jobpartymember="Mark";
}
//} remove this bracket 
Dont use global variables, it's a horrible programming practice unless you're placing constants there.

Also, please use code tags and use proper indentation in your code so it is easier to read.
Topic archived. No new replies allowed.