Code:#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main() {
int dice1;
int dice2;
int dice3;
int sumold;
int sumnew;
int runtime;
char user_guess[10];
runtime = 1;
while(runtime==1) {
srand(time(NULL));
dice1 = (rand() % 6) + 1;
dice2 = (rand() % 6) + 1;
dice3 = (rand() % 6) + 1;
sumold = dice1 + dice2 + dice3;
printf("You have rolled:\n%d %d %d\nYour total is: %d\n", dice1, dice2, dice3, sumold);
printf("\nWill your next total be higher, lower or the same (q to quit)? (h/l/s/q): ");
while(runtime==1) {
gets(user_guess);
srand(time(NULL));
dice1 = (rand() % 6) + 1;
dice2 = (rand() % 6) + 1;
dice3 = (rand() % 6) + 1;
sumnew = dice1 + dice2 + dice3;
printf("\nYou have rolled:\n%d %d %d\nYour total is: %d\n", dice1, dice2, dice3, sumnew);
if((user_guess=="h" || user_guess=="H") && sumnew > sumold) {
printf("\nYOUR PREDICTION WAS CORRECT!!\n");
}
else if((user_guess=="l" || user_guess=="L") && sumnew < sumold) {
printf("\nYOUR PREDICTION WAS CORRECT!!\n");
}
else if((user_guess=="s" || user_guess=="S") && sumold == sumnew) {
printf("\nYOUR PREDICTION WAS CORRECT!!\n");
}
else if(user_guess=="q" || user_guess=="Q") {
runtime = 0;
}
else {
printf("\nYOUR PREDICTION WAS WRONG!!\n");
}
sumnew = sumold;
printf("\nWill your next total be higher, lower or the same (q to quit)? (h/l/s/q): ");
}
}
printf("Congratulations on completing the game!");
return 0;
}
Supposed to be an easy challenge that I found in a Youtube tutorial (YouTube). I'm very new to C so its probably a simple mistake but i just can't get it to work. The program is intended to function like this:
1. Print out 3 random dice numbers and a total
2. Take input from the user as to whether the next 3 rolls will produce a sum that is higher/lower/same as previous roll (or quit)
3. Print out 3 more random dice numbers and a total
4. Let the user know if their prediction was correct or allow them to exit the game
5. If the user stays, back to step 2
The intended output is this:
OR this:Code:You have rolled:
1 1 2
Your total is: 4
Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): h
You have rolled:
3 5 3
Your total is: 11
YOUR PREDICTION WAS CORRECT!!
But this is the output im getting with the code above:Code:You have rolled:
1 1 2
Your total is: 4
Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): q
You have rolled:
3 5 3
Your total is: 11
Congratulations on completing the game!
Im using codeblocks as an IDE and I tried the debugging function. The program seems to be skipping the "if" nest entirely except for the "wrong prediction part".Code:You have rolled:
1 1 2
Your total is: 4
Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): h
You have rolled:
3 5 3
Your total is: 11
YOUR PREDICTION WAS WRONG!!
Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): l
You have rolled:
2 3 4
Your total is: 9
YOUR PREDICTION WAS WRONG!!
Will your next total be higher, lower or the same (q to quit)? (h/l/s/q): q
You have rolled:
1 6 5
Your total is: 12
YOUR PREDICTION WAS WRONG!!
First post on this forum so sorry if its too long or badly formatted. Thanks :)