Click here to Skip to main content
15,562,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This program rolls two dice and presents the total. It then asks
the user to guess if the next total will be higher, lower, or equal.
It then rolls two more dice and tells the user how they did.

What I have tried:

/* This program rolls two dice and presents the total. It then asks
the user to guess if the next total will be higher, lower, or equal.
It then rolls two more dice and tells the user how they did. */

#include<stdio.h>;
#include<time.h>;
#include<ctype.h>;


main()
{
      int dice1 ,dice2 ,total1,total2;
      char answer ,quit;
      time_t nasamajh;
    // now for different random numbers each time:
      srand(time(&nasamajh));
do
{
      dice1 = (rand()%6)+1;
      dice2 = (rand()%6)+1;
      total1 = dice1 + dice2;
      printf("Your 1st dice roll is %d and 2nd dice roll is %d\n", dice1,dice2);
      printf("That makes a total of %d\n", total1);

      puts("\n\nDo you think your next dice will be");
      puts("(H)igher, (L)ower or (S)ame as the 1st one?");
      puts("Enter H, L or S");
      scanf(" %c", &answer);
      answer = toupper(answer);

      dice1=(rand()%6+1);
      dice2=(rand()%6+1);
      total2 = dice1 + dice2;

      if(answer == 'H')
      {
          if(total2>total1)
          {
              puts("You were right");
              printf("Your second dices rolled are %d and %d respectively ", dice1,dice2);
              printf("that makes a total of %d ", total2);

          }
          else
          {
              printf("the 2nd combo of dices rolled are %d and %d", dice1,dice2);
              printf("Sorry, your 2nd combo %d is not higher than the first one i.e %d", total2,total1);
          }

      }

      if(answer == 'L')
      {
          if(total2<total1)
          {
              puts("You were right");
              printf("Your second dices rolled are %d and %d respectively ", dice1,dice2);
              printf("that makes a total of %d ", total2);

          }
          else
          {
               printf("Your second dices rolled are %d and %d respectively \n", dice1,dice2);
              printf("Sorry, your 2nd combo %d is not lower than the first one i.e %d \n", total2,total1);
          }

      }

      if(answer == 'S')
      {
          if(total2==total1)
          {
              puts("You are right!");
              printf("Your second dices rolled are %d and %d respectively \n", dice1,dice2);
              printf("that makes a total of %d \n", total2);

          }
          else
          {
               printf("Your second dices rolled are %d and %d respectively\n ", dice1,dice2);
              printf("Sorry, your 2nd combo %d is not the same as the first one i.e %d \n", total2,total1);
          }

      }

   puts("Do u want to play the dice rolling game again (Y/N)?");
   scanf(" %c", &answer);
   answer = toupper(answer);
         if(answer =='y')
         {
             continue;
         }
         else
         {
             break;
         }
} while(answer =='Y');


    return 0;
}
Posted
Updated 26-Aug-22 4:39am
Comments
Dave Kreskowiak 26-Aug-22 9:23am    
And you had a question or a problem with this code? "Not working" is not a problem description. Describe what happens and what you expect to happen.
Sharyar Javaid 26-Aug-22 9:40am    
i mean its not looping! it works one time and then terminates
CPallini 26-Aug-22 9:33am    
A wild guess:
https://stackoverflow.com/questions/33687758/problems-with-standard-i-o-scanf

See here:
C
   puts("Do u want to play the dice rolling game again (Y/N)?");
   scanf(" %c", &answer);
   answer = toupper(answer);
         if(answer =='y')
         {
             continue;
         }
         else
         {
             break;
         }
} while(answer =='Y');

There are 3 mistakes here:
1) the if block tests against a lower case 'y', but you've previously set answer tu uppercase
2) th if block is unneeded, as it duplicates the test of the do...while loop, error 1 notwithstanding
3) scanf("%c", &answer) is not going to do what you expect. The first time through the loop, it will read the first character from the input buffer. Lets assume that its a y and so the loop continues on. The next time through the loop the scanf() call will read the next char in the input buffer. In this case it will be the newline ('\n') from the previous input, or if the user entered 'yes', then the scanf() call will return 'e' from the input buffer. Better to read the entire input buffer (e.g. scanf("%s", buffer)), and then test the first char.
 
Share this answer
 
Comments
Sharyar Javaid 26-Aug-22 9:59am    
thanks! I changed the 'y' to capital 'Y' and it works now also yes if I get rid of the if statement, it works then too! but your 3rd point I could not understand as I'm still halfway throught the book "absolute beginner's guide to C by greg perry" and not yet gone through the buffer
merano99 26-Aug-22 10:26am    
Wouldn't it be much better to use the fgets() function instead of scanf("%s",..)?
Stepping through the code in the debugger will show you what's going on.

Look up what "continue" and "break" do, then step through the code in the debugger. You'll should see what's wrong immediately. Hint: You don't need the "if" statement at the end of the loop, at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900