Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

c++11 - Basic C++ Dice game

I have a problem here, would be really nice if anyone could help me out here. Its my first time using this program so dont be to judgemental.

#include <cstdlib>
#include <iostream>

using namespace std;
int throw1, throw2, throw3, throw4;
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3);
int deposit;
int account;

int main(){
    int count = 0;

    while(count < 3){

        cin>>deposit;

        while(deposit>5000 || deposit<0){                //Makes sure so that my deposit is between 0-5000
            cout<<"deposit failed"<<endl;
            cin>>deposit;

        }
        account = deposit;
        cout<<"You have deposited" <<deposit<<"Swedish Krona"<<endl;   
        cout<<"and you have this much cash on your account"<<account<<"Swedish Krona"<<endl;

        if (konto>499){                 //Makes sure so that i have the money to bet, and if i dont have the money, i can just put in more 
            cout<<"please place your bet"<<endl;
            cout<<"bet1=100, bet2=300, bet3=500"<<endl;
            cin>>bet1;
            cin>>bet2;
            cin>>bet3;
            account = (deposit - bet);
            cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
        }
        else if(account>299){
            cout<<"please place your bet"<<endl;
            cout<<"bet1=100, bet=300"<<endl;     
            cin>>bet1;
            cin>>bet2;
            account =(deposit - bet);
            cout<<"you have this much cash on your account"<<account<<"Swedish Krona"<<endl;
        }
        else if(account>99){
            cout<<"please place your bet"<<endl;
            cout<<"bet1=100"<<endl;
            cin>>bet1;
            cout<<"you have placed your bet"<<bet<<"Swedish Krona"<<endl;
        }

        while (account<100 || deposit>5000){
            cout<<"insufficient funds"<<endl;
            cin>>deposit;
            account=deposit;
        }

        {
            cout<<"Throw dice"<<endl;
            srand(time(0)); 
            Throw1 = rand() % 6 + 1;
            Throw2 = rand() % 6 + 1;
            Throw3 = rand() % 6 + 1;
            Throw4 = rand() % 6 + 1;
            cout<<"You rolled"<<Throw1<<endl;
            cout<<"You rolled"<<Throw2<<endl;
            cout<<"Computer rolled"<<Throw3<<endl;
            cout<<"Computer rolled"<<Throw4<<endl;
        }
    }
    count++;

    system ("pause");
}

So the thing here is that, for some reason i always bet 500, even though type in bet1 or bet2, and i have no clue how to fix that problem. And then my loop function (int count 0; while(count < 3)count++) it starts to loop endlessly without me pressing anything, even though i use the same loop function in simple coding like just typing some cout<< things it works fine, but when i use it in this code, it goes to drain, do anyone know why this is happening, would appreciate if anyone could answer, thanks in advanced.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
int bet1 = 100;
int bet2 = 300;
int bet3 = 500;
int bet=(bet1, bet2, bet3)

The last line will be evaluated like this: 100, 300, 500. Result of comma separated list of expression will be last value, which is 500. So your bet variable will be always set to 500.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...