guess number of dice-C program exercise

0 alex he · March 5, 2015
hi, Mr. Bucky. I like your video very much. I just started to study C programming and I did your exercise for dice and met some trouble. Could you help me to find out what my problem is?  Every time I run the program,it can only run once and I have to restart it again.








#include <stdio.h>
#include <stdlib.h>


int main()
{
    int i;
    int dicenumber;
    int guess;

    dicenumber = (rand()%6)+1;


    do{
    printf("guess a number(1-6)\n");
    scanf(" %d", &guess);

    if(guess == dicenumber){
        printf("time: %d congratulations\n",i);
    }
    if(guess < dicenumber){
        printf("time: %d too low\n",i);
    }
    if(guess > dicenumber){
        printf("time: %d too high\n",i);
    }
    i++;
    }while(guess == dicenumber);



    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Dol Lod · March 5, 2015
On a side note, initialize i to 0. 

I think I figured out the problem. 

while(guess == dicenumber);

should be 
while(guess!=dicenumber)

This is under my assumption that the user continues guessing until they guess the random number. Try to be more careful about the conditions in a while loop. Those conditions should be a trigger for repeating a pattern, not breaking out of a pattern. 
  • 1

C

107,332 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator