First c++ game: dice - Broken, though :(

Discussion in 'Programming General' started by Burden, Feb 1, 2009.

First c++ game: dice - Broken, though :(
  1. Unread #1 - Feb 1, 2009 at 11:49 AM
  2. Burden
    Joined:
    Dec 30, 2008
    Posts:
    894
    Referrals:
    0
    Sythe Gold:
    0

    Burden Apprentice
    Banned

    First c++ game: dice - Broken, though :(

    Code:
        
        //Dice game.
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    using namespace std;
    
    int main()
    {
    
        srand(time(0)); // seeds a random number based on the time
    
        int randomNumber = rand(); // creates randomized number
    
        int die = (randomNumber % 6) + 1; // get a number between 1 and 6
        int cash = 10;
        int guess;
        
        
        
        
        do
        {
            cout << "You have " << cash << " dollars, guess a number!\n";
            cin >> guess;
        
        if (guess == die)
           cout << "You got it right! You won a dollar!";
           cash = (cash + 1);
        
        
        if (guess < die || guess >die)
           cout << "You guessed wrong, OH NO, YOU LOST A DOLLAR!";
           cash = (cash - 1);
    
        }
        while (cash == 0);
              cout << "You have run out of cash, very sorry, better luck next time.";
        
        return 0;
    }
    
    
    
        
        
    
    EDIT: Updated, I fixed some of it.
    Now it wont loop when I guess wrong, just ends it.

    EDIT: If you guess it right, it says I still only have 10 dollars and then says I have run out of cash and ends.
     
  3. Unread #2 - Feb 1, 2009 at 11:32 PM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    First c++ game: dice - Broken, though :(

    i fixed it up to how I believe you intended it to work.

    Your problems were:
    1) Not re-seeding the random number generator to generate a new dice roll after each win.
    2) Not bracketing your if statements caused them to not function as you intended.

    Code:
        //Dice game.
    
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    
    using namespace std;
    
    int main()
    {
        int die = (rand() % 6) + 1; // get a number between 1 and 6
        int cash = 10;
        int guess; 
        
        do
        {
            cout << "You have " << cash << " dollars, guess a number!\n";
            cin >> guess;
    
        if (guess == die) {
           cout << "You got it right! You won a dollar!\n\n";
           cash++;
           srand(time(0)); // seeds a random number based on the time
    			 die = (rand() % 6) + 1; // get a number between 1 and 6
    		}
        else {
           cout << "You guessed wrong, OH NO, YOU LOST A DOLLAR!\n\n";
           cash--;
    		}
    
        }
        while (cash > 0);
        
    		cout << "You have run out of cash, very sorry, better luck next time.";
        
        getchar();
        getchar();
        return 0;
    }
    
     
< [Help]MAKING RUNESCAPE BOT[HELP] | I am new to coding >

Users viewing this thread
1 guest


 
 
Adblock breaks this site