Basically I did some code for betting on horses here
here the header with the function prototypes
1 2 3 4
int assignOdds (int horse) ; // function to assign odds
int whichHorseToBetOn (void) ; // function to ask which horse to bet on
int getAmount (void) ; // function to ask how much to bet
int runRace (void) ; // function to run the race
//Horse Betting
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "betting.h"
usingnamespace std ;
int Cash = 100; // starting cash
intconst NUMBEROFHORSES = 8 ;
int Bet = 0 ; // how much have they bet
int horseWon = 0 ;
int x =0 ;
int odds[7] ; // array which holds each horses odds
int tempOdds = 0 ;
int winner = 0;
int thisBet = 0; // holds horse bet on
int Amount = 0; //. holds amount bet this race
int main()
{
while (Cash > 0)
{
cout << "You have " << Cash << " to spend. " ;
for(x=0 ; x<NUMBEROFHORSES ; x++) //loop asign and display odds
{
odds[x]= assignOdds(x) ;
cout << "Horse " << x+1 << " Odds are " << odds[x] <<"/1 " ;
}
cout << "Which Horse do you want to bet on - " ;
Bet = whichHorseToBetOn() ;
thisBet=getAmount();
cout << endl ;
winner=runRace();// selects winning horse randomly with runRace() function
if (winner+1 == Bet) {
Cash = Cash+(thisBet*odds[winner])+thisBet ; // If you have picked the winner then calculate earnings
}
}
}
int assignOdds (int horse) // assign random odds to horse
{
tempOdds=(rand()%10)+1;
return tempOdds ;
}
int whichHorseToBetOn ()
{
cin >> Bet ;
return Bet ;
}
int getAmount ()
{
cout << "How much do you want to bet?
You currently have " << Cash << " to spend - ";
cin >> Amount ;
Cash=Cash-Amount ;
return Amount ;
}
int runRace () // calculates randomnly which horse has won the race and displays it on screen
{
horseWon=(rand()%8);
cout << "Horse " << horseWon+1 << " has won the race!
" ;
cout << endl << endl << endl ;
return horseWon;
}
Now its a fairly simple program but what I wanted to do is to changes it to using class's.I though it would be a fairy simple process but I can't seem t even return a smple variable to my main.here's what i've tried so far
race.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef RACE_H
#define RACE_H
class race
{
public:
race();
virtual ~race();
int getCash();
private:
int Cash ; // starting cash
};
#endif // RACE_H
You're missing a vital piece of understanding in regards to functions: return X doesn't send 'X' to the caller; it sends the value of 'X' to the caller. This should work: cout << "You have " << ob.getCash() << " to spend. \n \n" ;
Aside from that, you're making a critical mistake in your understanding of classes: all members of an object are automatically initialized when an object it made. Your constructor simply has to assign a value!
1 2 3 4
race::race()
{
Cash = 100; // Cash already exists; no need to reinitialize it!
}
I haven't really checked the "Main file" of your code, so if there are still problems, please let us know which!
So in previous program I was able to use my cash variable in a while loop as I had declared it too int cash=100; so the while loop worked fine
1 2 3 4 5 6
int main()
{
while (Cash > 0)
{
cout << "You have " << Cash << " to spend. " ;
}
but since I'm using ob.getcash() to take it in I can't use that in the while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13
usingnamespace std;
int main()
{
race ob;
while (Cash> 0) // main game loop - runs whilst you have more than 0 playerCash!
{
cout << "You have " << ob.getCash() << " to spend. \n \n" ;
}
}
So is there a way to do this? maybe make another object of my race class thats equal to ob.
and use that.
Seems like the getAmount() function actually decreases the value of Cash, there are some design flaws on your program though like it's mixing both OOP and procedural. The ob object has a Cash property, and then you also have a global Cash variable, and you're trying to use both interchangeably. So that will cause some problems, you should only have one Cash variable so you can keep track of the money a person has left for betting.
Sorry, just noticed you were actually trying to convert it to use classes. ^^"
I think your actual game loop should look like this or something close to it. =)
1 2 3 4 5
while (ob.getCash() > 0) // main game loop - runs whilst you have more than 0 playerCash!
{
cout << "You have " << ob.getCash() << " to spend. \n \n" ;
// subtract the bet from the player's total cash using a method
}
this is my current header and cpp file that goes with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef RACE_H
#define RACE_H
class race
{
public:
race();
virtual ~race();
int getCash();
int getx();
int getnumhorses();
private:
int Cash ; // starting cash
int numberofhorses ; // total number of horses to bet on
int x ; // used in "for" loop as a counter
};
#endif // RACE_H
that's most likely the error, ob.getx() will return an integer value to you, and you can't assign a value to another. That would be like doing something like
0 = 0; // invalid can't assign a literal constant to another literal constant
I think what you meant to do was something like
for(int i = ob.getX(); i < ob.getnumhorses(); ++i)
anyway, there's really no need to put the counter inside your class though. Since the counter doesn't really belong to any object, if you don't mind me pointing out.
Ya that worked but there just too much to convert and I just don't know enough about class's and setters and getters.Gonna try and re-write my orginal code using dynamic memory