# selling weapons

This topic is 3481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am making a dungeon game like crawl but in text instead of ascii, and I need a question. First off, I'm new here, and I'm wondering if this site is C++ at all, but I am moderately good at C++, but I want to know, with this code (Only the part I'm wondering about, much more to my game) How can I make it so I can sell weapons back to the shop?" (The //if ?'s are so I know when they begin and end.: void shops1() {//shops1 system("PAUSE"); system("CLS"); cout << "As you enter the group of shops, you spot signs next to them." << endl; cout << "There is a weapon shop, a shield shop, a potion shop, and a magic shop. What do you do?" << endl; cout << "1)Weapon Shop" << endl; cout << "2)Shield Shop" << endl; cout << "3)Potion Shop" << endl; cout << "4)Magic Shop" << endl; cout << "5)Leave" << endl; cin >> HEROCHOICE; if (HEROCHOICE == 1) {//if 1 cout << "You enter the weapon's stall, and a short and chubby man greets you. 'Hello there sir. I have weapons for sale, what will it be then?" << endl; cout << "1)Buy Weapons" << endl; cout << "2)Sell Weapons" << endl; cout << "3)Not Interested" << endl; cin >> HEROCHOICE; if (HEROCHOICE == 1) {//if 2 cout << "'Well then, please look at my wares.'" << endl; system("PAUSE"); cout << "1)Bronze Shortsword = 50g" << endl; cout << "2)Bronze Longsword = 100g" << endl; cout << "3)Iron Rapier = 250g" << endl; cout << "4)Iron Mace = 260g" << endl; cout << "5)Wooden Short Bow = 90g" << endl; cout << "6)Wooden Long Bow = 150g" << endl; cout << "7)Jagged-Toothed Saber = 350g" << endl; cout << "8)Never Mind" << endl; cin >> HEROCHOICE; }//if 2 end else if (HEROCHOICE == 2) {//if 3 cout << "'Well then, lets see what you got'" << endl; system("PAUSE"); cout << "1)Bronze Shortsword = 25g" << endl; cout << "2)Bronze Longsword = 45g" << endl; cout << "3)Iron Rapier = 150g" << endl; cout << "4)Iron Mace = 160g" << endl; cout << "5)Wooden Short Bow = 30g" << endl; cout << "6)Wooden Long Bow = 75g" << endl; cout << "7)Jagged-Toothed Saber = 245g" << endl; cout << "8)Never Mind" << endl; cin >> HEROCHOICE; }//if 3 end else if (HEROCHOICE == 2) {//if 4 cout << "'Ok then, come again soon!'" << endl; }//if 4 end }//if 1 end }//shops1 end : hope you can help, and that its not too confusing. All this function isnt done yet either, I'm just trying to figure out how I could sell weapons. [Edited by - MAGIgullorks on August 14, 2008 3:49:39 PM]

##### Share on other sites
Otherwise, here is my whole game so far (There are plenty of spelling errors, but I'll fix that near the end. This is the whole thing after I made sure there were no errors:

#include <iostream>
#include <stdio.h>
using namespace std;
char HERONAME[10];
int HEROCHOICE;
int CURRENTROOM;
int MONSTERCHANCE[50];
int TREASURECHEST[25];
int HEROGOLD;
int HEROPOTION[5];
int BEENHERE;

/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

void shops1()
{//shops1
system("PAUSE");
system("CLS");
cout << "As you enter the group of shops, you spot signs next to them." << endl;
cout << "There is a weapon shop, a shield shop, a potion shop, and a magic shop. What do you do?" << endl;
cout << "1)Weapon Shop" << endl;
cout << "2)Shield Shop" << endl;
cout << "3)Potion Shop" << endl;
cout << "4)Magic Shop" << endl;
cout << "5)Leave" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 1
cout << "You enter the weapon's stall, and a short and chubby man greets you. 'Hello there sir. I have weapons for sale, what will it be then?" << endl;
cout << "1)Buy Weapons" << endl;
cout << "2)Sell Weapons" << endl;
cout << "3)Not Interested" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 2
cout << "'Well then, please look at my wares.'" << endl;
system("PAUSE");
cout << "1)Bronze Shortsword = 50g" << endl;
cout << "2)Bronze Longsword = 100g" << endl;
cout << "3)Iron Rapier = 250g" << endl;
cout << "4)Iron Mace = 260g" << endl;
cout << "5)Wooden Short Bow = 90g" << endl;
cout << "6)Wooden Long Bow = 150g" << endl;
cout << "7)Jagged-Toothed Saber = 350g" << endl;
cout << "8)Never Mind" << endl;
cin >> HEROCHOICE;
}//if 2 end
else if (HEROCHOICE == 2)
{//if 3
cout << "'Well then, lets see what you got'" << endl;
system("PAUSE");
cout << "1)Bronze Shortsword = 25g" << endl;
cout << "2)Bronze Longsword = 45g" << endl;
cout << "3)Iron Rapier = 150g" << endl;
cout << "4)Iron Mace = 160g" << endl;
cout << "5)Wooden Short Bow = 30g" << endl;
cout << "6)Wooden Long Bow = 75g" << endl;
cout << "7)Jagged-Toothed Saber = 245g" << endl;
cout << "8)Never Mind" << endl;
cin >> HEROCHOICE;
}//if 3 end
else if (HEROCHOICE == 2)
{//if 4
cout << "'K then, come again soon!'" << endl;
}//if 4 end
}//if 1 end
}//shops1 end

/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

void inn1()
{//inn1
}//inn1 end

/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

void MONSTERCHANCER()
{//MONSTERCHANCER
if (MONSTERCHANCE[0] == 1)
{//if 1
cout << "Suddenly a giant rat appears!" << endl;
}//if 1 end
else if (MONSTERCHANCE[1] == 1)
{//if 2
cout << "Suddenly a tarantula appears!" << endl;
}//if 2 end
else if (MONSTERCHANCE[2] == 1)
{//if 3
cout << "Suddenly a ratling soldier appears!" << endl;
}//if 3 end
return;
}//MONSTERCHANCER end

/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

void start()
{//start
system("COLOR 1E");
cout << "<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>";
cout << "<^> You are in your room, slowly falling into a hazy sleep. As your vision blurs, a voice <^>";
cout << "<^> whipers loudly, 'I SEE YOU'. You feel a black curtain shut on your eyes, and you open <^>";
cout << "<^> them, startled. You find yourself in a strange room, stone walls all around you. You <^>";
cout << "<^> quickly find your footing, and stand up immeaditly to find a small dusty scroll in the <^>";
cout << "<^> corner of the tiny room. You open it, reading, 'MORTAL, I HAVE A MISSION FOR YOU, THE <^>";
cout << "<^> DEMON OF THE ABYSS HAS BEGUN HIS DESTRUCTION ON EARTH, AND YOU ARE THE ONLY ONE WHO CAN <^>";
cout << "<^> STOP HIM. FIND THE OBISDIAN SHIELD OF THE ANCIENTS, AND DESTROY HIM. I KNOW YOU CAN DO <^>";
cout << "<^>IT, NOW GO!'. You find a small line on the bottom, with the word NAME and magically find <^>";
cout << "<^> a quilled pen with just enough ink on it in your pocket. What do you write on the line? <^>";
cout << "<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>^<^>";
cin >> HERONAME;
cout << HERONAME << endl;
cout << "__________" << endl;
cout << "And suddenly, the note dissapears into a fine puff of dust, and is gone. Next to you, you find a golden box, and inside is a small dagger and a silk drawstring bag with gold discs in it. A note reads, 'Here is 150 gold coins for your journey, young hero.'" << endl;
cout << " N " << endl;
cout << "E+W" << endl;
cout << " S " << endl;
system("PAUSE");
system("CLS");
return;
}//start end

/*-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------*/

int main()
{//main
start();
srand(time(NULL));
CURRENTROOM = 1;
TREASURECHEST[0] = 1;
TREASURECHEST[1] = 1;
TREASURECHEST[2] = 1;
TREASURECHEST[3] = 1;
HEROPOTION[0] = 0;
HEROPOTION[1] = 0;
HEROPOTION[2] = 0;
HEROPOTION[3] = 0;
HEROPOTION[4] = 0;
HEROGOLD = 150;
for(;;)
{//main loop
system("PAUSE");
system("CLS");
if (CURRENTROOM == 1)
{//if 1
cout << "You are in a musty stone room. Suddenly a voice comes from inside your head, 'Young one, dont be afraid, go on your journey and destroy the Demon of the Abyss!'" << endl;
cout << "There is a door to the west, what do you do?" << endl;
cout << "1)Go through the doorway to the west" << endl;
cout << "2)Stand and wait" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 2
cout << "You squeeze yourself through the tiny doorway and go into the next room." << endl;
CURRENTROOM = 2;
}//if 2 end
else if (HEROCHOICE == 2)
{//if 3
cout << "You just stand in place and do nothing." << endl;
}//if 3 end
}//if 1 end
else if (CURRENTROOM == 2)
{//if 4
cout << "The room is pretty long and dark, except for a floating red orb that illuminates the room" << endl;
cout << "You can just make out a door outline to the east, west, and way in the back to the south, the east one you came from. What do you do?" << endl;
cout << "1)Go through the doorway to the west" << endl;
cout << "2)Go through the doorway to the east" << endl;
cout << "3)Go to the door to the back of the room in the south" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 5
cout << "You walk through the door to the west" << endl;
CURRENTROOM = 3;
}//if 5 end
else if (HEROCHOICE == 2)
{//if 6
cout << "You walk back to the first room" << endl;
CURRENTROOM = 1;
}//if 6 end
else if (HEROCHOICE == 3)
{//if 7
cout << "You walk quickly to the far side of the room and go throught the doorway" << endl;
CURRENTROOM = 4;
}//if 7 end
}//if 4 end
else if (CURRENTROOM == 3)
{//if 8
cout << "This room is tiny and claustrophobic. There is just one tiny torch on the wall to the right." << endl;
cout << "There is a door from where your came from and a small treasure chest next to you. What do you do?" << endl;
cout << "1)Try and open the treasure chest" << endl;
cout << "2)Go back out the door" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1 && TREASURECHEST[0] == 1)
{//if 9
cout << "You pry open the treasure chest." << endl;
MONSTERCHANCE[0] = rand() % 2 + 1;
MONSTERCHANCER();
TREASURECHEST[0] = 0;
cout << "You find 50 gold and a blue potion!" << endl;
HEROGOLD = HEROGOLD + 50;
HEROPOTION[0]++;
}//if 9 end
else if (HEROCHOICE == 1 && TREASURECHEST[0] != 1)
{//if 10
cout << "You pry open the treasure chest." << endl;
cout << "It was empty." << endl;
}//if 10 end
else if (HEROCHOICE == 2)
{//if 11
cout << "You exit the room" << endl;
CURRENTROOM = 2;
}//if 11 end
}//if 8 end
else if (CURRENTROOM == 4)
{//if 12
cout << "This room is extremely long, but bright, and has a large hallwayish feel." << endl;
cout << " A door and a treasure chest are to the northwest. There is also a door behind you to the north. What do you do?" << endl;
cout << "1)Go back throught the door you came from" << endl;
cout << "2)Go to the treasure chest way in the corner and try to open it" << endl;
cout << "3)Go through the door way to the northwest by the treasure chest" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 13
cout << "You exit the room" << endl;
CURRENTROOM = 2;
}//if 13 end
else if (HEROCHOICE == 2 && TREASURECHEST[1] == 1)
{//if 14
cout << "You pry open the treasure chest." << endl;
MONSTERCHANCE[1] = rand() % 2 + 1;
MONSTERCHANCER();
TREASURECHEST[1] = 0;
cout << "You find 25 gold!" << endl;
HEROGOLD = HEROGOLD + 25;
}//if 14 end
else if (HEROCHOICE == 2 && TREASURECHEST[1] != 1)
{//if 15
cout << "You pry open the treasure chest." << endl;
cout << "It was empty." << endl;
}//if 15 end
else if (HEROCHOICE == 3)
{//if 16
cout << "You head to the door and go through it." << endl;
CURRENTROOM = 5;
}//if 16 end
}//if 12 end
else if (CURRENTROOM == 5)
{//if 17
cout << "This room is about the same size as the first one, but with a bookcase covered in cobwebs and barely any lighting." << endl;
cout << "There is a narrow staircase going downwards, and the door you came from behind you. What do you do?" << endl;
cout << "1)Go back through the doorway" << endl;
cout << "2)Go down the staircase" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 18
cout << "You go back through the door" << endl;
CURRENTROOM = 4;
}//if 18 end
else if (HEROCHOICE == 2)
{//if 19
cout << "You barely manage to squeeze through the narrow stairway leading downwards." << endl;
CURRENTROOM = 6;
}//if 20 end
}//if 17 end
else if (CURRENTROOM == 6)
{//if 21
cout << "You are in a extremely dark room with bricks layin all over the ground." << endl;
cout << "There is a narrow staircase leading upwards, and a door right to the west of you. What do you do?" << endl;
cout << "1)Go back up the staircase" << endl;
cout << "2)Go through the door" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 22
cout << "You walk up the staircase." << endl;
CURRENTROOM = 5;
}//if 22 end
else if (HEROCHOICE == 2)
{//if 23
cout << "You go through the doorway." << endl;
CURRENTROOM = 7;
}//if 23 end
}//if 21 end
else if (CURRENTROOM == 7)
{//if 24
cout << "The room smells of decay, and a rat squeaks as it scampers over your foot." << endl;
cout << "There is a door to the east where you came from, a treasure chest way in the front of the room, and another door in the northeast of the room by the treasure chest. What do you do?" << endl;
cout << "1)Go back through the door you came from" << endl;
cout << "2)Open the treasure chest" << endl;
cout << "3)Go through the door by the chest" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 25
cout << "You go back through the door" << endl;
CURRENTROOM = 6;
}//if 25 end
else if (HEROCHOICE == 2 && TREASURECHEST[2] == 1)
{//if 26
cout << "You pry open the treasure chest." << endl;
MONSTERCHANCE[2] = rand() % 2 + 1;
MONSTERCHANCER();
TREASURECHEST[2] = 0;
cout << "You find 55 gold!" << endl;
HEROGOLD = HEROGOLD + 55;
}//if 26 end
else if (HEROCHOICE == 2 && TREASURECHEST[2] != 1)
{//if 27
cout << "You pry open the treasure chest." << endl;
cout << "It was empty." << endl;
}//if 27 end
else if (HEROCHOICE == 3)
{//if 28
cout << "You pass the chest and go through the door next to it." << endl;
CURRENTROOM = 8;
}//if 28 end
}//if 24 end
else if (CURRENTROOM == 8)
{//if 29
cout << "The room is about a doorway wide, but quite long. It is completely empty." << endl;
cout << "There is a door you came from to the west, and loud shouts and voices coming from the door to the east. What do you do?" << endl;
cout << "1)Go back through the door behind you" << endl;
cout << "2)Go through the door to the east with shouts from behind it" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 30
cout << "You go back through the door behind you." << endl;
CURRENTROOM = 7;
}//if 30 end
else if (HEROCHOICE == 2)
{//if 31
cout << "You go through the door to the east." << endl;
CURRENTROOM = 9;
}//if 31 end
}//if 29 end
else if (CURRENTROOM == 9)
{//if 32
cout << "This place is humongous, a large plaza, with rough, sooty people everywhere and a dirty fountain in the middle." << endl;
cout << "There is a door from where you came behind you, a big group of stalls to the east, a large staircase way in the south, and to the southeast is a good sized old-looking building with an Inn sign on it. What do you do?" << endl;
cout << "1)Go back through the door you came from" << endl;
cout << "2)Enter the cluster of shops to the east" << endl;
cout << "3)Go into the Inn to the southeast" << endl;
cout << "4)Go down the staircase to the south" << endl;
cin >> HEROCHOICE;
if (HEROCHOICE == 1)
{//if 33
cout << "You exit the noisy plaza and go through the door you came from." << endl;
CURRENTROOM = 8;
}//if 33 end
else if (HEROCHOICE == 2)
{//if 34
cout << "You enter the cluster of small shops." << endl;
shops1();
}//if 34 end
else if (HEROCHOICE == 3)
{//if 35
cout << "You enter the large Inn to the southeast." << endl;
inn1();
}//if 35 end
else if (HEROCHOICE == 4)
{//if 36
cout << "You descend the staircase to the south." << endl;
CURRENTROOM = 10;
}//if 36 end
}//if 32 end
}//main loop end
}//main end

##### Share on other sites
This site focuses on all aspects of game development (programming includes multiple languages such as C++, C#, Java, etc...). This specific forum is focused on aspects of general game programming which, to my understanding, you could post in any language as long as it pertains to game programming.

Your code seems quite static and runs almost straight through. What I would do, is create classes for general objects like a 'Shop' class or a 'Vendor' class. That class would then have the functions you would need to sell or buy including a list of general items. I would probably then create an instantiated class (although these could be put in the actual parent class) that held the vendor information such as the description as you walked in the door, the vendor name, any of those vendor stats. With that, you can create objects of the instantiated class and populate them with data. So instead of having like 10 methods of Shop1(), Shop2(), Shop3()... You would have objects of:

oShop2.PromptSell();
oShop3.PrintDescription();

To sell in your current code, you would need to check the user input, then check to make sure they have that item, then remove the item from their inventory, and add to their gold.

##### Share on other sites
Oh, I forgot to ask one more question. My whole main loops, as shown, so should I put a getchar() and if it is say m, it opens a menu that shows your attack, defense, health, exp, and inventory? Is that what getchar does??? And if I did, would I do something like this:

int keypress = GEtchar();
if (keypress == '(M = integer?)')
{
}

?

##### Share on other sites
I think what programmermattc suggested is your best bet. If you're just starting to program, I would highly recommend you learn about classes and Object Oriented programming soon (in my experience, learning to code the wrong way, and then later trying to adapt to the right way is much more difficult). Also, a small tip to help you in your current program: It looks like you might have better luck if you cleaned up your code so it's not so confusing. I see a whole lot of IF statements in your code, see this page http://www.cplusplus.com/doc/tutorial/control.html and look at the bottom under "switch statements".

Hope this helps!

EDIT:

To address your last post, I wouldn't even use GetChar() with your current design. The way I would set it up would be with a prompt ever "turn" that asks the user what they want to do, then list some options and use std::cin to act on that input. Something like this:

 //somewhere in your main loopint choice;cout << "What would you like to do?\n";cout << "1) Go to town and shop\n";cout << "2) Go to forest and fight\n";cout << "3) Open inventory\n";cin >> choice;switch(choice) {case 1:     DisplayTownShopsMenu();     break;case 2:     GoToForest();     break;case 3:     DisplayInventory();     break;default:     break;}

Granted, this code is certainly by no means cut & paste, but the concept still makes sense and you could design something that fits in with your current program quite easily (and greatly improve it as well!).

Good luck!

##### Share on other sites
Quote:
 Original post by ChugginWindexI see a whole lot of IF statements in your code, see this page http://www.cplusplus.com/doc/tutorial/control.html and look at the bottom under "switch statements".

I was going to mention the switch statements as well, definitely look into them if you have time.

You can find out what GetChar() does HERE. It sounds like it'll take the next character in a string. In their example, they loop through the string until they find a '.' character.

##### Share on other sites
Thanks for the help, but with switch statements, you cant do something like an else can you? Or do I just hope they dont write something wrong? thats y I only use that for certain games. I'll try learning class next...

##### Share on other sites
That's the main reason why I mentioned that my code wasn't cut & paste, if you notice in the switch statement that I used in my example, there's a "default:" part at the very end that just says it breaks. Before that break line, you can put anything you want. The default case serves as an all-around "else" for the if statements that you're replacing with that design.

Ideally using the example I gave above, all of that code would be wrapped up inside of a loop of some sort or a function so that if the user doesn't input a correct value, the default case can notify the program to go back and start the prompt over again.

I should probably note however before I leave you with the wrong idea, the if statement absolutely has uses in many places that the switch statement is not as flexible. A good example would be if you had more than thing you wanted to test for each case, an if statement would look like

if(playerclass == warrior && playerhealth > 0) {//play warrior adventure music}else if(playerclass == mage && playerhealth < 0) {//play mage death music}else if( etc.. ) { }

However testing these multiple values on each case requires a little extra work if you wanna use a switch statement. Seeing as there's no real performance gain to either, sometimes you're just looking for what's going to be the most understandable when you have to go back and change it a week later.

##### Share on other sites
First off, I would used code tags for all that code ( The Forums FAQ can show you how if you don't know)

second, I would really consider taking on a smaller project at the level you're out right now. It's definitely possible to create a whole game in the way you're going about, however it will become increasingly difficult to modify content, debug and make your game more interactive, with out really utilizing some of the features of c++, specifically Classes!

It seems to me like you do have a very clear idea of what you want to get done (which is a very good thing!)
However, I would definitely read up a little bit on classes for keeping things a little more manageable. As well as maybe looking into switch statements for handling input.

Edit: lol, I think my post just turned into saying exactly what just was said above, but i promise i did hit reply after programmermatic's post first post

Another Edit!:
Quote:
 Original post by MAGIgullorksThanks for the help, but with switch statements, you cant do something like an else can you? Or do I just hope they dont write something wrong? thats y I only use that for certain games. I'll try learning class next...

with switch statements you use
switch foo{  case bar: //if foo == bar  cout << "bar!";  break; case bar2: //if foo == bar2  cout << "bar2!";  break;default: //if no cases are true  cout << "Foo is not equal to bar, or bar2";  break; }

[Edited by - A dam on August 14, 2008 3:55:59 PM]

##### Share on other sites
I think I found my error! Last time I tried making a switch, like today, I accidentally put '' around a number case... It kept printing the right one AND the default too... My bad

• 45
• 11
• 17
• 11
• 13