Passing Parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sonia

    Passing Parameters

    Hi, Please look at the following code. This is a simple program for a game
    of craps - very basic.
    I have declared the gameStatus as global variable. I was wondering if there
    is a better way of handling this (possibly via local variables)
    I am using that variable to keep track of the status of the game and pass
    it back to main() from craps() where I either increase or decrease a bank
    balance.

    Should this be handled with local parameters ? would that be a better way.
    If so, how would I do that ?

    Thanks

    Following is the code:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>

    using namespace std;

    int rollDice();
    int craps();

    enum Status {CONTINUE, WON, LOST};
    Status gameStatus;

    int main(){

    int bankBalance = 1000;
    int wager;
    char answer;

    do {
    cout<<"Place a wager: ";
    cin>>wager;

    while (wager >= bankBalance) {
    cout<<"\nYour bank balance is: "<<bankBalance< <endl;
    cout<<"Please enter a valid wager: "<<endl;
    cin>>wager;
    }


    craps();

    if (gameStatus == WON) {
    bankBalance = bankBalance + wager;
    cout<<"Your bank balance is $"<<bankBalance <<endl;

    }
    else {
    bankBalance = bankBalance - wager;
    cout<<"Your bank balance is $"<<bankBalance <<endl;
    }

    cout<<"Do you want to play again? (y/n)";
    cin>>answer;

    } while( answer =='y' || answer == 'Y'); // end of while

    return 0;
    }

    int craps() {

    int sum, myPoint;

    srand(time(0));
    sum = rollDice();

    switch (sum) {

    case 7:
    case 11:
    gameStatus = WON;
    break;

    case 2:
    case 3:
    case 12:
    gameStatus = LOST;
    break;
    default:
    gameStatus = CONTINUE;
    myPoint = sum;
    cout<<"Point is: "<<myPoint<<end l;

    }

    while(gameStatu s==CONTINUE) {
    sum=rollDice();

    if(sum==myPoint )
    gameStatus = WON;
    else if (sum==7)
    gameStatus = LOST;
    }

    if (gameStatus == WON)
    cout<<"Player wins"<<endl;
    else
    cout<<"Player Loses"<<endl;

    return(gameStat us);
    }

    int rollDice() {

    int die1, die2, workSum;

    die1 = 1+rand() % 6;
    die2 = 1+rand() % 6;

    workSum = die1 + die2;
    cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;

    return workSum;

    }




  • Mike Wahler

    #2
    Re: Passing Parameters

    Sonia <smach02@hotmai l.com> wrote in message
    news:og95b.25$y 6.14@bignews5.b ellsouth.net...[color=blue]
    > Hi, Please look at the following code. This is a simple program for a game
    > of craps - very basic.
    > I have declared the gameStatus as global variable. I was wondering if[/color]
    there[color=blue]
    > is a better way of handling this (possibly via local variables)
    > I am using that variable to keep track of the status of the game and pass
    > it back to main() from craps() where I either increase or decrease a bank
    > balance.
    >
    > Should this be handled with local parameters ? would that be a better way.
    > If so, how would I do that ?[/color]

    You don't need to do any of that. See below.
    [color=blue]
    >
    > Thanks
    >
    > Following is the code:
    >
    > #include <iostream>
    > #include <cstdlib>
    > #include <ctime>
    >
    > using namespace std;
    >
    > int rollDice();
    > int craps();
    >
    > enum Status {CONTINUE, WON, LOST};[/color]

    [color=blue]
    > Status gameStatus;[/color]

    Remove this line.
    [color=blue]
    >
    > int main(){
    >
    > int bankBalance = 1000;
    > int wager;
    > char answer;
    >
    > do {
    > cout<<"Place a wager: ";
    > cin>>wager;
    >
    > while (wager >= bankBalance) {
    > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
    > cout<<"Please enter a valid wager: "<<endl;
    > cin>>wager;
    > }
    >
    >[/color]
    [color=blue]
    > craps();[/color]

    Remove this line.
    [color=blue]
    >[/color]
    [color=blue]
    > if (gameStatus == WON) {[/color]

    Replace with:

    if (craps() == WON) {
    [color=blue]
    > bankBalance = bankBalance + wager;
    > cout<<"Your bank balance is $"<<bankBalance <<endl;
    >
    > }
    > else {
    > bankBalance = bankBalance - wager;
    > cout<<"Your bank balance is $"<<bankBalance <<endl;
    > }
    >
    > cout<<"Do you want to play again? (y/n)";
    > cin>>answer;
    >
    > } while( answer =='y' || answer == 'Y'); // end of while
    >
    > return 0;
    > }[/color]

    -Mike




    Comment

    • Gianni Mariani

      #3
      Re: Passing Parameters

      Sonia wrote:[color=blue]
      > Hi, Please look at the following code. This is a simple program for a game
      > of craps - very basic.
      > I have declared the gameStatus as global variable. I was wondering if there
      > is a better way of handling this (possibly via local variables)
      > I am using that variable to keep track of the status of the game and pass
      > it back to main() from craps() where I either increase or decrease a bank
      > balance.
      >
      > Should this be handled with local parameters ? would that be a better way.
      > If so, how would I do that ?
      >
      > Thanks
      >
      > Following is the code:
      >
      > #include <iostream>
      > #include <cstdlib>
      > #include <ctime>
      >
      > using namespace std;
      >
      > int rollDice();
      > int craps();
      >
      > enum Status {CONTINUE, WON, LOST};
      > Status gameStatus;
      >
      > int main(){
      >
      > int bankBalance = 1000;
      > int wager;
      > char answer;
      >
      > do {
      > cout<<"Place a wager: ";
      > cin>>wager;
      >
      > while (wager >= bankBalance) {
      > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
      > cout<<"Please enter a valid wager: "<<endl;
      > cin>>wager;
      > }
      >
      >
      > craps();
      >
      > if (gameStatus == WON) {
      > bankBalance = bankBalance + wager;
      > cout<<"Your bank balance is $"<<bankBalance <<endl;
      >
      > }
      > else {
      > bankBalance = bankBalance - wager;
      > cout<<"Your bank balance is $"<<bankBalance <<endl;
      > }
      >
      > cout<<"Do you want to play again? (y/n)";
      > cin>>answer;
      >
      > } while( answer =='y' || answer == 'Y'); // end of while
      >
      > return 0;
      > }
      >
      > int craps() {
      >
      > int sum, myPoint;
      >
      > srand(time(0));
      > sum = rollDice();
      >
      > switch (sum) {
      >
      > case 7:
      > case 11:
      > gameStatus = WON;
      > break;
      >
      > case 2:
      > case 3:
      > case 12:
      > gameStatus = LOST;
      > break;
      > default:
      > gameStatus = CONTINUE;
      > myPoint = sum;
      > cout<<"Point is: "<<myPoint<<end l;
      >
      > }
      >
      > while(gameStatu s==CONTINUE) {
      > sum=rollDice();
      >
      > if(sum==myPoint )
      > gameStatus = WON;
      > else if (sum==7)
      > gameStatus = LOST;
      > }
      >
      > if (gameStatus == WON)
      > cout<<"Player wins"<<endl;
      > else
      > cout<<"Player Loses"<<endl;
      >
      > return(gameStat us);
      > }
      >
      > int rollDice() {
      >
      > int die1, die2, workSum;
      >
      > die1 = 1+rand() % 6;
      > die2 = 1+rand() % 6;
      >
      > workSum = die1 + die2;
      > cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;
      >
      > return workSum;
      >
      > }
      >[/color]

      Try to create a few classes. See - no globals !


      #include <iostream>
      #include <cstdlib>
      #include <ctime>

      using namespace std;


      class Dice
      {
      public:

      int rollDice() {

      int die1, die2, workSum;

      die1 = 1+rand() % 6;
      die2 = 1+rand() % 6;

      workSum = die1 + die2;
      cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;

      return workSum;
      }

      };

      class CrapsGame : public Dice
      {
      public:

      enum Status {CONTINUE, WON, LOST};
      Status gameStatus;

      int craps() {

      int sum, myPoint;

      srand(time(0));
      sum = rollDice();

      switch (sum) {

      case 7:
      case 11:
      gameStatus = WON;
      break;

      case 2:
      case 3:
      case 12:
      gameStatus = LOST;
      break;
      default:
      gameStatus = CONTINUE;
      myPoint = sum;
      cout<<"Point is: "<<myPoint<<end l;

      }

      while(gameStatu s==CONTINUE) {
      sum=rollDice();

      if(sum==myPoint )
      gameStatus = WON;
      else if (sum==7)
      gameStatus = LOST;
      }

      if (gameStatus == WON)
      cout<<"Player wins"<<endl;
      else
      cout<<"Player Loses"<<endl;

      return(gameStat us);
      }

      };


      class CrapsPlayer
      {
      public:

      int bankBalance;

      CrapsPlayer()
      : bankBalance( 1000 )
      {
      }

      int PlaceAWager()
      {
      int wager;

      cout<<"Place a wager: ";
      cin>>wager;

      while (wager >= bankBalance) {
      cout<<"\nYour bank balance is: "<<bankBalance< <endl;
      cout<<"Please enter a valid wager: "<<endl;
      cin>>wager;
      }

      return wager;
      }

      void PlayCraps( int wager, CrapsGame & game )
      {
      game.craps();

      if (game.gameStatu s == CrapsGame::WON) {
      bankBalance += wager;
      cout<<"Your bank balance is $"<<bankBalance <<endl;

      }
      else
      {
      bankBalance -= wager;
      cout<<"Your bank balance is $"<<bankBalance <<endl;
      }
      }

      bool AskToPlay()
      {
      char answer;
      cout<<"Do you want to play again? (y/n)";
      cin>>answer;

      return answer =='y' || answer == 'Y';
      }

      };

      int main(){

      int wager;

      CrapsGame game;

      CrapsPlayer player;

      do {

      player.PlayCrap s( player.PlaceAWa ger(), game );

      } while( player.AskToPla y() ); // end of while

      return 0;
      }


      Comment

      • John Harrison

        #4
        Re: Passing Parameters


        "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
        news:bj3k2j$21t @dispatch.conce ntric.net...[color=blue]
        > Sonia wrote:[color=green]
        > > Hi, Please look at the following code. This is a simple program for a[/color][/color]
        game[color=blue][color=green]
        > > of craps - very basic.
        > > I have declared the gameStatus as global variable. I was wondering if[/color][/color]
        there[color=blue][color=green]
        > > is a better way of handling this (possibly via local variables)
        > > I am using that variable to keep track of the status of the game and[/color][/color]
        pass[color=blue][color=green]
        > > it back to main() from craps() where I either increase or decrease a[/color][/color]
        bank[color=blue][color=green]
        > > balance.
        > >
        > > Should this be handled with local parameters ? would that be a better[/color][/color]
        way.[color=blue][color=green]
        > > If so, how would I do that ?
        > >[/color][/color]

        [snip]
        [color=blue]
        >
        > Try to create a few classes. See - no globals !
        >
        >
        > #include <iostream>
        > #include <cstdlib>
        > #include <ctime>
        >
        > using namespace std;
        >
        >
        > class Dice
        > {
        > public:
        >
        > int rollDice() {
        >
        > int die1, die2, workSum;
        >
        > die1 = 1+rand() % 6;
        > die2 = 1+rand() % 6;
        >
        > workSum = die1 + die2;
        > cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;
        >
        > return workSum;
        > }
        >
        > };
        >
        > class CrapsGame : public Dice
        > {
        > public:
        >
        > enum Status {CONTINUE, WON, LOST};
        > Status gameStatus;
        >
        > int craps() {
        >
        > int sum, myPoint;
        >
        > srand(time(0));
        > sum = rollDice();
        >
        > switch (sum) {
        >
        > case 7:
        > case 11:
        > gameStatus = WON;
        > break;
        >
        > case 2:
        > case 3:
        > case 12:
        > gameStatus = LOST;
        > break;
        > default:
        > gameStatus = CONTINUE;
        > myPoint = sum;
        > cout<<"Point is: "<<myPoint<<end l;
        >
        > }
        >
        > while(gameStatu s==CONTINUE) {
        > sum=rollDice();
        >
        > if(sum==myPoint )
        > gameStatus = WON;
        > else if (sum==7)
        > gameStatus = LOST;
        > }
        >
        > if (gameStatus == WON)
        > cout<<"Player wins"<<endl;
        > else
        > cout<<"Player Loses"<<endl;
        >
        > return(gameStat us);
        > }
        >
        > };
        >[/color]

        [snip]

        Deriving CrapsGame from Dice? Poor design surely, there's no obvious
        releationship.

        Make Dice a freestanding function and class it from CrapsGame (or anywhere
        else).

        john


        Comment

        • John Harrison

          #5
          Re: Passing Parameters


          "Sonia" <smach02@hotmai l.com> wrote in message
          news:og95b.25$y 6.14@bignews5.b ellsouth.net...[color=blue]
          > Hi, Please look at the following code. This is a simple program for a game
          > of craps - very basic.
          > I have declared the gameStatus as global variable. I was wondering if[/color]
          there[color=blue]
          > is a better way of handling this (possibly via local variables)
          > I am using that variable to keep track of the status of the game and pass
          > it back to main() from craps() where I either increase or decrease a bank
          > balance.
          >
          > Should this be handled with local parameters ? would that be a better way.
          > If so, how would I do that ?
          >[/color]

          You don;t need parameters you need craps() to return the game status, see
          below.
          [color=blue]
          > Thanks
          >
          > Following is the code:
          >
          > #include <iostream>
          > #include <cstdlib>
          > #include <ctime>
          >
          > using namespace std;
          >
          > int rollDice();
          > int craps();
          >
          > enum Status {CONTINUE, WON, LOST};
          > Status gameStatus;[/color]

          Remove line above.
          [color=blue]
          >
          > int main(){
          >
          > int bankBalance = 1000;
          > int wager;
          > char answer;[/color]

          Add this

          Status gameStatus;
          [color=blue]
          >
          > do {
          > cout<<"Place a wager: ";
          > cin>>wager;
          >
          > while (wager >= bankBalance) {
          > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
          > cout<<"Please enter a valid wager: "<<endl;
          > cin>>wager;
          > }
          >
          >
          > craps();[/color]

          Replace with this

          gameStatus = craps();
          [color=blue]
          >
          > if (gameStatus == WON) {
          > bankBalance = bankBalance + wager;
          > cout<<"Your bank balance is $"<<bankBalance <<endl;
          >
          > }
          > else {
          > bankBalance = bankBalance - wager;
          > cout<<"Your bank balance is $"<<bankBalance <<endl;
          > }
          >
          > cout<<"Do you want to play again? (y/n)";
          > cin>>answer;
          >
          > } while( answer =='y' || answer == 'Y'); // end of while
          >
          > return 0;
          > }
          >
          > int craps() {[/color]

          Replace with this

          Status craps() {
          [color=blue]
          >
          > int sum, myPoint;
          >
          > srand(time(0));
          > sum = rollDice();
          >
          > switch (sum) {
          >
          > case 7:
          > case 11:
          > gameStatus = WON;
          > break;
          >
          > case 2:
          > case 3:
          > case 12:
          > gameStatus = LOST;
          > break;
          > default:
          > gameStatus = CONTINUE;
          > myPoint = sum;
          > cout<<"Point is: "<<myPoint<<end l;
          >
          > }
          >
          > while(gameStatu s==CONTINUE) {
          > sum=rollDice();
          >
          > if(sum==myPoint )
          > gameStatus = WON;
          > else if (sum==7)
          > gameStatus = LOST;
          > }
          >
          > if (gameStatus == WON)
          > cout<<"Player wins"<<endl;
          > else
          > cout<<"Player Loses"<<endl;
          >
          > return(gameStat us);
          > }
          >
          > int rollDice() {
          >
          > int die1, die2, workSum;
          >
          > die1 = 1+rand() % 6;
          > die2 = 1+rand() % 6;
          >
          > workSum = die1 + die2;
          > cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;
          >
          > return workSum;
          >
          > }[/color]

          You were almost there already.

          john


          Comment

          • Sonia

            #6
            Re: Passing Parameters


            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:bj3rt6$f2k cu$1@ID-196037.news.uni-berlin.de...[color=blue]
            >
            > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
            > news:bj3k2j$21t @dispatch.conce ntric.net...[color=green]
            > > Sonia wrote:[color=darkred]
            > > > Hi, Please look at the following code. This is a simple program for a[/color][/color]
            > game[color=green][color=darkred]
            > > > of craps - very basic.
            > > > I have declared the gameStatus as global variable. I was wondering if[/color][/color]
            > there[color=green][color=darkred]
            > > > is a better way of handling this (possibly via local variables)
            > > > I am using that variable to keep track of the status of the game and[/color][/color]
            > pass[color=green][color=darkred]
            > > > it back to main() from craps() where I either increase or decrease a[/color][/color]
            > bank[color=green][color=darkred]
            > > > balance.
            > > >
            > > > Should this be handled with local parameters ? would that be a better[/color][/color]
            > way.[color=green][color=darkred]
            > > > If so, how would I do that ?
            > > >[/color][/color]
            >
            > [snip]
            >[color=green]
            > >
            > > Try to create a few classes. See - no globals !
            > >
            > >
            > > #include <iostream>
            > > #include <cstdlib>
            > > #include <ctime>
            > >
            > > using namespace std;
            > >
            > >
            > > class Dice
            > > {
            > > public:
            > >
            > > int rollDice() {
            > >
            > > int die1, die2, workSum;
            > >
            > > die1 = 1+rand() % 6;
            > > die2 = 1+rand() % 6;
            > >
            > > workSum = die1 + die2;
            > > cout<<"Player rolled: "<<die1<<" + "<<die2<<" =[/color][/color]
            "<<workSum<<end l;[color=blue][color=green]
            > >
            > > return workSum;
            > > }
            > >
            > > };
            > >
            > > class CrapsGame : public Dice
            > > {
            > > public:
            > >
            > > enum Status {CONTINUE, WON, LOST};
            > > Status gameStatus;
            > >
            > > int craps() {
            > >
            > > int sum, myPoint;
            > >
            > > srand(time(0));
            > > sum = rollDice();
            > >
            > > switch (sum) {
            > >
            > > case 7:
            > > case 11:
            > > gameStatus = WON;
            > > break;
            > >
            > > case 2:
            > > case 3:
            > > case 12:
            > > gameStatus = LOST;
            > > break;
            > > default:
            > > gameStatus = CONTINUE;
            > > myPoint = sum;
            > > cout<<"Point is: "<<myPoint<<end l;
            > >
            > > }
            > >
            > > while(gameStatu s==CONTINUE) {
            > > sum=rollDice();
            > >
            > > if(sum==myPoint )
            > > gameStatus = WON;
            > > else if (sum==7)
            > > gameStatus = LOST;
            > > }
            > >
            > > if (gameStatus == WON)
            > > cout<<"Player wins"<<endl;
            > > else
            > > cout<<"Player Loses"<<endl;
            > >
            > > return(gameStat us);
            > > }
            > >
            > > };
            > >[/color]
            >
            > [snip]
            >
            > Deriving CrapsGame from Dice? Poor design surely, there's no obvious
            > releationship.
            >
            > Make Dice a freestanding function and class it from CrapsGame (or anywhere
            > else).
            >
            > john
            >
            >[/color]

            Thank You all, John, Gianni and Mike. That helped a lot. I am working my
            way to classes, this is only chapter 3 for me. But thanks in advance. It
            makes it much clearer to see an example that's done without classes and them
            seeing classes created on an example familiar to me.
            Mike, when you say he derived a CrapsGame from dice are you referring to the
            "class CrapsGame : public Dice" line of code ? Do you suggest that a dice
            should be left as a function rather than a class ?
            Thank You again.



            Comment

            • Sonia

              #7
              Re: Passing Parameters


              "John Harrison" <john_andronicu s@hotmail.com> wrote in message
              news:bj3s17$f24 vp$1@ID-196037.news.uni-berlin.de...[color=blue]
              >
              > "Sonia" <smach02@hotmai l.com> wrote in message
              > news:og95b.25$y 6.14@bignews5.b ellsouth.net...[color=green]
              > > Hi, Please look at the following code. This is a simple program for a[/color][/color]
              game[color=blue][color=green]
              > > of craps - very basic.
              > > I have declared the gameStatus as global variable. I was wondering if[/color]
              > there[color=green]
              > > is a better way of handling this (possibly via local variables)
              > > I am using that variable to keep track of the status of the game and[/color][/color]
              pass[color=blue][color=green]
              > > it back to main() from craps() where I either increase or decrease a[/color][/color]
              bank[color=blue][color=green]
              > > balance.
              > >
              > > Should this be handled with local parameters ? would that be a better[/color][/color]
              way.[color=blue][color=green]
              > > If so, how would I do that ?
              > >[/color]
              >
              > You don;t need parameters you need craps() to return the game status, see
              > below.
              >[color=green]
              > > Thanks
              > >
              > > Following is the code:
              > >
              > > #include <iostream>
              > > #include <cstdlib>
              > > #include <ctime>
              > >
              > > using namespace std;
              > >
              > > int rollDice();
              > > int craps();
              > >
              > > enum Status {CONTINUE, WON, LOST};
              > > Status gameStatus;[/color]
              >
              > Remove line above.
              >[color=green]
              > >
              > > int main(){
              > >
              > > int bankBalance = 1000;
              > > int wager;
              > > char answer;[/color]
              >
              > Add this
              >
              > Status gameStatus;
              >[color=green]
              > >
              > > do {
              > > cout<<"Place a wager: ";
              > > cin>>wager;
              > >
              > > while (wager >= bankBalance) {
              > > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
              > > cout<<"Please enter a valid wager: "<<endl;
              > > cin>>wager;
              > > }
              > >
              > >
              > > craps();[/color]
              >
              > Replace with this
              >
              > gameStatus = craps();
              >[color=green]
              > >
              > > if (gameStatus == WON) {
              > > bankBalance = bankBalance + wager;
              > > cout<<"Your bank balance is $"<<bankBalance <<endl;
              > >
              > > }
              > > else {
              > > bankBalance = bankBalance - wager;
              > > cout<<"Your bank balance is $"<<bankBalance <<endl;
              > > }
              > >
              > > cout<<"Do you want to play again? (y/n)";
              > > cin>>answer;
              > >
              > > } while( answer =='y' || answer == 'Y'); // end of while
              > >
              > > return 0;
              > > }
              > >
              > > int craps() {[/color]
              >
              > Replace with this
              >
              > Status craps() {
              >[color=green]
              > >
              > > int sum, myPoint;
              > >
              > > srand(time(0));
              > > sum = rollDice();
              > >
              > > switch (sum) {
              > >
              > > case 7:
              > > case 11:
              > > gameStatus = WON;
              > > break;
              > >
              > > case 2:
              > > case 3:
              > > case 12:
              > > gameStatus = LOST;
              > > break;
              > > default:
              > > gameStatus = CONTINUE;
              > > myPoint = sum;
              > > cout<<"Point is: "<<myPoint<<end l;
              > >
              > > }
              > >
              > > while(gameStatu s==CONTINUE) {
              > > sum=rollDice();
              > >
              > > if(sum==myPoint )
              > > gameStatus = WON;
              > > else if (sum==7)
              > > gameStatus = LOST;
              > > }
              > >
              > > if (gameStatus == WON)
              > > cout<<"Player wins"<<endl;
              > > else
              > > cout<<"Player Loses"<<endl;
              > >
              > > return(gameStat us);
              > > }
              > >
              > > int rollDice() {
              > >
              > > int die1, die2, workSum;
              > >
              > > die1 = 1+rand() % 6;
              > > die2 = 1+rand() % 6;
              > >
              > > workSum = die1 + die2;
              > > cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;
              > >
              > > return workSum;
              > >
              > > }[/color]
              >
              > You were almost there already.
              >
              > john
              >
              >[/color]

              Yes John, Thanks, this is what I was thinking as well, just wasn't sure how
              to arrange it. Now it makes sense. I appreciate it.
              Sonia


              Comment

              • John Harrison

                #8
                Re: Passing Parameters

                >[color=blue]
                > Thank You all, John, Gianni and Mike. That helped a lot. I am working my
                > way to classes, this is only chapter 3 for me. But thanks in advance. It
                > makes it much clearer to see an example that's done without classes and[/color]
                them[color=blue]
                > seeing classes created on an example familiar to me.
                > Mike, when you say he derived a CrapsGame from dice are you referring to[/color]
                the[color=blue]
                > "class CrapsGame : public Dice" line of code ? Do you suggest that a dice
                > should be left as a function rather than a class ?
                > Thank You again.
                >[/color]

                I'm certainly suggesting that, not sure about Mike.

                And yes, "class CrapsGame : public Dice" says that Craps game is a class
                with derives from another class called Dice.

                No problem with CrapsGame or CrapsPlayer being a class.

                This sort of thing starts lots of arguments however, so don't take my word
                for it.

                john



                Comment

                • Sonia

                  #9
                  Re: Passing Parameters


                  "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                  news:bj3s17$f24 vp$1@ID-196037.news.uni-berlin.de...[color=blue]
                  >
                  > "Sonia" <smach02@hotmai l.com> wrote in message
                  > news:og95b.25$y 6.14@bignews5.b ellsouth.net...[color=green]
                  > > Hi, Please look at the following code. This is a simple program for a[/color][/color]
                  game[color=blue][color=green]
                  > > of craps - very basic.
                  > > I have declared the gameStatus as global variable. I was wondering if[/color]
                  > there[color=green]
                  > > is a better way of handling this (possibly via local variables)
                  > > I am using that variable to keep track of the status of the game and[/color][/color]
                  pass[color=blue][color=green]
                  > > it back to main() from craps() where I either increase or decrease a[/color][/color]
                  bank[color=blue][color=green]
                  > > balance.
                  > >
                  > > Should this be handled with local parameters ? would that be a better[/color][/color]
                  way.[color=blue][color=green]
                  > > If so, how would I do that ?
                  > >[/color]
                  >
                  > You don;t need parameters you need craps() to return the game status, see
                  > below.
                  >[color=green]
                  > > Thanks
                  > >
                  > > Following is the code:
                  > >
                  > > #include <iostream>
                  > > #include <cstdlib>
                  > > #include <ctime>
                  > >
                  > > using namespace std;
                  > >
                  > > int rollDice();
                  > > int craps();
                  > >
                  > > enum Status {CONTINUE, WON, LOST};
                  > > Status gameStatus;[/color]
                  >
                  > Remove line above.
                  >[color=green]
                  > >
                  > > int main(){
                  > >
                  > > int bankBalance = 1000;
                  > > int wager;
                  > > char answer;[/color]
                  >
                  > Add this
                  >
                  > Status gameStatus;
                  >[color=green]
                  > >
                  > > do {
                  > > cout<<"Place a wager: ";
                  > > cin>>wager;
                  > >
                  > > while (wager >= bankBalance) {
                  > > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
                  > > cout<<"Please enter a valid wager: "<<endl;
                  > > cin>>wager;
                  > > }
                  > >
                  > >
                  > > craps();[/color]
                  >
                  > Replace with this
                  >
                  > gameStatus = craps();
                  >[color=green]
                  > >
                  > > if (gameStatus == WON) {
                  > > bankBalance = bankBalance + wager;
                  > > cout<<"Your bank balance is $"<<bankBalance <<endl;
                  > >
                  > > }
                  > > else {
                  > > bankBalance = bankBalance - wager;
                  > > cout<<"Your bank balance is $"<<bankBalance <<endl;
                  > > }
                  > >
                  > > cout<<"Do you want to play again? (y/n)";
                  > > cin>>answer;
                  > >
                  > > } while( answer =='y' || answer == 'Y'); // end of while
                  > >
                  > > return 0;
                  > > }
                  > >
                  > > int craps() {[/color]
                  >
                  > Replace with this
                  >
                  > Status craps() {
                  >[color=green]
                  > >
                  > > int sum, myPoint;
                  > >
                  > > srand(time(0));
                  > > sum = rollDice();
                  > >
                  > > switch (sum) {
                  > >
                  > > case 7:
                  > > case 11:
                  > > gameStatus = WON;
                  > > break;
                  > >
                  > > case 2:
                  > > case 3:
                  > > case 12:
                  > > gameStatus = LOST;
                  > > break;
                  > > default:
                  > > gameStatus = CONTINUE;
                  > > myPoint = sum;
                  > > cout<<"Point is: "<<myPoint<<end l;
                  > >
                  > > }
                  > >
                  > > while(gameStatu s==CONTINUE) {
                  > > sum=rollDice();
                  > >
                  > > if(sum==myPoint )
                  > > gameStatus = WON;
                  > > else if (sum==7)
                  > > gameStatus = LOST;
                  > > }
                  > >
                  > > if (gameStatus == WON)
                  > > cout<<"Player wins"<<endl;
                  > > else
                  > > cout<<"Player Loses"<<endl;
                  > >
                  > > return(gameStat us);
                  > > }
                  > >
                  > > int rollDice() {
                  > >
                  > > int die1, die2, workSum;
                  > >
                  > > die1 = 1+rand() % 6;
                  > > die2 = 1+rand() % 6;
                  > >
                  > > workSum = die1 + die2;
                  > > cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;
                  > >
                  > > return workSum;
                  > >
                  > > }[/color]
                  >
                  > You were almost there already.
                  >
                  > john
                  >
                  >[/color]

                  John, I've made the changes, the code don't seem to be compiling though.
                  This is what I get now:
                  I am using MC Visual C++ 6.0
                  --------------------Configuration: ex3_54_fixed - Win32
                  Debug--------------------
                  Compiling...
                  ex3_54_fixed.cp p
                  f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(33) : error
                  C2440: '=' : cannot convert from 'int' to 'enum Status'
                  Conversion to enumeration type requires an explicit cast
                  (static_cast, C-style cast or function-style cast)
                  f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(55) : error
                  C2556: 'enum Status __cdecl craps(void)' : overloaded function differs only
                  by return type from 'int __cdecl craps(void)'
                  f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(8) : see
                  declaration of 'craps'
                  f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(55) : error
                  C2371: 'craps' : redefinition; different basic types
                  f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(8) : see
                  declaration of 'craps'
                  f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(65) : error
                  C2065: 'gameStatus' : undeclared identifier
                  Error executing cl.exe.
                  What am I missing ..here's the modified code:
                  #include <iostream>
                  #include <cstdlib>
                  #include <ctime>

                  using namespace std;

                  int rollDice();
                  int craps();

                  enum Status {CONTINUE, WON, LOST};
                  //Status gameStatus; // line removed..insert ed into main()

                  int main(){

                  Status gameStatus; // added here as opposed to global declaration

                  int bankBalance = 1000;
                  int wager;
                  char answer;



                  do {
                  cout<<"Place a wager: ";
                  cin>>wager;

                  while (wager >= bankBalance) {
                  cout<<"\nYour bank balance is: "<<bankBalance< <endl;
                  cout<<"Please enter a valid wager: "<<endl;
                  cin>>wager;
                  }
                  //craps(); // removed
                  gameStatus = craps(); // substituted for the above line

                  if (gameStatus == WON) {
                  bankBalance = bankBalance + wager;
                  cout<<"Your bank balance is $"<<bankBalance <<endl;

                  }
                  else {
                  bankBalance = bankBalance - wager;
                  cout<<"Your bank balance is $"<<bankBalance <<endl;
                  }

                  cout<<"Do you want to play again? (y/n)";
                  cin>>answer;

                  } while( answer =='y' || answer == 'Y'); // end of while


                  return 0;

                  }
                  // int crap() { // removed
                  Status craps() { // substituted for the above line
                  int sum, myPoint;

                  srand(time(0));
                  sum = rollDice();

                  switch (sum) {

                  case 7:
                  case 11:
                  gameStatus = WON;
                  break;

                  case 2:
                  case 3:
                  case 12:
                  gameStatus = LOST;
                  break;
                  default:
                  gameStatus = CONTINUE;
                  myPoint = sum;
                  cout<<"Point is: "<<myPoint<<end l;

                  }

                  while(gameStatu s==CONTINUE) {
                  sum=rollDice();

                  if(sum==myPoint )
                  gameStatus = WON;
                  else if (sum==7)
                  gameStatus = LOST;
                  }

                  if (gameStatus == WON)
                  cout<<"Player wins"<<endl;
                  else
                  cout<<"Player Loses"<<endl;



                  return(gameStat us);
                  }

                  int rollDice() {

                  int die1, die2, workSum;

                  die1 = 1+rand() % 6;
                  die2 = 1+rand() % 6;

                  workSum = die1 + die2;
                  cout<<"Player rolled: "<<die1<<" + "<<die2<<" = "<<workSum<<end l;

                  return workSum;

                  }




                  Comment

                  • John Harrison

                    #10
                    Re: Passing Parameters

                    > >[color=blue][color=green]
                    > >[/color]
                    >
                    > John, I've made the changes, the code don't seem to be compiling though.[/color]

                    OK my fault, see below.
                    [color=blue]
                    > This is what I get now:
                    > I am using MC Visual C++ 6.0
                    > --------------------Configuration: ex3_54_fixed - Win32
                    > Debug--------------------
                    > Compiling...
                    > ex3_54_fixed.cp p
                    > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(33) : error
                    > C2440: '=' : cannot convert from 'int' to 'enum Status'
                    > Conversion to enumeration type requires an explicit cast
                    > (static_cast, C-style cast or function-style cast)
                    > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(55) : error
                    > C2556: 'enum Status __cdecl craps(void)' : overloaded function differs[/color]
                    only[color=blue]
                    > by return type from 'int __cdecl craps(void)'
                    > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(8) :[/color]
                    see[color=blue]
                    > declaration of 'craps'
                    > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(55) : error
                    > C2371: 'craps' : redefinition; different basic types
                    > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(8) :[/color]
                    see[color=blue]
                    > declaration of 'craps'
                    > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(65) : error
                    > C2065: 'gameStatus' : undeclared identifier
                    > Error executing cl.exe.
                    > What am I missing ..here's the modified code:
                    > #include <iostream>
                    > #include <cstdlib>
                    > #include <ctime>
                    >
                    > using namespace std;
                    >
                    > int rollDice();
                    > int craps();[/color]

                    This prototype need to change as well

                    Status craps();

                    And also this line needs to be moved after the definition of Status below.
                    [color=blue]
                    >
                    > enum Status {CONTINUE, WON, LOST};
                    > //Status gameStatus; // line removed..insert ed into main()
                    >
                    > int main(){
                    >
                    > Status gameStatus; // added here as opposed to global declaration
                    >
                    > int bankBalance = 1000;
                    > int wager;
                    > char answer;
                    >
                    >
                    >
                    > do {
                    > cout<<"Place a wager: ";
                    > cin>>wager;
                    >
                    > while (wager >= bankBalance) {
                    > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
                    > cout<<"Please enter a valid wager: "<<endl;
                    > cin>>wager;
                    > }
                    > //craps(); // removed
                    > gameStatus = craps(); // substituted for the above line
                    >
                    > if (gameStatus == WON) {
                    > bankBalance = bankBalance + wager;
                    > cout<<"Your bank balance is $"<<bankBalance <<endl;
                    >
                    > }
                    > else {
                    > bankBalance = bankBalance - wager;
                    > cout<<"Your bank balance is $"<<bankBalance <<endl;
                    > }
                    >
                    > cout<<"Do you want to play again? (y/n)";
                    > cin>>answer;
                    >
                    > } while( answer =='y' || answer == 'Y'); // end of while
                    >
                    >
                    > return 0;
                    >
                    > }
                    > // int crap() { // removed
                    > Status craps() { // substituted for the above line
                    > int sum, myPoint;
                    >[/color]

                    Forgot to add a variable declaration here

                    Status gameStatus;
                    [color=blue]
                    > srand(time(0));
                    > sum = rollDice();
                    >
                    > switch (sum) {
                    >[/color]

                    That should do it.

                    john



                    Comment

                    • Gianni Mariani

                      #11
                      Re: Passing Parameters

                      John Harrison wrote:[color=blue]
                      > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
                      > news:bj3k2j$21t @dispatch.conce ntric.net...
                      >[/color]
                      ....[color=blue]
                      >
                      > Deriving CrapsGame from Dice? Poor design surely, there's no obvious
                      > releationship.
                      >
                      > Make Dice a freestanding function and class it from CrapsGame (or anywhere
                      > else).[/color]

                      Craps is a game of dice. It's 100% obvious to me that this is a IS-A
                      relationship. For example if I wanted to simulate "loaded" dice ?

                      Anyhow, this particular function rolls twice and returns the sum. It's
                      very particular to craps and potentially other similar games. In
                      hindsight, it should give you both values and not the sum.

                      Sonia, this is an excellent example of how 2 people can come up with
                      wildly different opinions even though they've been at it for a while.
                      It really does not matter either way how you do this. There are more
                      obvious and clearer examples of where you need to inherit vs have a member.

                      I suggest you keey your threshhold for creating a class is very low.
                      Small interesting chunks are much easier to understand and maintain that
                      large spaghetti plates.






                      Comment

                      • Sonia

                        #12
                        Re: Passing Parameters


                        "John Harrison" <john_andronicu s@hotmail.com> wrote in message
                        news:bj3tmc$f0m j9$1@ID-196037.news.uni-berlin.de...[color=blue][color=green][color=darkred]
                        > > >
                        > > >[/color]
                        > >
                        > > John, I've made the changes, the code don't seem to be compiling though.[/color]
                        >
                        > OK my fault, see below.
                        >[color=green]
                        > > This is what I get now:
                        > > I am using MC Visual C++ 6.0
                        > > --------------------Configuration: ex3_54_fixed - Win32
                        > > Debug--------------------
                        > > Compiling...
                        > > ex3_54_fixed.cp p
                        > > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(33) : error
                        > > C2440: '=' : cannot convert from 'int' to 'enum Status'
                        > > Conversion to enumeration type requires an explicit cast
                        > > (static_cast, C-style cast or function-style cast)
                        > > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(55) : error
                        > > C2556: 'enum Status __cdecl craps(void)' : overloaded function differs[/color]
                        > only[color=green]
                        > > by return type from 'int __cdecl craps(void)'
                        > > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(8) :[/color]
                        > see[color=green]
                        > > declaration of 'craps'
                        > > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(55) : error
                        > > C2371: 'craps' : redefinition; different basic types
                        > > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(8) :[/color]
                        > see[color=green]
                        > > declaration of 'craps'
                        > > f:\programming\ c++\how to program\chapter 3\ex3_54_fixed. cpp(65) : error
                        > > C2065: 'gameStatus' : undeclared identifier
                        > > Error executing cl.exe.
                        > > What am I missing ..here's the modified code:
                        > > #include <iostream>
                        > > #include <cstdlib>
                        > > #include <ctime>
                        > >
                        > > using namespace std;
                        > >
                        > > int rollDice();
                        > > int craps();[/color]
                        >
                        > This prototype need to change as well
                        >
                        > Status craps();
                        >
                        > And also this line needs to be moved after the definition of Status below.
                        >[color=green]
                        > >
                        > > enum Status {CONTINUE, WON, LOST};
                        > > //Status gameStatus; // line removed..insert ed into main()
                        > >
                        > > int main(){
                        > >
                        > > Status gameStatus; // added here as opposed to global declaration
                        > >
                        > > int bankBalance = 1000;
                        > > int wager;
                        > > char answer;
                        > >
                        > >
                        > >
                        > > do {
                        > > cout<<"Place a wager: ";
                        > > cin>>wager;
                        > >
                        > > while (wager >= bankBalance) {
                        > > cout<<"\nYour bank balance is: "<<bankBalance< <endl;
                        > > cout<<"Please enter a valid wager: "<<endl;
                        > > cin>>wager;
                        > > }
                        > > //craps(); // removed
                        > > gameStatus = craps(); // substituted for the above line
                        > >
                        > > if (gameStatus == WON) {
                        > > bankBalance = bankBalance + wager;
                        > > cout<<"Your bank balance is $"<<bankBalance <<endl;
                        > >
                        > > }
                        > > else {
                        > > bankBalance = bankBalance - wager;
                        > > cout<<"Your bank balance is $"<<bankBalance <<endl;
                        > > }
                        > >
                        > > cout<<"Do you want to play again? (y/n)";
                        > > cin>>answer;
                        > >
                        > > } while( answer =='y' || answer == 'Y'); // end of while
                        > >
                        > >
                        > > return 0;
                        > >
                        > > }
                        > > // int crap() { // removed
                        > > Status craps() { // substituted for the above line
                        > > int sum, myPoint;
                        > >[/color]
                        >
                        > Forgot to add a variable declaration here
                        >
                        > Status gameStatus;
                        >[color=green]
                        > > srand(time(0));
                        > > sum = rollDice();
                        > >
                        > > switch (sum) {
                        > >[/color]
                        >
                        > That should do it.
                        >
                        > john
                        >
                        >
                        >[/color]

                        John, That Did it :)
                        Thanks...
                        Good night now, Its way past my bed time :)
                        Sonia



                        Comment

                        • Karl Heinz Buchegger

                          #13
                          Re: Passing Parameters



                          Gianni Mariani wrote:[color=blue]
                          >
                          > John Harrison wrote:[color=green]
                          > > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
                          > > news:bj3k2j$21t @dispatch.conce ntric.net...
                          > >[/color]
                          > ...[color=green]
                          > >
                          > > Deriving CrapsGame from Dice? Poor design surely, there's no obvious
                          > > releationship.
                          > >
                          > > Make Dice a freestanding function and class it from CrapsGame (or anywhere
                          > > else).[/color]
                          >
                          > Craps is a game of dice. It's 100% obvious to me that this is a IS-A
                          > relationship. For example if I wanted to simulate "loaded" dice ?
                          >[/color]

                          Huch.
                          Craps is a game. It certainly is *not* a dice
                          For it to play you need dice.
                          So craps *has-a* dice.

                          class craps
                          {
                          dice the_dice;
                          };

                          Same with cars and engines. A car is not an engine, but a car contains
                          an engine.

                          --
                          Karl Heinz Buchegger
                          kbuchegg@gascad .at

                          Comment

                          • David White

                            #14
                            Re: Passing Parameters

                            "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
                            news:bj3u6r$jk0 @dispatch.conce ntric.net...[color=blue]
                            > John Harrison wrote:[color=green]
                            > > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message
                            > > news:bj3k2j$21t @dispatch.conce ntric.net...
                            > >[/color]
                            > ...[color=green]
                            > >
                            > > Deriving CrapsGame from Dice? Poor design surely, there's no obvious
                            > > releationship.
                            > >
                            > > Make Dice a freestanding function and class it from CrapsGame (or[/color][/color]
                            anywhere[color=blue][color=green]
                            > > else).[/color]
                            >
                            > Craps is a game of dice.[/color]

                            But your class Dice is not a dice game. It's the dice themselves. A dice
                            game _uses_, or _has_, dice. It also has players and rules etc.
                            [color=blue]
                            > It's 100% obvious to me that this is a IS-A
                            > relationship.Fo r example if I wanted to simulate "loaded" dice ?[/color]

                            Then you might derive a class LoadedDice from Dice, and have your Craps
                            game - an object of an unrelated class - contain a LoadedDice object instead
                            of a Dice object.

                            DW



                            Comment

                            • Gianni Mariani

                              #15
                              Re: Passing Parameters

                              Karl Heinz Buchegger wrote:
                              [color=blue]
                              >
                              > Huch.
                              > Craps is a game. It certainly is *not* a dice[/color]

                              So certain are you ?
                              [color=blue]
                              > For it to play you need dice.
                              > So craps *has-a* dice.
                              >
                              > class craps
                              > {
                              > dice the_dice;
                              > };
                              >
                              > Same with cars and engines. A car is not an engine, but a car contains
                              > an engine.
                              >[/color]

                              Cars are built around an engine. I can see how some designs would
                              inherit from engine. Why not ?

                              It depends on what you're trying to model with your design.


                              Comment

                              Working...