CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    2

    Roll a dice program.

    I'm trying to make a dice program that will give a random number when user throws the dice. Every random number gets logged into a system that shows how many of every number has come. It's basic atm but my goal is to make it good.

    It goes through the converter but it's not working properly.

    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    using namespace std;

    int main()
    {
    srand((unsigned)time(0));
    int first = 0;
    int second = 0;
    int third = 0;
    int fourth = 0;
    int fifth = 0;
    int sixth = 0;
    char Throw;
    int i;
    int randomnumber = 0;
    do{
    cout << "Write y if you want to throw, q quits\n";
    cin >> Throw;
    {
    if(Throw == 'y')
    for(int i=0; i<1; i++)
    randomnumber = (rand()%6)+1;
    }
    {
    if(randomnumber == '1')
    first = first + 1;
    else if(randomnumber == '2')
    second = second + 1;
    else if(randomnumber == '3')
    third = third + 1;
    else if(randomnumber == '4')
    fourth = fourth + 1;
    else if(randomnumber == '5')
    fifth = fifth + 1;
    else if(randomnumber == '6')
    sixth = sixth + 1;
    cout <<first<<"\n";
    cout <<second<<"\n";
    cout <<third<<"\n";
    cout <<fourth<<"\n";
    cout <<fifth<<"\n";
    cout <<sixth<<"\n";
    }
    }
    while(Throw != 'q');
    return 0;
    }

    It prints: "
    y
    0
    0
    0
    0
    0
    0
    " indicating that none of the variables have changed.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Roll a dice program.

    Proper indenting and code tags should show you part of the problem:

    Code:
    int main()
    {
        srand((unsigned)time(0));
        int first = 0;
        int second = 0;
        int third = 0;
        int fourth = 0;
        int fifth = 0;
        int sixth = 0;
        char Throw;
        int i;
        int randomnumber = 0;
        do{
            cout << "Write y if you want to throw, q quits\n";
            cin >> Throw;
            {
            if(Throw == 'y')
                for(int i=0; i<1; i++)
                    randomnumber = (rand()%6)+1;
            }
            {
                if(randomnumber == '1')
                    first = first + 1;
                else if(randomnumber == '2')
                    second = second + 1;
                else if(randomnumber == '3')
                    third = third + 1;
                else if(randomnumber == '4')
                    fourth = fourth + 1;
                else if(randomnumber == '5')
                    fifth = fifth + 1;
                else if(randomnumber == '6')
                    sixth = sixth + 1;
                cout <<first<<"\n";
                cout <<second<<"\n";
                cout <<third<<"\n";
                cout <<fourth<<"\n";
                cout <<fifth<<"\n";
                cout <<sixth<<"\n";
            }
        }while(Throw != 'q');
        
        return 0;
    }
    You proabably want those "if(randomnumber == 'x')"inside the for loop.

    Also:
    Code:
    randomnumber == '1'
    Randome is an integer who's value is between 1 and 6. '1' is a char. It's integer value is just a code that represents one. Proabably some random number between 40 an 100. what you want is:
    Code:
    if(randomnumber == 1)
    Notice the lack of quotes
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Jun 2005
    Posts
    315

    Re: Roll a dice program.

    Code:
    if(randomnumber == '1')
    This is comparing randomnumber (which is HEX 0x01.....0x06) to ASCII '1' which is HEX 0x31.
    Try removing the apostrophies in the other if statements as well.
    Code:
    if(randomnumber == 1)

  4. #4
    Join Date
    May 2010
    Posts
    2

    Re: Roll a dice program.

    My friend helped me with this but he missed the loop thing. Thanks so much now it works!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured