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 ==========
// RPG Game.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
usingnamespace 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;
casedefault:
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.)
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
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?