Nov 27, 2013 at 11:52pm UTC
I'm writing a program for class and I have to make multiple games. I'm working on the first game (Roulette). The game asks the player for:
their name,
how many times they want to play,
if they want to bet on a certain number or even/odd,
lastly how much they would like to bet
The problem I ran into is when the player enters the amount of times he would like to play an error box shows up.
http://imgur.com/mmrkf9J
Here is my code
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
#include<iostream>
#include<string>
#include<iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
class Player
{
public :
Player(const char *);
~Player();
virtual float bonus() = 0;
virtual float total() const = 0 ;
virtual void print() const ;
private :
char *name;
};
Player::Player(const char *f)
{
name = new char [strlen(f) + 1];
strcpy_s(name, strlen(f), f);
}
void Player::print() const
{
cout << name;
}
Player::~Player()
{
delete [] name;
}
class Roulette: public Player
{
public :
Roulette(const char *, int = 0);
virtual float bonus();
virtual float total() const ;
virtual void print() const ;
void game();
private :
int plays, number,random;
double bet, winnings, extra,ep;
char gametype[2];
char evenodd[2];
};
Roulette::Roulette(const char *f, int p)
: Player(f)
{
plays = p;
}
void Roulette::game()
{
int i;
for (i=0;i<plays;i++)
{
cout << endl << "Roulette Game " << ++i << endl;
cout << "How much would you like to bet? " ;
cin >> bet;
cout << "Would you like to bet on a specific number (N) or on odd/even (O)? " ;
cin >> gametype;
if (gametype == "N" ) // selects to pick a number
{
cout << "What number would you like to bet on? " ;
cin >> number;
if (number == 00)
number = 37;
random = rand()%38; // to pick a random number in a range, use the following formula:
// num = random() % (high - low + 1) + low;
cout << "The ball landed on " << random << endl;
if (number != random) // lose
{
cout << "You lose $" << bet << endl;
winnings -= bet;
}
else // win
{
cout << "You win $" << 35*bet << endl;
winnings += 35*bet;
}
}
if (gametype == "O" ) // selects even or odd
{
cout << "Would you like to bet on even (E) or odd (O)? " ;
cin >> evenodd;
random = rand()%38;
cout << "The ball landed on " << random << endl;
if (evenodd == "E" ) // selects EVEN
{
if (2*(random/2) == random) // even win
{
cout << "You win $" << bet << endl;
winnings += bet;
}
else // even lose
{
cout << "You lose $" << bet << endl;
winnings -= bet;
}
}
if (evenodd == "O" ) // selects ODD
{
if (2*(random/2) == random) // odd lose
{
cout << "You lose $" << bet << endl;
winnings -= bet;
}
else // odd win
{
cout << "You win $" << bet << endl;
winnings += bet;
}
}
}
}
}
float Roulette::bonus()
{
if (winnings <= 0)
ep = 0;
if (winnings > 0 && winnings <= 10)
ep = .01;
if (winnings > 10 && winnings <= 100)
ep = .015;
if (winnings > 100 && winnings <= 500)
ep = .018;
if (winnings > 500)
ep = .021;
return winnings*ep;
}
float Roulette::total() const
{
return winnings*ep + winnings;
}
void Roulette::print() const
{
cout << "Final Results for " ;
Player::print();
cout << " after " << plays << "games of Roulette:" << endl;
cout << "You won a total of $" << winnings << endl;
cout << "The casino will add a bonus of " << ep*100 << "% to your winnings." << endl;
}
int main()
{
char name[40];
int plays;
cout << fixed << showpoint << setprecision(2);
srand((unsigned )time(NULL));
cout << "********* Roulette **********" << endl << endl;
cout << "What is your full name? " ;
cin.getline(name,40);
cout<< "How many times would you like to play Roulette? " ;
cin >> plays;
Player* ptr;
ptr = new Roulette(name, plays);
ptr->print();
cout << "The casino bonus is $" << ptr->bonus() << endl;
cout << "Your total winnings are $" << ptr->total() << endl << endl;
system("PAUSE" );
return 0;
}
Last edited on Nov 28, 2013 at 12:01am UTC
Nov 28, 2013 at 12:42am UTC
strcpy_s(name, strlen(f) + 1, f)
In c++ (or C in this case), strings are stored in this format
H E L L O W O R L D \0
The \0
at the end is not displayed, and it signals to the program that the string has ended.
Now the problem is that strlen(f)
does not include the \0
when it is counting the string, so the \0 does not get copied to the new string.
Now, when your print the string later, the program does not know where the strings stops. This causes the program to crash.
-Blueberry
Last edited on Nov 28, 2013 at 12:42am UTC