Challenge #2 Help !!!

0 Dimitar Matev · September 11, 2014
Hi guys , this code that i am posting is a part from Challenge #2 and i have problem, so plss help.

I got everything working (no errors) , but i saw that my program is throwing wrong answers :(


#include <stdio.h>

int main(){
    int i,diceRoll;
    char c;
    diceRoll = (rand()%6) +1;
    for(; c!='f' ;){
        i = diceRoll;
        diceRoll = (rand()%6) +1;
        printf("Is the next number higher , lower or same (h/l/s)(press 'f' to quit) than %d\n", diceRoll);
        scanf(" %c",&c);
        if(c=='h' && i<diceRoll || c=='l' && i>diceRoll || c=='s' && i==diceRoll ){
            printf("Good Job!The number is %d\n", diceRoll);
        }else{printf("Wrong!The number is %d\n", diceRoll);}

    }


return 0;
}

and i don't know what is wrong :(
I will be happy if someone help me :)

Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · September 11, 2014

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

int main(){
int i = 0, diceRoll = 0;
char c = 0;
srand(time(NULL));
diceRoll = (rand() % 6) + 1;
while (c != 'f'){
i = diceRoll;
diceRoll = (rand() % 6) + 1;
printf("Is the next number higher , lower or same (h/l/s)(press 'f' to quit) than %d\n", i); //you made diceRoll as the number which is next so why do you output it here
scanf_s(" %c", &c, 1);
if (c == 'h' && i < diceRoll || c == 'l' && i > diceRoll || c == 's' && i == diceRoll){
printf("Good Job!The number is %d\n", diceRoll);
}
else{ printf("Wrong!The number is %d\n", diceRoll); }
}

return 0;
}

You did output the wrong number in your printf line.
and you should use srand so the random number is really always random.
0 Dimitar Matev · September 11, 2014
Thank you for your help!!! Much appreciated :)
  • 1

C

107,265 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator