Advertisement Jump to content
  • Advertisement

Ryan Atkins

Member
  • Content Count

    20
  • Joined

  • Last visited

Community Reputation

100 Neutral

About Ryan Atkins

  • Rank
    Member
  1. Ryan Atkins

    Statements in while loop not working

    Thanks for the advice. I really needed to stop having my hand held. I went back, checked everything, and, minus balancing stuff, finished most of the game. Thanks for the help. Hopefully you'll see less post from me.
  2. I figured it out. I'll never be thorough enough. [s]I know there are probably plenty of things in this code unfinished. My current problem is that when ever I defeat the goblin in (on of the) case 1, it unravels my entire, original while(ourHero.getStr()>0) loop and goes to the game over text. I don't get it. Then when ever I go to give myself a 50/50 chance of making my health actually go to 0, it refuses to go to the game over page. Here's a video of exactly what's going on and the code. [media][/media][/s] #include <iostream> #include <string> #include "Character.h" #include "weapon.h" #include <windows.h> #include "functions.h" #include <cstdlib> #include <ctime> using namespace std; character chooseClass(); weapon chooseWeapon1(); void startScreen(); void theGame(); int main() { startScreen(); cout << "Greetings, adventurer! What be thy name? \n"; string name; cin >> name; system("cls"); character ourHero = chooseClass(); cout << "\n\nPlease press enter to continue..."; cin.ignore(); cin.get(); system("cls"); weapon ourWeapon = chooseWeapon1(); cout << "\n\nPlease press enter to continue..."; cin.ignore(); cin.get(); system("cls"); while(ourHero.getHealth()>0) { cout << "Please make a selection:\n\n[1]The Armory\n\n[2]The Pit\n\n[3]Item Shop\n\n[4]Stats\n\n[5]Quit Game\n"; int menuSelect; srand(time(0)); cin >> menuSelect; system("cls"); switch(menuSelect) { case 1: { system("cls"); cout << "Welcome to The Frorth City Armory\n\n"; Sleep(1500); cout << "What would you like to do?\n\n[1]Upgrade Main-hand Weapon\n\n[2]Upgrade Off-hand Weapon\n\n[3]Exit\n"; int choice; cin >> choice; system("cls"); switch(choice) { case 1: { ourWeapon.sharpenWeapon1(); cout << "Main-hand sharpened!"; cin.ignore(); cin.get(); system("cls"); break; } case 2: { if(ourWeapon.getDamageMultiplier2()>0) { ourWeapon.sharpenWeapon2(); cout << "Off-hand sharpened!"; cin.ignore(); cin.get(); system("cls"); break; } else { cout << "No off-hand weapon to upgrade."; cin.ignore(); cin.get(); system("cls"); break; } } case 3: { break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue...\n"; cin.ignore(); cin.get(); system("cls"); } } break; } case 2: { cout << "YOU HAVE ENTERED THE PIT\n\nChoose your opponent:\n\n[1] Goblin\n\n[2] Kobold\n\n[3] Ork \n\n[4] Pit Fighter\n"; int thePitChoice; int combatChoice; int theHit; int currentHealth; character goblin("goblin", 20, 7, 11, 0, 0); cin >> thePitChoice; system("cls"); if(ourHero.getLevel() < 2) switch(thePitChoice) { case 1: { cout << "A goblin enters The Pit.\n"; while(goblin.getHealth()>0) { cout << "Which action would you like to take?\n\n[1]Attack\n\n[2]Beg for mercy\n"; cin >> combatChoice; system("cls"); switch(combatChoice) { case 1: { if((ourHero.getStr()+(rand()%11))>(goblin.getDex()+(rand()%11))) { theHit=ourHero.getStr()*(ourWeapon.getDamageMultiplier1()+ourWeapon.getDamageMultiplier2()); cout << "You have hit the goblin for " << theHit <<"!\n\n"; currentHealth=goblin.getHealth()-theHit; goblin.setHealth(currentHealth); if(0 < goblin.getHealth()) cout << "This message doesn't show up"; cout << "You have slain a goblin!\n\nPlease press enter to continue...\n"; cin.ignore(); cin.get(); system("cls"); ourHero.setHealth(0); break; } else { cout << "The goblin has dodged your swing!"; } if((goblin.getStr()+(rand()%11))>(ourHero.getDex()+(rand()%11))) { theHit=goblin.getStr()*1.5; } else { cout << "You have dodged the goblin's attack!"; } Sleep(2500); system("cls"); break; } case 2: { if((rand()%3) < 2) { cout << "For some odd reason, the goblin allows you to keep your life."; } else { cout << "Poor decision. The goblin executes you!"; Sleep(3000); system("cls"); ourHero.setHealth(0); } break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue...\n"; cin.ignore(); cin.get(); system("cls"); } } } break; } case 2: { cout << "A kobold enters The Pit.\n"; break; } case 3: { cout << "An ork enters The Pit.\n"; break; } case 4: { cout << "You hear a gate from beyond creak open, from within emerging a tall, slim \nfigure.\n\n"; Sleep(2450); cout << "As the figure enters in the mild, torch light you hear a whisper...\n\n"; cout << "Please press enter to continue...\n"; cin.ignore(); cin.get(); system("cls"); cout << "\"PSSSssst! Over hear!\"\n\nYou lean forward and see two beady eyes staring back at you.\n\n"; Sleep(4000); cout << "\"I can help you defeat your enemy... just do what I say.\"\n\nYou are unsure if you trust this " << "voice as you ready yourself for battle.\n\n"; cout << "Please press enter to continue...\n"; cin.ignore(); cin.get(); break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue...\n"; cin.ignore(); cin.get(); system("cls"); } } break; } case 3: { cout << "Out to lunch"; cin.ignore(); cin.get(); system("cls"); break; } case 4: { cout << "Here are the stats for our " << ourHero.getClass() << ", " << name << "." << endl <<endl; cout << "Health: " << ourHero.getHealth() << endl << "Strength: " << ourHero.getStr() << endl << "Dexterity: " << ourHero.getDex() << endl << endl; cout << "Weapon stats for " << ourWeapon.getWeaponName() << "." << endl << endl; cout << "Main-hand Damage: " << ourWeapon.getDamageMultiplier1() << endl << "Off-hand Damage: " << ourWeapon.getDamageMultiplier2(); cin.ignore(); cin.get(); system("cls"); break; } case 5: { cout << "Are you sure you wish to exit Sabra?\n[Y]es?"; cin.ignore(); cin.get(); break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue...\n"; cin.ignore(); cin.get(); system("cls"); } } } if (ourHero.getHealth()<1) cout << "Game Over!\n"; cin.get(); return 0; }
  3. It's odd I overlooked not placing some cin.ingore()/cin.get()s here and there, plus missing those breaks. I'm panicking over the smallest things. This is mostly fixed, minus the functions that obviously need added. My only problem is figuring out how to make the "Exit the game" function work.
  4. I've been posting a lot since I joined this week trying to make a simple text game. I've only been doing C++ a week and a few days, but, even though I feel like I learned a fair amount in that time, maybe instead of trying to create, I should keep learning so I'm better equipped to make what I'm aiming at. I wanted to make the game only to solidify the code I have learned. I'm afraid maybe I am setting bad habits in stone, instead. In this game, I was successful in making and learning how to make classes. I put in a few basic functions in source files. Today, I wanted to add in a menu function the game could be played and adjusted within. The problem is the string's seem to never break. Here is what I did, which is the result of me rethinking how I should be spending my time with C++. within the main.cpp while(ourHero.getHealth()>0) // Our main player(class) { cout << "Please make a selection:\n\n[1]The Armory\n\n[2]The Pit\n\n[3]Item Shop\n\n[4]Quit Game\n"; int menuSelect; cin >> menuSelect; switch(menuSelect) { case 1: { system("cls"); cout << "Welcome to The Frorth City Armory\n\n"; Sleep(1500); cout << "What would you like to do?\n\n[1]Upgrade Main-hand Weapon\n\n[2]Upgrade Off-hand Weapon\n\n[3]Exit\n"; int choice; cin >> choice; switch(choice) { case 1: { ourWeapon.sharpenWeapon1(); system("cls"); cout << "Main-hand sharpened!"; break; } case 2: { ourWeapon.sharpenWeapon2(); system("cls"); cout << "Off-hand sharpened!"; break; } case 3: { break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue\n"; cin.ignore(); cin.get(); system("cls"); } } break; } case 2: { cout << " The Pit is currently closed!"; } case 3: { cout << "Out to lunch"; break; } case 4: { cout << "Are you sure you wish to exit Sabra?\n[Y]es?"; cin.get(); break; } default: { cout << "Invalid selection.\n\n"; cout << "Please press enter to continue\n"; cin.ignore(); cin.get(); system("cls"); } } }
  5. That's a little over my head at this point. I tried directly copying that code. My compiler didn't agree with the readInt command. Is there a head file that might make it work?
  6. I really can't thank you enough. I had actually tried making a separate function but what I didn't know was that I needed to do this: [color=#000000][size=2]character ourHero [color=#666600][size=2]=[color=#000000][size=2] chooseCharacter[color=#666600][size=2]();. Not being able to pull variables out of scopes was holding me back on almost everything I wanted to make. Thanks again! I might as well mention though... I can getting this warning: main.cpp|47|warning: control reaches end of non-void function| Listed line 47 below #include <iostream> #include <string> #include "Character.h" #include "weapon.h" #include <windows.h> #include "functions.h" using namespace std; character chooseClass() { int classChoice; cout << "Which character will you be slaying our foes with?\n" << "1. Fighter\n\n2. Assassin\n\n3. Barbarian\n\n"; cin >> classChoice; switch(classChoice) { case 1: { cout << "\nA fighter! As a fighter you'll be able to wield war axes, battle axes, and" << "... \nwell. Any type of axe you can think up. You'll also be able to wear some of the heavier armor " << "types found here on Anternia."; return character ("fighter", 50, 13, 9); break; } case 2: { cout << "\nSo you're an assassin, eh? As an assassin, you'll have less strength than the" << " \nfighter or barbarian, but what you don't have in might, you make up for in \npercision. You will be able " << "to perform certain feats that your counterparts \ncould only dream of doing."; return character("assassin", 30, 8, 14); break; } case 3: { cout << "Great fury! I've not come across many barbarians, but I've never seen such \nfocused rage in any other." << " As a barbarian, you may not be able to sneak through the window of Prince Krism and steal his Gohl Gem, but " << "you will be able to \nknock down the front door before being dragged off to Frorth City Dungeon."; return character("barbarian", 40, 15, 7); break; } default: cout << "Invalid class selection."; system("cls"); chooseClass(); } } // Line 47 void startScreen(); int main() { startScreen(); cout << "Greetings, adventurer! What be thy name? \n"; string name; cin >> name; void startScreen(); character ourHero = chooseClass(); cout << ourHero.getClass(); cin.get(); return 0; }
  7. Thanks guys, but I've tried that and it still gives me this error: main.cpp|54|error: request for member 'getClass' in 'ourHero', which is of non-class type 'character()'| getClass grabs the string pClass(which is a private member) from the character class. You can actually see I declared it about int main(), but even when it's within int main, it still gives me the same error.
  8. I was trying to figure this out myself, but couldn't. This code compiles just fine, until I remove the // in front of ourHero.getClass... I'm unable to set the values in the switch function apparently. Can I use the switch function while still getting my variables set? I'm positive my class is working fine. #include <iostream> #include <string> #include "Character.h" #include "weapon.h" #include <windows.h> #include "functions.h" using namespace std; character ourHero(); void startScreen(); int main() { startScreen(); cout << "Greetings, adventurer! What be thy name? \n"; string name; cin >> name; void startScreen(); int classChoice; cout << "Which character will you be slaying our foes with?\n" << "1. Fighter\n\n2. Assassin\n\n3. Barbarian\n\n"; cin >> classChoice; switch(classChoice) { case 1: { cout << "\nA fighter! As a fighter you'll be able to wield war axes, battle axes, and" << "... \nwell. Any type of axe you can think up. You'll also be able to wear some of the heavier armor " << "types found here on Anternia."; character ourHero("fighter", 50, 13, 9); break; } case 2: { cout << "\nSo you're an assassin, eh? As an assassin, you'll have less strength than the" << " \nfighter or barbarian, but what you don't have in might, you make up for in \npercision. You will be able " << "to perform certain feats that your counterparts \ncould only dream of doing."; character ourHero("assassin", 30, 8, 14); break; } case 3: { cout << "Great fury! I've not come across many barbarians, but I've never seen such \nfocused rage in any other." << " As a barbarian, you may not be able to sneak through the window of Prince Krism and steal his Gohl Gem, but " << "you will be able to \nknock down the front door before being dragged off to Frorth City Dungeon."; character ourHero("barbarian", 40, 15, 7); break; } default: cout << "Invalid class selection."; } //ourHero.getClass(); cin.get(); return 0; }
  9. C++ is working me over.
  10. Ryan Atkins

    Placing Classes in Separate Files

    I've been watching thenewbostons video tutorials. I feel like he barely touched how to really put a class file in a separate file. I started off learning c++ reading the stuff on learncpp.com. I went back there and got it working perfectly. Here's what I did. I see that both what newbie_gamer and I did work, but is his way a more correct way to do it? Character.h #ifndef CHARACTER_H #define CHARACTER_H class height { public: height(int f, int g); void setHigh(int h); void setLow(int l); int getLow() { return low; } int getHigh() { return high; } private: int low; int high; }; #endif // CHARACTER_H Character.cpp #include <iostream> #include <string> #include "Character.h" using namespace std; height::height(int f, int g) { setHigh(f); setLow(g); } void height::setHigh(int h) { high = h; } void height::setLow(int l) { low = l; }
  11. Ryan Atkins

    Placing Classes in Separate Files

    I saw you didn't have a public or private section in the class. Am I suppose to leave that out? Here's my new list of errors, followed by my current code. Character.cpp||In constructor 'height::height(int, int)':| Character.cpp|14|error: expected primary-expression before 'public'| Character.cpp|14|error: expected ';' before 'public'| Character.cpp|16|error: a function-definition is not allowed here before '{' token| Character.cpp|20|error: a function-definition is not allowed here before '{' token| Character.cpp|24|error: a function-definition is not allowed here before '{' token| Character.cpp|28|error: a function-definition is not allowed here before '{' token| Character.cpp|31|error: expected primary-expression before 'private'| Character.cpp|31|error: expected ';' before 'private'| Character.cpp|33|warning: unused variable 'high'| ||=== Build finished: 8 errors, 1 warnings ===| #include <iostream> #include <string> #include "Character.h" using namespace std; character.cpp height::height(int f, int g) { { setHigh(f); setLow(g); } public; void setHigh(int h) { high = h; } void setLow(int l) { low = l; } int getLow() { return low; } int getHigh() { return high; } private: int low; int high; } Character.h #ifndef CHARACTER_H #define CHARACTER_H class height { public: height(int f, int g); void setHigh(int h); void setLow(int l); int getLow(); int getHigh(); private: int low; int high;}; #endif // CHARACTER_H
  12. I felt like I understood classes enough to start placing them in separate files, so I watched a tutorial, but it didn't go as far as adding the public and private parts. I can get as far as adding a simple cout << "whatever"; but once I actually try to make a functional class, I get these errors... Character.cpp|8|error: expected ')' before ',' token| Character.cpp|8|error: expected initializer before ')' token| ||=== Build finished: 2 errors, 0 warnings ===| What am I doing wrong? character.h #ifndef CHARACTER_H #define CHARACTER_H class hieght { public: hieght(); }; #endif // CHARACTER_H Character.cpp #include <iostream> #include <string> #include "Character.h" using namespace std; hieght::hieght(f, g) // error is pointing here { setHigh(f); setLow(g); public; setHigh(h) { high = h; } setLow(l) { low = l; } int getLow() { return low; } int getHigh() { return high; } private: int low; int high; }
  13. Ryan Atkins

    Never ending loops and repeating functions!

    How would you go about making [color=#000000][size=2]player ourHero [color=#666600][size=2]= [color=#000000][size=2] chooseClass [color=#666600][size=2]() without calling the function again? I normally do the braces with the if statements. Not sure why I missed them there, unless I was waiting to add the other 2 classes. Will add them. Thanks!
  14. So I rewrote this nearly 5 times from scratch every time and this is my most successful one yet. One of my two problems, if you don't count having all my code in the main.cpp file, is that my default on the switch() is neverendingly repeated. My other problem is that player chooseClass repeats twice. Once in the main() and then once below where it was written. I placed it there because it's the only place I got no errors or warnings while setting it to = player ourHero. Any suggestions would be great, even if it's not on what I mentioned. I first tried using strings, but I obviously have yet to grasp that concept. #include <iostream> #include <string> using namespace std; struct player { player(const string playerClass, int health, int str, int dex, int cha) : playerClass(playerClass), health(health), str(str), dex(dex), cha(cha){} string playerClass; int health; int str; int dex; int cha; }; struct item { item(const string itemName, string modifier, float multiplier) : itemName(itemName), modifier(modifier), multiplier(multiplier) {} string itemName; string modifier; float multiplier; }; player chooseClass() { cout << "\nWhich class would you like to play?\n" << endl << "1. Fighter\n" << endl << "2. Assassin\n" << endl << "3. Barbarian\n" << endl; char playerSelect; cin >> playerSelect; while(true) { switch(playerSelect) { case '1': return player("Fighter", 50, 13, 9, 7); case '2': return player("Assassin", 30, 7, 13, 8); case '3': return player("Barbarian", 40, 14, 11, 4); default: cout << "Invalid selection. Please choose again..." << endl << "1. Fighter\n" << endl << // If this happens, it never ends. "2. Assassin\n" << endl << "3. Barbarian\n" << endl; } } } player ourHero = chooseClass(); // I believe this is my repeating function problem item chooseWeapon() { cout << "In Anternia, there are many, many valuable, rare, powerful weapons. However, this is not what you will have.\n" << "Press any key to continue." << endl; cin.ignore(); cin.get(); if(ourHero.playerClass == "Fighter") cout << "As a fighter you must harness your strength behind your weapon, but also remain agile enough to slice the throat" << " of your enemies.\n" << endl << "Press any key to continue." << endl; cin.get(); cout << "Here is your miltia short sword."; return item("militia short sword", "str", 1); } int main() { cout << "Welcome to Anternia. Please enter your name: "; string playerName; cin >> playerName; cout << "\nI don't think I've heard that one before.\n" << endl << "Are you male or female?\n" << endl << "1. Male\n" << endl << "2. Female\n" << endl; string sex; cin >> sex; chooseClass(); chooseWeapon(); return 0; }
  15. Ryan Atkins

    Can't get my if statements working in a function

    Thank you both. I'm going to spend my night figuring out classes.
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!