[Acquire help] Case errors

Hi,
first of all as a gentleman i would like to introduce my self a bit. People who are in a hurry may skip these lines. So i'm a regular student who wants to learn c++, and have tried many languages and finally settled down at it. Enough of that:
I have recently began with a text based RPG game but i ran into some errors after some time.
So i think the basic source of error may be the incorrect use of the rand() function. I would like it to generate a number between 1-5 for instance. I have used my old friend the google and found out that i must use some new includes and than divide the random number with rand_max or something. Please can you tell me about this.
Second i couldn't make a function that returns all of my variables. I would like a function that includes all of my variable declarations and than passes them away like public variables.
I may have misunderstood the use of char arrays...

Please tell me what should i and how should i do.

Finally let me copy the errorlog.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1
1>  RPG Game.cpp
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(16): error C2117: 'develop' : array bounds overflow
1>          c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(16) : see declaration of 'develop'
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(17): error C2117: 'fightok' : array bounds overflow
1>          c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(17) : see declaration of 'fightok'
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(28): error C2365: 'develop' : redefinition; previous definition was 'data variable'
1>          c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(16) : see declaration of 'develop'
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(34): error C3861: 'new_game': identifier not found
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(52): error C3861: 'doing': identifier not found
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(60): error C2450: switch expression of type 'char [7]' is illegal
1>          Integral expression required
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(61): error C2051: case expression not constant
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(62): error C2064: term does not evaluate to a function taking 0 arguments
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(64): error C2051: case expression not constant
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(67): error C2059: syntax error : 'default'
1>c:\users\andris\documents\visual studio 2010\projects\rpg game\rpg game\rpg game.cpp(71): warning C4060: switch statement contains no 'case' or 'default' labels
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// RPG Game.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;
// Public variables
int health = 100;
int attack = 3;
int potions = 1;
int hpplus;
int attplus;
int odds;
char answer[7];
char develop[7] = "develop";
char fightok[5] = "fight";
int main_menu;

//Shows off your current bio
int stats()
{
	cout<<"Health: "<<health<<"\nAttack: "<<attack<<"\nNumber of potions: "<<potions<<"\n";
	return 0;
}
//Developing skills
int develop()
{
	hpplus = rand();
	attplus = rand();
	health += hpplus;
	attack += attplus;
	system("PAUSE");
	new_game();
	return 0;
}
//Handles the fighing action. !!!UNFINISHED!!!
int fight()
{
	odds = rand();
	if ( odds == 0 )
		cout<<"You have won!";
	else
		cout<<"OWNED";
	system("PAUSE");
	return 0;
}
//Being called by the welcomescreen by main()
int new_game()
{
	stats();
	doing();
	return 0;
}
// Asks what the user wants to do
int doing()
{
	cout<<"What do you want to do? Fight monsters (type fight) or develop skills (type develop)?";
	cin>> answer;
	switch ( answer ) {
	case develop:
		develop();
		break;
	case fightok:
		fight();
		break;
	case default:
		cout<<"You have mistyped something.\n";
		doing();
		break;
	}
}
// Exit game function. Terminates
int exit_game()
{
	cout<<"Exit Game function was called\n";
	system("PAUSE");
	return 0;
}
// Runs when the game begins, welcomescreenlike
int main()
{
	cout<<"1) - New Game\n";
	cout<<"2) - Exit Game\n";
	cin>>main_menu;
	switch ( main_menu ){
	case 1:
		new_game();
		break;
	case 2:
		exit_game();
		break;
	}
	return 0;
}


Thank you in advance.

(PS: Please forgive me if i made some english mistakes also. It's not my first language.)
Keke.
So i think the basic source of error may be the incorrect use of the rand() function. I would like it to generate a number between 1-5 for instance. I have used my old friend the google and found out that i must use some new includes and than divide the random number with rand_max or something. Please can you tell me about this.

rand() function randoms a number between 0 and rand_max (about 32000). if you divide rand() with rand_max you get a number between 0 and 1. You can easily multiple it.
int smth = (rand() / rand_max) * 5; // number between 0 and 5

some errors are caused by functions that arent defined yet. define them after using namespace std; statement. like this:
int new_game();
so that your compiler knows that they exist :P.

in ur switch statement im not sure if u can use char arrays. also its not case default, just default.

and not sure also but i think u need to make the char array one bigger than their actually size :P. bcus when you use quotation mark the compiler puts a null terminator at the end
Last edited on
you'll need:

1
2
3
#include <time.h>
#include <math.h>
#include <cstdlib> 


for random to work you shoud set a seed in your main, like this:

srand(time(null));

To generate a number from 0 to n-1, use random like this:

rand()%n

The rest of a quocient is always between 0 (number divides evenly) and n-1.
Okey, i got it working finally. No errors, the code runs. But i ran into another problem. More of a bug.

So when i press new game and the it asks me if i would like to fight or develop skills i type either fight or develop i get the error message for misstyping something. Here is the (part of the) code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int doing()
{
	cout<<"What do you want to do? Fight monsters (type fight) or develop skills (type develop)?";
	cin>> answer;
	if (answer == develop)
		development();
	if (answer == fightok)
		fight();
	if (answer != fightok && develop){
		cout<<"You have misstyped something!\n";
		system("PAUSE");
		doing();
	}
	return 0;
}


Of course the question is how do i fix that?

EDIT: srand(time(null)); i should replace the null for the biggest possible value?
Last edited on
No u shouldn't replace it to anything.

srand(time(null))

Just sets a seed to calculate randow numbers based on time. time(null) returns the system time.

Imagine you want numbers from 0 to 5:

int x=rand()%5;

If you want numbers ranging from 1 to 6 then just add one.

You are comparing adresses instead of values. If you want to compare answer and develop, include:

#include <cstring>

if(strcmp(answer,develop)==0)
{...}

However I highly encourage you read some basics, because those errors indicate that you haven't done it yet.

EDIT:

--------------------------------------------//--------------------------------------------------

Approuch 2, simplest way:

Instead of declaring your strings as char[].

include:

#include <string> //NOT string.h

and declare your strings as strings. Then you can use the == operator like you were trying to.

string awnser;
Last edited on
Thank you, it works now.
Okey, i was reading them. But i may have forgotten much. Sorry for newbie question.
No need to apologise, I'm a newbie myself. I post here twice or 3 times a day with problems just like yours... ;)
Topic archived. No new replies allowed.