Thread: C++ Code - A simple challenge

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    C++ Code - A simple challenge

    I was recently chatting in an AIM chatroom and someone started rolling dice. You know "ONLINE HOST rolls two 6 sided dice. 3 4"
    I know it's good to try and write programs that solve annoying things for some good programming practice. So I figured I would try and write a program that does the same thing as the dice rolling. Now I havent actually sat down with my compiler and tryed writing a program to do such a thing yet. I figure since this is the first time trying to solve a problem like this, I'll see how it's done first. (that and I'm not to good with cpp yet, I only know the basics. I'm just learning how to use functions now) So what I'm saying is I'm proposing a dice rolling code challenge. I know writing a code to do this will be extremly easy for most of you, but I would just kinda like to see how you guys do it.

    I figure the only things you would have to use to solve this problem is just use a few variables and if else statements. Using functions would probably help too right? I'm interested to see how this would be coded.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Using rand would probably be the way. Look it up if you want more. This sounds too much like hw. So, I am going to have to tell you to look it up.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    Lol, not homework at all. I dont even have any classes dealing with computer programming. I'm taking basic GED classes at the moment. It just kinda caught my attention while I was chatting and I figured it would be a good way to put some of the things I learned to use. I guess I'll just try to put together some code in a bit and see if I can come any where near to getting it to work. I havent used rand to much though. Only used it once in a little program for a guessing game. I should be able to kinda piece together some code though. I'll post it when I'm done. Thanks.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Each dice could be a separate rand call.

    srand(time(NULL));
    short dice1=(int) rand()%6 + 1;
    short dice2=(int) rand()%6 + 1;
    cout<<"the first dice rolled "<<dice1<<endl;
    cout<<"the second dice rolled "<<dice2<<endl;

  5. #5
    imhungry
    Guest
    The following code is using the ranodom.h because that is the one we are using in my cs class. It is available at http://lpdatafiles.com/data/cpluspc/RANDOM.H

    Code:
    #include <iostream.h>
    #include <c:\random.h>
    #include <conio.h>
    
    int main()
    {
    	randomize();
    		for (int i=1; i<=2; i++)
    		{
    			cout << "roll "<< i << ": " << 1 + random(6) << endl;
    		}
    	getch();
    	return 0;
    }

  6. #6
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Maybe it should be #include <random.h> or if it´s user defind function #include "random.h"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C code problem
    By neo28 in forum C Programming
    Replies: 24
    Last Post: 05-16-2009, 10:48 AM
  2. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Windows Programming
    Replies: 0
    Last Post: 10-14-2002, 01:29 PM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Game Programming
    Replies: 0
    Last Post: 10-14-2002, 01:27 PM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  5. Simple Code, looking for input.
    By Alien_Freak in forum C Programming
    Replies: 3
    Last Post: 03-03-2002, 11:34 AM