Thread: Problem with enumeration-style program

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    14

    Problem with enumeration-style program

    Hi guys, I have an assignment to create a program that simulates the game "Craps" using enumeration.

    Rules of Craps-Roll two dice.
    If the shooter rolls a 7 or an 11 on the 1st roll, they win
    If the shooter rolls a 2, 3, or 12 on the 1st roll, they lose
    If the shooter rolls anything else, they continue to roll the dice until either of the following occurs:
    - They roll their initial sum, which means they win.
    -They roll a 7, which means they lose.

    Basically my problem is that after a player wins and wants to play again by entering 0, my program outputs a message as if the computer had run a simulation on its own. Its kind of hard to explain so I will do it with screen shots.

    Initial Screen:
    http://i.imgur.com/TbWWq.png
    After enter stroke:
    http://i.imgur.com/eFIk6.png
    After I entered 0:
    http://i.imgur.com/OuNWW.png
    As you can see, even though my code requires you to hit enter before the next function is called, somehow output gets called without the enter.

    The code:
    Code:
    #include<iostream>#include<time.h>
    #include <cstdlib>
    using namespace std;
    enum Status{WON,LOST,CONTINUE};
    int RollDice(int &turn);
    bool check(Status x, int turn,int val, int num);
    void assign(Status &status, int &turn, int &val, int &num);
    void output(Status status,int val);
    int main(){
        int val=0,num=0;
        int turn=0,ans;
        Status status;
        srand(time(0));
        cout<<"Welcome to Craps!"<<endl;
        do{
        do{
    
    
            assign(status,turn,val,num);
            output(status,val);
        }while(check(status,turn,val,num));
        turn=0;
        num=0;
        val=0;
        num=0;
        cout<<"Enter 0 to play again"<<endl;
        cin>>ans;
        system("cls");
        }while(ans==0);
    }
    int RollDice(int &turn)
    {
        int roll=rand()%12+1;
        return roll;
        turn++;
    }
    bool check(Status status, int turn, int val, int num)
    {
        switch(status){
        case WON:
            return false;
            break;
        case LOST:
            return false;
            break;
        case CONTINUE:
            return true;
        }
    
    
    /*
        if((turn==0)&&(num==0)){
        switch(status)
        {
        case WON:
            return false;
            break;
        case LOST:
            break;
        default:
            status=CONTINUE;
            num=val;
            return true;
            break;
        }
        }
        else{
            if(num==val){
                status=WON;
                return false;
            }
            else if(num==7){
                status=LOST;
                return false;
            }
            else{
                status=CONTINUE;
                return true;
            }
        }*/
    }
    void output(Status status,int val)
    {
        switch(status){
        case WON:
            cout<<"You win with "<<val<<endl;
            break;
        case LOST:
            cout<<"You lose with "<<val<<endl;
            break;
        case CONTINUE:
            cout<<"CONTINUE"<<endl;
        }
    }
    
    
    
    
    void assign(Status &status, int &turn, int &val, int &num)
    {
        cout<<"LAST TURN's NUM: "<<num<<endl;
        cout<<"What you rolled last turn: "<<val<<endl;
        val=RollDice(turn);
        cout<<"What you rolled this turn: "<<val<<endl;
        cout<<"TURN="<<turn<<endl<<"NUM="<<num<<endl;
        cout<<"Press enter to roll";
        cin.get();
        if((turn==0)&&(num==0))
        {
        turn++;
        switch(val)
        {
        case 7:
        case 11:
            status=WON;
            break;
        case 2:
        case 3:
        case 12:
            status=LOST;
            break;
        default:
            status=CONTINUE;
            num=val;
            break;
        }
        }
        else{
            if(num==val)
                status=WON;
            else if(val==7)
                status=LOST;
            else{
                status=CONTINUE;
                num=val;
            }
        }
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Replace this
    Code:
    cout<<"Press enter to roll";
        cin.get();
    with
    Code:
    cout<<"Press enter to roll"<<endl;
        cin.get();
    the input buffer probably is not flushed. Hope this helps
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    14
    Hmm, it actually just moved continue to the next line :l .

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Add cin.ignore() to line 28 to remove the whitespace left in the buffer after line 27.

    I'd also avoid using the system() commands as this could lead to very bad security breaches. You could write your own ClearScreen() function or just let it be. I personally use Unix so my terminals are full of text all the time.. Doesn't really bother me.
    Last edited by Lesshardtofind; 01-06-2013 at 05:12 PM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    14
    Perfect! Thank you very much Lesshardtofind and I appreciate the effort std10093! I'm actually learning programming in a class so I try to use just what the teacher teaches us but security breaches? O.O

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Why system() is evil - C++ Forum

    Read that article if u are curious.

    Craps was the first game I made in c++ to. )

    Keep it up games make programming fun!
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-09-2012, 06:50 AM
  2. simple problem: MenuChooserProgram using enumeration
    By D1ffsta in forum C++ Programming
    Replies: 12
    Last Post: 07-30-2011, 03:47 AM
  3. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  4. Printer enumeration problem
    By knutso in forum Windows Programming
    Replies: 7
    Last Post: 09-21-2004, 02:23 AM
  5. Do you have a paint-style program?
    By swordfish in forum Windows Programming
    Replies: 1
    Last Post: 09-13-2001, 12:58 PM