Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Roll the dice

Roll the dice

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark 0
    wrote on last edited by
    #1

    Does anybody have a simple "Roll the Dice" program? I just need to get my hands on some working code to study. I have been trying to build this program on my own but I get tripped up when trying to implement other functions besides main. I am a beginer, what can I say:-O -Thanks Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

    R M 2 Replies Last reply
    0
    • M Mark 0

      Does anybody have a simple "Roll the Dice" program? I just need to get my hands on some working code to study. I have been trying to build this program on my own but I get tripped up when trying to implement other functions besides main. I am a beginer, what can I say:-O -Thanks Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      ---Mark--- wrote: I am a beginer, what can I say There's nothing wrong with that. We were (and still are in many ways) all beginners at one point. While I don't have a working program, I can give you a hint to get you going: use the rand() function to obtain a random number. Restrict the random number to the interval [1..6] by using the % operator. /ravi My new year's resolution: 2048 x 1536 Home | Articles | Freeware | Music ravib@ravib.com

      1 Reply Last reply
      0
      • M Mark 0

        Does anybody have a simple "Roll the Dice" program? I just need to get my hands on some working code to study. I have been trying to build this program on my own but I get tripped up when trying to implement other functions besides main. I am a beginer, what can I say:-O -Thanks Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

        M Offline
        M Offline
        Mark 0
        wrote on last edited by
        #3

        Still working on my program. And here is what I got so far: #include #include void showone(); void showtwo(); void showthree(); void showfour(); void showfive(); void showsix(); char getans(); void printlines(int n); main() { int r; char ans; ans = getans(); while(ans== 'y') { r = rand()%6 + 1; printlines(2); if (r==1) showone(); if (r==2) showtwo(); if (r==3) showthree(); if (r==4) showfour(); if (r==5) showfive(); if (r==6) showsix(); printlines(2); } printlines(2); } char getans() { int ans; printf("Throw y/n ?"); ans = -1; while (ans == -1) { ans=getchar(); } return ans; } void printlines(int n) { int i; for(i=1 ; i<=n ; i++) printf("\n"); } void showone() { printf("\n * \n"); } void showtwo() { printf(" * \n\n"); printf(" * \n"); } void showthree() { printf(" * \n"); printf(" * \n"); printf(" *\n"); } void showfour() { printf(" * * \n\n"); printf(" * * \n"); } void showfive() { printf(" * * \n"); printf(" * \n"); printf(" * * \n"); } void showsix() { int i; for(i=1 ; i>=3 ; i++) printf(" * * \n"); } The problem I am having is I can't figure how to make it loop back to the y/n. How it is now it goes on forever. How can I make it ask "Throw y/n" each time? Thanks -Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

        M M H 3 Replies Last reply
        0
        • M Mark 0

          Still working on my program. And here is what I got so far: #include #include void showone(); void showtwo(); void showthree(); void showfour(); void showfive(); void showsix(); char getans(); void printlines(int n); main() { int r; char ans; ans = getans(); while(ans== 'y') { r = rand()%6 + 1; printlines(2); if (r==1) showone(); if (r==2) showtwo(); if (r==3) showthree(); if (r==4) showfour(); if (r==5) showfive(); if (r==6) showsix(); printlines(2); } printlines(2); } char getans() { int ans; printf("Throw y/n ?"); ans = -1; while (ans == -1) { ans=getchar(); } return ans; } void printlines(int n) { int i; for(i=1 ; i<=n ; i++) printf("\n"); } void showone() { printf("\n * \n"); } void showtwo() { printf(" * \n\n"); printf(" * \n"); } void showthree() { printf(" * \n"); printf(" * \n"); printf(" *\n"); } void showfour() { printf(" * * \n\n"); printf(" * * \n"); } void showfive() { printf(" * * \n"); printf(" * \n"); printf(" * * \n"); } void showsix() { int i; for(i=1 ; i>=3 ; i++) printf(" * * \n"); } The problem I am having is I can't figure how to make it loop back to the y/n. How it is now it goes on forever. How can I make it ask "Throw y/n" each time? Thanks -Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

          M Offline
          M Offline
          Mike Beckerleg
          wrote on last edited by
          #4

          Try changing ans = getans(); while(ans== 'y') { ... } to while(getans()=='y') { ... } That should make it ask the question each time.

          1 Reply Last reply
          0
          • M Mark 0

            Still working on my program. And here is what I got so far: #include #include void showone(); void showtwo(); void showthree(); void showfour(); void showfive(); void showsix(); char getans(); void printlines(int n); main() { int r; char ans; ans = getans(); while(ans== 'y') { r = rand()%6 + 1; printlines(2); if (r==1) showone(); if (r==2) showtwo(); if (r==3) showthree(); if (r==4) showfour(); if (r==5) showfive(); if (r==6) showsix(); printlines(2); } printlines(2); } char getans() { int ans; printf("Throw y/n ?"); ans = -1; while (ans == -1) { ans=getchar(); } return ans; } void printlines(int n) { int i; for(i=1 ; i<=n ; i++) printf("\n"); } void showone() { printf("\n * \n"); } void showtwo() { printf(" * \n\n"); printf(" * \n"); } void showthree() { printf(" * \n"); printf(" * \n"); printf(" *\n"); } void showfour() { printf(" * * \n\n"); printf(" * * \n"); } void showfive() { printf(" * * \n"); printf(" * \n"); printf(" * * \n"); } void showsix() { int i; for(i=1 ; i>=3 ; i++) printf(" * * \n"); } The problem I am having is I can't figure how to make it loop back to the y/n. How it is now it goes on forever. How can I make it ask "Throw y/n" each time? Thanks -Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

            M Offline
            M Offline
            mahade1
            wrote on last edited by
            #5

            Hi, Try doing this: void showone(); void showtwo(); void showthree(); void showfour(); void showfive(); void showsix(); char getans(); void printlines(int n); void main() { int r; char ans; srand( (unsigned)time( NULL ) ); ans = getans(); while(ans== 'y') { r = rand()%6 + 1; printlines(2); if (r==1) showone(); if (r==2) showtwo(); if (r==3) showthree(); if (r==4) showfour(); if (r==5) showfive(); if (r==6) showsix(); printlines(2); ans = getans(); } printlines(2); } char getans() { int ans; fflush(stdin ); printf("Throw y/n ?"); ans = -1; while (ans == -1) { ans=getchar(); } return ans; } void printlines(int n) { int i; for(i=1 ; i<=n ; i++) printf("\n"); } void showone() { printf("\n * \n"); } void showtwo() { printf(" * \n\n"); printf(" * \n"); } void showthree() { printf(" * \n"); printf(" * \n"); printf(" *\n"); } void showfour() { printf(" * * \n\n"); printf(" * * \n"); } void showfive() { printf(" * * \n"); printf(" * \n"); printf(" * * \n"); } void showsix() { int i; for(i=1 ; i<=3 ; i++) printf(" * * \n"); } Regards, Mahadevan

            M 1 Reply Last reply
            0
            • M Mark 0

              Still working on my program. And here is what I got so far: #include #include void showone(); void showtwo(); void showthree(); void showfour(); void showfive(); void showsix(); char getans(); void printlines(int n); main() { int r; char ans; ans = getans(); while(ans== 'y') { r = rand()%6 + 1; printlines(2); if (r==1) showone(); if (r==2) showtwo(); if (r==3) showthree(); if (r==4) showfour(); if (r==5) showfive(); if (r==6) showsix(); printlines(2); } printlines(2); } char getans() { int ans; printf("Throw y/n ?"); ans = -1; while (ans == -1) { ans=getchar(); } return ans; } void printlines(int n) { int i; for(i=1 ; i<=n ; i++) printf("\n"); } void showone() { printf("\n * \n"); } void showtwo() { printf(" * \n\n"); printf(" * \n"); } void showthree() { printf(" * \n"); printf(" * \n"); printf(" *\n"); } void showfour() { printf(" * * \n\n"); printf(" * * \n"); } void showfive() { printf(" * * \n"); printf(" * \n"); printf(" * * \n"); } void showsix() { int i; for(i=1 ; i>=3 ; i++) printf(" * * \n"); } The problem I am having is I can't figure how to make it loop back to the y/n. How it is now it goes on forever. How can I make it ask "Throw y/n" each time? Thanks -Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

              H Offline
              H Offline
              Henry miller
              wrote on last edited by
              #6

              Basicly what the other two guys said, except you might want a do/while loop and not a standard while loop. Look it up, the do while will always give you one roll before asking if you want to roll again. I recomend "the art of computer programing" by Donald Knuth http://www-cs-faculty.stanford.edu/~knuth/taocp.html which describes random numbers in far more detail than you ever thought possible. These are unfortuntly advanced books, completing one should be considered equivelant to a PHD! Still you should be aware of them, and (if you can afford them) have them on the bookshelf. What I want you to get from these books: random numbers have been the most popular subject in advanced computer science since before there was comptuer science. I estimate that 1/3rd of all PHDs in computer science studied random numbers (and there is still pleny of room for more study) Your simple program is not complete until you understand something about random numbers. In particular, the most common implimentation of rand() will ALWAYS give a odd-even-odd-even sequence! This is not something you want I'm going to guess. Issues like this are what seperate simple programs like yours (that look just fine) from complex programs done by an expert. You can never know it all, but I've just given you an area to look.

              1 Reply Last reply
              0
              • M mahade1

                Hi, Try doing this: void showone(); void showtwo(); void showthree(); void showfour(); void showfive(); void showsix(); char getans(); void printlines(int n); void main() { int r; char ans; srand( (unsigned)time( NULL ) ); ans = getans(); while(ans== 'y') { r = rand()%6 + 1; printlines(2); if (r==1) showone(); if (r==2) showtwo(); if (r==3) showthree(); if (r==4) showfour(); if (r==5) showfive(); if (r==6) showsix(); printlines(2); ans = getans(); } printlines(2); } char getans() { int ans; fflush(stdin ); printf("Throw y/n ?"); ans = -1; while (ans == -1) { ans=getchar(); } return ans; } void printlines(int n) { int i; for(i=1 ; i<=n ; i++) printf("\n"); } void showone() { printf("\n * \n"); } void showtwo() { printf(" * \n\n"); printf(" * \n"); } void showthree() { printf(" * \n"); printf(" * \n"); printf(" *\n"); } void showfour() { printf(" * * \n\n"); printf(" * * \n"); } void showfive() { printf(" * * \n"); printf(" * \n"); printf(" * * \n"); } void showsix() { int i; for(i=1 ; i<=3 ; i++) printf(" * * \n"); } Regards, Mahadevan

                M Offline
                M Offline
                Mark 0
                wrote on last edited by
                #7

                What does the fflush() function do? Thanks -Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

                M 1 Reply Last reply
                0
                • M Mark 0

                  What does the fflush() function do? Thanks -Mark As you journey through life take a minute every now and then to give a thought for the other fellow. He could be plotting something.-Hagar the Horrible

                  M Offline
                  M Offline
                  mahade1
                  wrote on last edited by
                  #8

                  The fflush() function flushes the console input before u use getchar().

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups