Buying and selling game
Jan 25, 2009 at 3:29am UTC
I want to make this game where the player buys and sells things. While playing the player should be able to see what he/she has and how much money they have, how would I do that, plus when ever the game gets past the second question it shuts down... i dont know how to make it continue to the other question and loop back to the beginning again. Help please (using Dev-C++ 4.4.9.2)
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Start();
void GetResults();
int money, ph, pc, pm, px, maxrand;
char d, s, w, f;
void
Start ()
{
money = 0;
ph = 0;
pc = 0;
pm = 0;
px = 0;
maxrand = 6;
cout << "Select where you want to go: \n" ; // where to go?
cout << "1 : London \n" ;
cout << "2 : Chicago \n" ;
cout << "3 : Hong Kong \n" ;
d = 30;
cin >> d; //going to 1-3
cout << "\n" ;
switch (d)
{
case '1' : maxrand = 40; // prices for everything for now
break ;
case '2' : maxrand = 30;
break ;
case '3' : maxrand = 10;
break ;
default : exit(0);
break ;
}
money = 10000;
srand( (unsigned )time(NULL ) );// init rand() func
f = rand() % maxrand; //qty gets a value between 0 & maxrand
GetResults();
cout << "selct what you want to buy: \n" ; // buying what?
cout << "1 : h \n" ;
cout << "2 : c \n" ;
cout << "3 : m \n" ;
cout << "4 : x \n" ;
cout << "5 : nothing \n" ;
s = 0;
cin >> s; //user buys something
cout << "\n" ;
switch (s)
{
case '1' : (money = money - f) & (ph = ph + 1); // subtracts money
break ;
case '2' : (money = money - f) & (pc = pc + 1);
break ;
case '3' : (money = money - f) & (pm = pm + 1);
break ;
case '4' : (money = money - f) & (px = px + 1);
break ;
case '5' : (money = money);
break ;
default : exit(0);
break ;
}
cout << "what would you like to sell? \n" ; // selling what?
cout << "1 : h \n" ;
cout << "2 : c \n" ;
cout << "3 : m \n" ;
cout << "4 : x \n" ;
cout << "5 : nothing \n" ;
w = 0;
switch (w)
{
case '1' : (money = money + f) & (ph = ph - 1); //adds money sub item
break ;
case '2' : (money = money + f) & (pc = pc - 1);
break ;
case '3' : (money = money + f) & (pm = pm - 1);
break ;
case '4' : (money = money + f) & (px = px - 1);
break ;
case '5' : (money = money) & (ph = ph) & (pc = pc) & (pm = pm) & (px = px);
break ;
default : exit(0);
break ;
}
}
void
GetResults ()
{
if (money <= 0)// no more money left :P
{
cout << "NO MONEY MORON!!!\n\n" ;
Start();
}
else if (money >= 30000)
{
cout << "you win!!!! \n" ;
}
}
int
main ()
{
cout << "buying and selling game \n" ;
Start();
return 0;
}
Jan 25, 2009 at 7:44pm UTC
http://www.cplusplus.com/forum/articles/6046/
This will teach you how to get input from cin and convert it correctly.
But from what I can see, your variable "w" is never assigned a value by the user, yet it's used in a switch() statement.
Jan 26, 2009 at 10:42pm UTC
Ok I modified it a bit, but Im still a bit lost, I cant get the money or the "items" to keep the value after someone buys or sells them when the program goes back and ask where they would like to go... Please help
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void Start();
void GetResults();
int money, ph, pc, pm, px, maxrand;
char d, s, w, f;
void
Start ()
{
while (money <= 30000)
{
money = 0;
ph = 0;
pc = 0;
pm = 0;
px = 0;
maxrand = 6;
cout << "Select where you want to go: \n" ; // where to go?
cout << "1 : London \n" ;
cout << "2 : Chicago \n" ;
cout << "3 : Hong Kong \n" ;
d = 30;
cin >> d; //going to 1-3
cout << "you choose: " << d << endl << endl;
switch (d)
{
case '1' : maxrand = 40; // prices for everything for now
break ;
case '2' : maxrand = 30;
break ;
case '3' : maxrand = 10;
break ;
default : exit(0);
break ;
}
money = 10000;
srand( (unsigned )time(NULL ) );// init rand() func
f = rand() % maxrand; //qty gets a value between 0 & maxrand
cout << "selct what you want to buy: \n" ; // buying what?
cout << "1 : h \n" ;
cout << "2 : c \n" ;
cout << "3 : m \n" ;
cout << "4 : x \n" ;
cout << "5 : nothing \n" ;
s = 0;
cin >> s; //user buys something
cout << "You chose: " << s << endl << endl;
switch (s)
{
case '1' : (money = money - (300+f)) & (ph = ph + 1); // subtracts money
break ;
case '2' : (money = money - (200+f)) & (pc = pc + 1);
break ;
case '3' : (money = money - (300+f)) & (pm = pm + 1);
break ;
case '4' : (money = money - (500+f)) & (px = px + 1);
break ;
case '5' : (money = money) & (px = px) & (pm = pm) & (pc = pc) & (ph = ph);
break ;
default : exit(0);
break ;
}
cout << "You money now is at:" << money << endl << endl;
cout << "You have this much h: " << ph << endl << endl;
cout << "You have this much c: " << pc << endl << endl;
cout << "You have this much m: " << pm << endl << endl;
cout << "You have this much x: " << px << endl << endl;
cout << "what would you like to sell? \n" ; // selling what?
cout << "1 : h \n" ;
cout << "2 : c \n" ;
cout << "3 : m \n" ;
cout << "4 : x \n" ;
cout << "5 : nothing \n" ;
w = 0;
cin >> w;
cout << "\n" ;
switch (w)
{
case '1' : (money = money + (50+f)) & (ph = ph - 1); //adds money sub item
break ;
case '2' : (money = money + (30+f)) & (pc = pc - 1);
break ;
case '3' : (money = money + (10+f)) & (pm = pm - 1);
break ;
case '4' : (money = money + (30+f)) & (px = px - 1);
break ;
case '5' : (money = money) & (ph = ph) & (pc = pc) & (pm = pm) & (px = px);
break ;
default : exit(0);
break ;
}
cout << "You money now is at:" << money << endl << endl;
cout << "You have this much h: " << ph << endl << endl;
cout << "You have this much c: " << pc << endl << endl;
cout << "You have this much m: " << pm << endl << endl;
cout << "You have this much x: " << px << endl << endl;
}
GetResults();
}
void
GetResults ()
{
if (money <= 0)// no more money left :P
{
cout << "NO MONEY MORON!!! \n" ;
Start();
}
else if (money >= 30000)
{
cout << "you win!!!! \n" ;
}
}
int
main ()
{
cout << "buying and selling game \n" ;
Start();
return 0;
}
Jan 26, 2009 at 11:34pm UTC
Well, it looks ok...although you have some major errors in your case statements.
This:
1 2
case '1' : (money = money - (300+f)) & (ph = ph + 1); // subtracts money
break ;
Although it might actually end up working, probably isn't doing exactly what you want it to do. You want to do something like:
1 2 3 4
case '1' :
(money = money - (300+f));
(ph = ph + 1); // subtracts money
break ;
Also, you can get to negative money, since your get results loop is only called after you get to 30k money or higher. Also, if you do manage to run it a bunch, then you might cause some nesting problems, i.e.:
main()->start()->results()[they lost]->start()-> (repeat ad infinitum, causing stack overlow or otherproblems)
Jan 27, 2009 at 2:55am UTC
Cool thanks, thats what I was looking for. Some one to actually tell me what I had done wrong
Feb 13, 2009 at 3:34am UTC
ok ok ok now my question is, the money resets after the second time through, i was wondering how do I make it stop reseting, and keep the value till either you win or loose the game...
Topic archived. No new replies allowed.