console game

it shuts itself down after the first question

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
 #include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{ 
	begin:
	string name;
	string surname;
	int hp = 100;
	int defense = 0;
	int attack = 1;
	int money = 100;
	char arena [10] [10] = {0};
	bool sword = 0; 
	int maxmana = 100;
	int mana = 100;
	bool male;
	int answer;
	int badgood = 0;
	int luck;

	cout << "enter your warriors first name" << endl;
	cin >> name;
	cout << "now their lastname" << endl;
	cin >> surname;
	system("CLS");
	cout << "to begin this game you will be asked some basic questions" << endl;
	cout << "are they a male?" << endl;
	cin >> answer;

	if (answer = 'yes');
	{
	male = "1";
	attack = attack + 1;
	}
	
	if (answer = 'no');
	{	
	male = "0";
	defense = defense + 1;
	}
	
	system("CLS");


	cout << "your enemy falls at your feet and offers his life" << endl;
	cout << "A. you kill him with your trusty sword" << endl;
	cout << "B. you make him your slave" << endl;
	cout << "C. you free him" << endl;

	cin >> answer;

	if (answer = 'A')
	{
		sword = 1;
		badgood = -10;
		attack = attack + 1;
	}
Try using == instead of =, in the if's.
http://www.cplusplus.com/doc/tutorial/operators/

= is the assignment operator
== is the comparison operator
thanks guys
but didn't work
Last edited on
1
2
3
int answer;  // <- answer is declared as an integer
//...
if (answer = 'yes');  // <- 'yes' is not an integer.  It's not even really a string 


technically 'yes' is an integer, which is why the compiler didn't give you an error, but it doesn't mean what you think it does.

If you want to compare it to strings, it needs to be a string.
Also, do not put semicolons after if statements.

What's probably happening is that cin is getting into a bad state because of the type mismatching.

You probably wanted to do this:

1
2
3
4
5
string answer;  // <- you want strings

// ...

if(answer == "yes") // <- note:  double quotes, not single.  And no semicolon 


Make similar changes for everywhere answer is used.
Last edited on
was half asleep last night when I did this thanks
Define please.

Also lets look at your code..
1) you are using goto I am assuming based on line 9.
2) 32 you ask if they are male but you are inputting to an int how is yes/no a number? Either input 1/0 for yes/no or change it to a string on line 21. If you change it to a string then you will not have to change line 34 or 40 too much.
3) Line 34 and 40 are strings not characters so you would put " around it and not '.
4) line 36 male is a boolean and you are trying to assign a string value to it.. If you really want to assign a number to make it true/false you would use 1 for true and 0 for false since booleans are really numbers. It would be best though if you just did male = true; or male = false; since booleans mainly take in true/false.
5) Line 54 you are using an int and trying to input a char. You may want to change the letters to numbers or have a second input variable that will take in the characters which I would recommend so you don't have to change line 56.
6) Some small fixes I would do is change line 17 to false not 0. line 37 , 43 , and 60 you can change to ++varaible; or varaible += 1; instead of variable = variable + 1;

The reason it crashes is because when you put a string where a number is supposed to be in the input buffer it will put the other chracters into the next inputs which will then crash it since you are not clearing/ignoring the failed inputs.

*edit also don't put semicolons after if statements.
**edit 2 looks like disch beat me to that.
***edit 3 also please don't pm me saying "Still won't work." If you're going to pm at least ask a question don't just tell me that my answer to earlier didn't work when in all reality it did work its just your code is a complete mess.
Last edited on
Topic archived. No new replies allowed.