GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Nov-2006, 15:03
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Help with Dice program


CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>
#include <pause.h>
using namespace std;

#define RAND_MAX = 6;

int main_Roll;
int player_Roll;
int max_Roll;
char wait = getchar();
int roll;



int roll_Dice(){

    max_Roll = 6;
    main_Roll & player_Roll <= max_Roll;

    main_Roll = rand();
    player_Roll = rand();


    roll = rand();
    main_Roll + roll;
    cout<<"My Roll : "<<roll<<"\n";
    cout<<"You are on "<<main_Roll<<"\n";

    roll = rand();
    player_Roll + roll;
    cout<<"Your Roll : "<<roll<<"\n";
    cout<<"You are on "<<player_Roll<<"\n";

    roll = rand();
    roll = rand();

    
}


int main(){


    cout<<"Welcome to the Dice Game \n";
    cout<<"The first player to 21 wins \n";

    do {
        roll_Dice();
    }while (main_Roll != 21 || player_Roll != 21);//This im not sure if its right but it doesnt seem to work

    if (main_Roll == 21){
        cout<<"You Lost! Unlucky!\n";
    }
    else if (player_Roll == 21) {
        cout<<"You win! Congratulations!\n";
    }


}


I cant see whats wrong with it. I want RAND_MAX to equal 6. Even though i've defined it it still wont work.

All I want is to play a game i used to play in Dice. Each player rolls 4 times
the one closest to 21 wins
  #2  
Old 21-Nov-2006, 15:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with Dice program


Quote:
Originally Posted by killzone
I cant see whats wrong with it. I want RAND_MAX to equal 6.


RAND_MAX is defined in the standard library header <stdlib.h>

All calls to the function rand() will return values from 0 to RAND_MAX (inclusive) as defined by the compiler's distribution library. You can't change the library function's action by trying to define your own value of RAND_MAX.

Consider this:

CPP / C++ / C Code:
int r;
int n = 6;

r = rand() % n;
The result will be an int value of r that is from 0 to 5 (inclusive). That is, the value assigned to r will be either 0, 1, 2, 3, 4, or 5.

If you want a number from 1 to 6 (inclusive), then you can try this:

CPP / C++ / C Code:

int r;

int n = 6;
r = (rand() % n) + 1;

Regards,

Dave

Using the modulus operator, %, for positive integers x and n, in an expression like this:

CPP / C++ / C Code:
r = x % n;

will always result in a value of r that is greater than or equal to zero and is less than or equal to n-1 (it's the remainder after performing the integer operation of dividing x by n.)
  #3  
Old 21-Nov-2006, 15:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with Dice program


Quote:
Originally Posted by killzone
CPP / C++ / C Code:
.
.
   main_Roll & player_Roll <= max_Roll;
.
.
.
    main_Roll + roll;

.
.
.
    player_Roll + roll;
.
.
.


These statements have absolutely no effect on any results in this program. What are you really trying to do with them? (Don't make us guess; tell us.)

Did your compiler have any messages? What compiler?

Regards,

Dave
  #4  
Old 22-Nov-2006, 10:55
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: Help with Dice program


I was just trying stuff out to see if it would change anything but it didn't and there were no compiler errors the program would just keep rolling 1000s
  #5  
Old 22-Nov-2006, 11:30
killzone killzone is offline
Junior Member
 
Join Date: Nov 2006
Posts: 66
killzone is an unknown quantity at this point

Re: Help with Dice program


I've changed it and now it wont do nothing and an error comes up.It compiles but there must be an error. Any help
CPP / C++ / C Code:
#include <iostream>
#include <stdlib.h>
#include <pause.h>
using namespace std;

int main_Roll;
int player_Roll;
int n;
int roll_Count;
int roll_num = (rand() % n) + 1;

int roll_Dice() {

    roll_num = (rand() % n) + 1;

    cout<<"My Roll : "<<roll_num<<"\n";
    cout<<"You are on "<<main_Roll<<"\n";

    roll_num = (rand() % n) + 1;

    cout<<"Your Roll : "<<roll_num<<"\n";
    cout<<"You are on "<<player_Roll<<"\n";

    roll_num = (rand() % n) + 1;
    roll_num = (rand() % n) + 1;
    roll_Count = 1;
}

int main(){



    cout<<"Welcome to the Dice Game \n";
    cout<<"The first player to 21 wins \n";

    roll_Dice();

    if (roll_Count <5) {
     do {
        main_Roll = rand();
        player_Roll = rand();



        cout<<"My Roll : "<<roll_num<<"\n";
        cout<<"You are on "<<main_Roll<<"\n";

        cout<<"Your Roll : "<<roll_num<<"\n";
        cout<<"You are on "<<player_Roll<<"\n";

        roll_num = rand();
        roll_num = rand();
        roll_Count + 1;
    }while (main_Roll != 21 || player_Roll != 21);
    }

    if (main_Roll == 21){
        cout<<"You Lost! Unlucky!\n";
    }
    else if (player_Roll == 21) {
        cout<<"You win! Congratulations!\n";
    }
    else {
        cout<<"It's a Draw!\n";
    }


}
  #6  
Old 22-Nov-2006, 12:15
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,893
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help with Dice program


Quote:
Originally Posted by killzone
I've changed it and now it wont do nothing and an error comes up.It compiles but there must be an error. Any help
CPP / C++ / C Code:
int n;
int roll_Count;
int roll_num = (rand() % n) + 1;


In C (or C++) you must assign a value to a variable before using that variable in an expression. If you want n to be equal to 6, then you could try:

CPP / C++ / C Code:
int n = 6;
int roll_Count;
int roll_num = (rand() % n) + 1;

Your program doesn't assign a value to n. What value would you expect the program to use? How could it possibly know? Look at your code and think about what you are trying to get it to do.


Regards,

Dave
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 04:48
Pipeline freeze simulation darklightred C++ Forum 6 27-Jul-2006 20:37
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 10:11
Dice rolling program (random number generator) crystalattice Python Forum 1 17-Apr-2006 18:34
Type casts ? kai85 C++ Forum 12 23-Jun-2005 13:04

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:28.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.