rand dice game problems, any thoughts?

+1 Canonsburg Don · November 11, 2015
I have a couple of problems so far.
1 - My code is not producing random numbers.  The numbers are the same every time the program is run.
2 - I'm using if - if else - else to determine win or lose and print appropriate message.  However, the printed outcome is always the same no matter what the prediction is, higher, lower or the same.  I've tried restructuring the if else statements because they seem to be overlooked but that did not help.
3 - Finally, I print a line at the end to display variables and score2 somehow is at 0.  I cannot see how score2 is being set to 0 (from 16).


#include
#include
#include
#include
#include

int main()
{
    int d1;
    int d2;
    int d3;
    char guess;
    char h;
    char l;
    char s;
    int score1 = 0;
    int score2 = 0;

      d1 = ( rand()%6 ) +1;
      d2 = ( rand()%6 ) +1;
      d3 = ( rand()%6 ) +1;
      score1 = d1+d2+d3;
      printf("\n\n %d, %d, %d, total score is %d", d1,d2,d3,score1);
      d1 = d2 = d3 = 0;
      printf("\n\n Will the sum of your next roll be H(igher), L(ower), or the S(ame)? ");
      scanf(" %c", &guess);

      printf("\n\n Your guess is %c.\n\n", (guess));

      d1 = ( rand()%6 ) +1;
      d2 = ( rand()%6 ) +1;
      d3 = ( rand()%6 ) +1;
      score2 = d1+d2+d3;
      printf(" %d, %d, %d, total score is %d\n", d1,d2,d3,score2);

    if(score2 > score1 && guess == h){
        printf("\n You win!");
    }else if (score2 = score1 && guess == s){
        printf("\n You win!");
    }else if (score2 < score1 && guess == l){
        printf("\n You win!");
    }else{
        printf("\n Loser!\n");
        printf(" Guess was %c, Score1= %d, Score2= %d,\n", guess, score1, score2);
    }
    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Canonsburg Don · November 11, 2015
    OK, I found the problem with the rand function (item 1) can be cured by seeding the random number generator with 
   srand(time(NULL)).  I wonder if Bucky's example would have delivered random after the first run?

   I worked out items 2 & 3 after checking Omar's code in the rand thread started by Bucky.  My problem was that I wasn't properly referencing the prediction string character.  

   The code works now.  See below. 



#include
#include
#include
#include
#include

int main()
{
    int d1;
    int d2;
    int d3;
    char guess[1];
    char h;
    char l;
    char s;
    int score1 = 0;
    int score2 = 0;
    srand(time(NULL));

      d1 = ( rand()%6 ) +1;
      d2 = ( rand()%6 ) +1;
      d3 = ( rand()%6 ) +1;
      score1 = d1+d2+d3;
      printf("\n\n %d + %d + %d = %d", d1,d2,d3,score1);
      d1 = d2 = d3 = 0;
      printf("\n\n Will the sum of your next roll be H(igher), L(ower), or the S(ame)? ");
      scanf(" %s", guess);

      if(guess[0] == 'h'){
        printf("\n\n You guessed HIGHER\n\n");
      }
      else if(guess[0] == 'l'){
        printf("\n\n You guessed LOWER\n\n");
      }else{
        printf("\n\n You guessed SAME\n\n");
        }
      //printf("\n\n Your guess is %s.\n\n", (guess));

      d1 = ( rand()%6 ) +1;
      d2 = ( rand()%6 ) +1;
      d3 = ( rand()%6 ) +1;
      score2 = d1+d2+d3;
      printf(" %d + %d + %d = %d\n", d1,d2,d3,score2);
      printf("\n Roll 1 = %d\n\n Roll 2 = %d\n", score1, score2);

    if ((score1 < score2) && (guess[0] == 'h')){
        printf("\n       YOU WIN
               !\n");
    }
    else if ((score1 == score2) && (guess[0] == 's')){
        printf("\n       YOU WIN!\n");
    }
    else if ((score1 > score2) && (guess[0] == 'l')){
        printf("\n       YOU WIN!\n");
    }else{
        printf("\n       LOSER!\n");
    }
    return 0;
}
0 Canonsburg Don · November 11, 2015
Does anyone else have a problem with the big C overlaying the text in the top thread post, making it unreadable?
Or is it just me?
  • 1

C

107,193 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator