Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Rewriting my code to dynamic memory allocation (http://www.go4expert.com/forums/rewriting-code-dynamic-memory-allocation-t27387/)

thefallenangel 16Dec2011 05:41

Rewriting my code to dynamic memory allocation
 
Hey guys,I'm having a go at re-writing a simple enough code I did, to use dynamic memory
here is the header and the main

Code:

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
Code:

//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. \n" ;                  for(x=0        ;        x<NUMBEROFHORSES        ;        x++)        //loop asign and display odds                {        odds[x]= assignOdds(x) ;        cout << "Horse " << x+1 << " Odds are " << odds[x] <<"/1 \n" ;                }        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 -\n  ";        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; }
My question is if I were to replace the "Cash=100" keyword with
int *ptr;
ptr = new int;
*ptr = 100;
Would this work?

xpi0t0s 17Dec2011 05:56

Re: Rewriting my code to dynamic memory allocation
 
Please repost the code with line breaks; it's unreadable as just a single line.
Also please use code blocks for all code.

Regarding your question, I couldn't see "Cash=100" anywhere, but on the face of it (a) yes it should work; (b) why didn't you try it instead of asking here; (c) why would you want to do it this way anyway - what's the benefit of using a pointer?


All times are GMT +5.5. The time now is 04:20.