Changing to a class structure

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 


Main file
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
//Horse Betting
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "betting.h"

using namespace std ;

int Cash = 100;   // starting cash
int const 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 


race.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "race.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
race::race()
{

    int Cash = 100;
}
race::~race()
{
    
}
int race::getCash()
{
    return Cash;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "race.h"

using namespace std;

int main()

	{
    race ob;
    ob.getCash();
cout << "You have " << Cash << " to spend. \n \n" ;
}


I can't seem to even return my cash variable to my main program and this is only the start of the program
cout << "You have " << ob.getCash() << " to spend." << endl << endl;
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!
Yes thanks that fixed it,reintializing int cash was pretty silly ,gonna move onto the next part of the program and see how I get on
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
using namespace 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.
Edit:

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
                }
Last edited on
Weird I had that in first and I got back an error but its working now but then the for loop is all messed up.
Orginally it was this
1
2
3
4
5
for(x=0	;	x<NUMBEROFHORSES	;	x++)	//loop asign and display odds
		{
	odds[x]= assignOdds(x) ;
	cout << "Horse " << x+1 << " Odds are " << odds[x] <<"/1 " ;
		}


and I tried changing it to this
1
2
3
4
for(ob.getx()=0	;ob.getx()<ob.getnumhorses();ob.getx()++)	
		{

		}


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 


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
#include "race.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
race::race()
{

    Cash = 100;
   // int tempOdds = 0 ;
     x =0 ;
     numberofhorses = 8 ;

}
race::~race()
{
    //dtor
}
int race::getCash()
{
    return Cash;
}
int race::getx()
{
    return x;
}
int race::getnumhorses()
{
    return numberofhorses;
}


I'm thinking the error had something to do with not being able to increment a function
ob.getx()=0

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.
Last edited on
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
well, you can email me or something if you want so I can help you debug your code easier, otherwise this thread may become rather long. :P

my email is:
dacster13@yahoo.com
Thanks
Topic archived. No new replies allowed.