From: Matt Habel on 16 Jun 2010 16:16 Ok, so I've just recently started programming again with c++. I've been working on a simple text RPG and i need some help with my current project. And also, this is the first time ive posted on this forum so if i post my code wrong or something please tell me. #include<iostream> #include <stdlib.h> #include <time.h> #include<stdio.h> #include"Function.h" #include"globals.h" using namespace std; int main() { {//Start Files bool y = (true); bool n = (false); char character; cout<<"Welcome to the world of Tribuo, would you like to partake on a journey through the wilds of this land?: "; { Funct1: cin>>character; if (character = true); goto Name; if (character = false); return 0; if (character != (true) | (false)); { cout<<"Please enter Y or N: "; goto Funct1; } } } {//Name input Name: char name; cout<<"What is your name?: "; cin>>name; (g_name = name); goto Town1; } {//town 1 Town1: int selection; cout<<"You wake up and find yourself in a town. You dont know what town it is so you walk outside. You notice a sign saying where everything is."; cout<<"Here are your selections."; cout<<"1.Meadow"; cout<<"2.Fields"; cout<<"3.Stone Road"; cout<<"4.Dirt Road"; cout<<"5.General Store"; cout<<"6.Inn"; cout<<"Please enter your selection: "; { cin>>selection; if (selection = 1); goto Meadows; if (selection = 2); goto Fields; if (selection = 3); goto Stoneroad; if (selection = 4); goto Dirtroad; if (selection = 5); goto General1; if (selection = 6); goto Inn; } } { Stoneroad: ; } { Dirtroad: ; } { General1: int select; int gold = g_gold; int price1 = 120; int price2 = 150; cout<<"As you enter the store, the storekeep shows you his wares"; cout<<"You have"<<gold<<" gold"; cout<<"1. Flimsy Sword, "<<price1<<" gold"; cout<<"2. Sturdy Sword, "<<price2<<" gold"; cin>>select; { if (select = 1) g_gold = (g_gold - price1); if (select = 2) g_gold = (g_gold - price2); } } { Inn: ; } { Meadows: { int randNumb; { if (randNumb = 1 | 2 | 3 | 4 | 5); goto meadrat; if (randNumb = 6 | 7 | 8 | 9 | 10); goto gardsnake; if (randNumb = 11 | 12 | 13 | 14 | 15); goto badger; } } { Fields: int randNumb; { if (randNumb = 1 | 2 | 3 | 4 | 5) goto fieldmouse; if (randNumb = 6 | 7 | 8 | 9 | 10) goto deer; if (randNumb = 11 | 12 | 13 | 14 | 15) goto ostrich; } } { meadrat: int hp = 15; int mexp = 3; int select; { cout<<"You encouter a Meadow Rat."; cout<<"1. Attack"; cout<<"2. Flee"; cin>>select; { if (select = 1) int battle; if (select = 2) goto Town1; } } } { gardsnake: int hp; hp = 17; int select; int mexp; mexp = 4; { cout<<"You encouter a Garden Snake."; cout<<"1. Attack"; cout<<"2. Flee"; cin>>select; { if (select = 1) int battle; if (select = 2) goto Town1; } } } { badger: int hp = 20; int mexp = 5; int select; { cout<<"You encouter a Badger."; cout<<"1. Attack"; cout<<"2. Flee"; cin>>select; { if (select = 1) int battle; if (select = 2) goto Town1; } } } { fieldmouse: int hp = 24; int mexp = 6; int select; { cout<<"You encouter a Badger."; cout<<"1. Attack"; cout<<"2. Flee"; cin>>select; { if (select = 1) int battle; if (select = 2) goto Town1; } } } { deer: int hp = 27; int mexp = 8; int select; { cout<<"You encouter a Badger."; cout<<"1. Attack"; cout<<"2. Flee"; cin>>select; { if (select = 1) int battle; if (select = 2) goto Town1; } } } { ostrich: int hp = 32; int mexp = 10; int select; { cout<<"You encouter a Badger."; cout<<"1. Attack"; cout<<"2. Flee"; cin>>select; { if (select = 1) int battle; if (select = 2) goto Town1; } } } } return 0; } Thats my main program. Heres the battle script. #include<iostream> #include<stdlib.h> #include<time.h> #include<stdio.h> #include"globals.h" #include"Function.h" using namespace std; int battle() { srand(time(0)); int php; int hp; int mexp; int str = (g_level + g_watt / 2); int mstr = ((hp / mexp) + mexp) / 2; int damagemin = (str - 2); int damagemax = (str + 2); int mdamagemin = (mstr - 2); int mdamagemax = (mstr + 2); int damage = (rand() % (damagemax-damagemin)) - damagemin; int mdamage = (rand() % (mdamagemax-mdamagemin)) - mdamagemin; float acc = g_acc; float macc = g_macc; acc = (str / hp); macc = (mstr / php); { while (hp > 0) { cout<<"The monster has "<<hp<<" left."; { srand(time(0)); float hit; float perchit = (1-acc); { while(hp > 0) { if (acc > 1) { Dealing_damage: hp = (hp = hp - damage); cout<<"You did"<<damage<<" to the monster!"; } if (acc < 1) { hitcalc: hit = (rand() % (1 - 0)) - 0; if (hit > 1) goto hitcalc; { if (hit > perchit) goto Dealing_damage; if (hit < perchit) cout<<"You missed the monster!"; } } } } return hp; } { while (php > 0) { cout<<"You have"<<php<<" left."; { srand(time(0)); float mhit; float perchmhit = (1-macc); while (php > 0) { if (macc > 1) { D_Damage: php = (php - mdamage); cout<<"You lost "<<mdamage<<" health."; } if (macc < 1) { mhitcalc: mhit = (rand() % (1 - 0)) - 0; { if (mhit > perchmhit) goto D_Damage; if (mhit < perchmhit) cout<<"The monster missed you!"; } } } return php; } } } cout<<"The monster is dead, you gain"<<mexp<<"."; } } } My globals are all in globals.h and my functions are in functions.h #ifndef globals_h #define globals_h #include"globals.cpp" extern int g_pexp = 0; extern int g_level = 1; extern int g_watt = 10; extern int g_gold = 100; extern int g_hp = 10; extern char g_name; extern float g_macc; extern float g_acc; #endif #ifndef functions_h #define functions_h void programs() { int randNumb(); int battle(); } #endif For some reason its returning this error. Error 3 error C2374: 'g_gold' : redefinition; multiple initialization. Could someone please point me in the right way to fixing this? Thank you in advance. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Helge Kruse on 16 Jun 2010 21:05 "Matt Habel" <habelinc(a)gmail.com> wrote in message news:186e84a7-7349-4d02-a9f4-5838629c41bb(a)a42g2000vbl.googlegroups.com... > Ok, so I've just recently started programming again with c++. I've > been working on a simple text RPG and i need some help with my current > project. ..... > My globals are all in globals.h and my functions are in functions.h > #ifndef globals_h > #define globals_h > #include"globals.cpp" > > > extern int g_pexp = 0; > extern int g_level = 1; > extern int g_watt = 10; > extern int g_gold = 100; > extern int g_hp = 10; > extern char g_name; > extern float g_macc; > extern float g_acc; > > > > #endif > #ifndef functions_h > #define functions_h > > void programs() > { > int randNumb(); > int battle(); > > } > #endif > > > For some reason its returning this error. Error 3 error C2374: > 'g_gold' : redefinition; multiple initialization. > > Could someone please point me in the right way to fixing this? > Thank you in advance. Your header files should hold variable declarations like extern int g_gold. One of your cpp implementaion file should hold the variable definition and initialization if appropriate: int g_gold = 100; This way you get _one_ definition as the compiler error message suggests. Helge -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Francis Glassborow on 16 Jun 2010 22:39 Matt Habel wrote: > Ok, so I've just recently started programming again with c++. I've > been working on a simple text RPG and i need some help with my current > project. > And also, this is the first time ive posted on this forum so if i post > my code wrong or something please tell me. OK I do not have time to wade through all your code and some things are a matter of style and others are more serious and some show a lack of understanding of how C++ works. > #include<iostream> > #include <stdlib.h> > #include <time.h> > #include<stdio.h> > #include"Function.h" > #include"globals.h" If you are writing pure C++ I would advise you to use the C++ headers that replace the C ones. E.g. cstdlib rather than stdlib.h > > using namespace std; > > > int main() > { > {//Start Files > > bool y = (true); > bool n = (false); These two variables do not seem to serve any useful purpose. Scrap them > char character; Use a more informative variable name, > > cout<<"Welcome to the world of Tribuo, would you like to partake on a > journey through the wilds of this land?: "; > { Funct1: Drop all these labels. It is not that goto is essentially bad but you need a positive reason for it. > cin>>character; > > if (character = true); You may not believe this but there are at least two errors in that simple expression. As written you are assigning 1 to character (which would be some form of control code on most systems). When you correct the '=' to '==' the result will always be true unless you somehow manage to read in a 'nul' (nul not '0') > goto Name; > > if (character = false); > return 0; Yes you think that somehow n and y as variables relate to the characters 'n' and 'y'. Despite your definition above there is no relationship. > > if (character != (true) | (false)); And here your logic falls apart. You meant to write: if (character != (true) | character != (false)) Note the way the operators bind. > { > cout<<"Please enter Y or N: "; If they did that and you had the previous code correctly checking for 'n' and 'y' you would have a very frustrated user. > goto Funct1; > } So let me rework your code to do what you wanted rather than what you did even though I would not write it that way. int main(){ // Intro and check user wants to proceed cout<<"Welcome to the world of Tribuo, would you like to partake on\n" << "a journey through the wilds of this land? \n" << "(please answer 'y' or 'n' followed by 'ENTER' key): "; while (true) { char answer; cin >> answer; answer = tolower(answer) // force lower case if(answer == 'n'){ cout << "Sorry, you will be missed.\n"; return 0; } if(answer == 'y') break; // exit loop cout << "Press either 'y' or 'n' followed by the return key" // Note that here I would have to insert some clean up code to clear // the input buffer as the user has likely pressed sever more keys } Note that this code has no need for a goto <snip> Check out C++'s select functionality and then make the response to each case a simple function call. Indeed if you get and read my book 'You Can Do It!' you will find a detailed section on writing menus, selection etc. I think that might save you a lot of grief <snip> > > void programs() > { > int randNumb(); > int battle(); > > } I do not know what you think this code does, but I am pretty certain it is not what it actually does (declare two functions that take no arguments and return an int). > #endif > > > For some reason its returning this error. Error 3 error C2374: > 'g_gold' : redefinition; multiple initialization. That is just a symptom of the miscoding that proceeded it. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 16 Jun 2010 22:36 Matt Habel wrote: > And also, this is the first time ive posted on this forum so if i post > my code wrong or something please tell me. You shouldn't post 400 lines of code. Start removing things until you have the smallest possible program that still shows the faulty behaviour. > using namespace std; This is dangerous, as it will everything, even things you may not want here. > bool y = (true); There are two ways to init a variable: bool y(true); bool y = true; Your brackets don't hurt, but they don't give you anything either. > if (character = true); > goto Name; The first line contains an assignment of a bool value to a char value. The result of that expression is the value of the left-hand side, i.e. the new value of "character". You probably want a comparison instead. In any case, the conditionally executed code is an empty statement ";" following the if-clause. The next line is then executed unconditionally. > if (character != (true) | (false)); This won't work either, it must be "if((c != true) || (c != false))". The simple vertical bar is a bitwise or. > extern int g_gold = 100; You can not declare and initialise a variable in a header that is included more than once. Declare it here: extern int g_gold; Define it once elsewhere: int g_gold = -42; Other than that, you program is so large and already contains a bazillion of errors, how about making smaller steps and testing in between? Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Martin B. on 16 Jun 2010 22:37 Matt Habel wrote: > Ok, so I've just recently started programming again with c++. I've > been working on a simple text RPG and i need some help with my current > project. > And also, this is the first time ive posted on this forum so if i post > my code wrong or something please tell me. > (....) > #include"globals.h" > > using namespace std; > > > int main() > { > (....) > > Thats my main program. > > Heres the battle script. > (....) > > My globals are all in globals.h and my functions are in functions.h > #ifndef globals_h > #define globals_h > #include"globals.cpp" What is this? You're includeing globals.cpp in globals.h ?? > > > extern int g_pexp = 0; > extern int g_level = 1; > extern int g_watt = 10; > extern int g_gold = 100; > extern int g_hp = 10; > extern char g_name; > extern float g_macc; > extern float g_acc; > The way extern should be used here is like this: --- globals.h --- extern int g_gold; // just declares the name for // anyone that includes globals.h --- --- globals.cpp --- int g_gold = 100; // defines g_gold and initializes it --- > > > #endif > (....) > > For some reason its returning this error. Error 3 error C2374: > 'g_gold' : redefinition; multiple initialization. > > Could someone please point me in the right way to fixing this? See above. br, Martin -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
|
Next
|
Last
Pages: 1 2 Prev: shared_ptr question Next: template<class... Arg>; variadic "while" //variadic "for"// |