#38 challenge 2 : Dice game (help please!)

+1 Ellie Shin · March 15, 2015

My code would always print "Try harder" as if
if(guess == 'h' && sum<sum2) these if statements dont get through
and i always get the same sum everytime i run it.


Can anyone please help me with my code D:


(and the hearts on the bottom are supposed to be i < 3...)

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
    // get the sum of 3 dice prnt out the total
    // use guess if its higher (h) or lower or the same
    // roll the dice again
    // if right, good job wrong, you suck
    int i = 0;
    int sum = 0;
    char guess[2];
    for(i = 0; i<3; i++){
        sum +=(rand()%6)+1;
        if(i==2){
            printf("The sum is %d \n",sum);
        }
    }
    puts("Enter your guess for the next sum: higher, lower, or the same. (h/l/s)");
    gets(guess);
    int sum2 = 0;
    for(i = 0; i<3; i++){
        sum2 +=(rand()%6)+1;
        if(i==2){
            printf("The sum is %d \n",sum2);
        }
    }
    printf("%s\t%d\t%d \n",guess,sum,sum2);
    if(guess == 'h' && sum<sum2){
        printf("Good job!");
    }else if(guess == "l" && sum>sum2){
        printf("Good job!");
    }else if(guess == 's' && sum==sum2){
        printf("Good job!");
    }else{
        printf("Try harder!");
    }

    return 0;
}



Post a Reply

Replies

Oldest  Newest  Rating
+1 Dol Lod · March 15, 2015
You have inconsistent code regarding comparisons. First, you compare the value at guess[0] with the character h, then compare guess with the string l, not the character l. Please be careful when writing your code. I decided to make the code less ambiguous.  You are also using unnecessary libraries like string. I think you should reduce down to the libraries you actually use. 

To fix the for loop comparison now showing up, I replaced <3 with <=2. If you want to, change it again while testing. 


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
// get the sum of 3 dice prnt out the total
// use guess if its higher (h) or lower or the same
// roll the dice again
// if right, good job wrong, you suck
int i = 0;
int sum = 0;
char guess[2];
for(i = 0; i<=2; i++)
{
sum +=(rand()%6)+1;
}
printf("The sum is %d \n",sum);
puts("Enter your guess for the next sum: higher, lower, or the same. (h/l/s)");
gets(guess);
guess[1]='\0';
int sum2 = 0;
for(i = 0; i<=2; i++)
{
sum2 +=(rand()%6)+1;
}
printf("The sum is %d \n",sum2);
printf("%s\t%d\t%d \n",guess,sum,sum2);
if(guess[0] == 'h' && sum<sum2)
{
printf("Good job!");
}
else if(guess[0] == 'l' && sum>sum2)
{
printf("Good job!");
}
else if(guess[0] == 's' && sum==sum2)
{
printf("Good job!");
}
else
{
printf("Try harder!");
}

return 0;
}
0 Ellie Shin · March 15, 2015
i used both the string and the char to try differnt things and forgot to fix it back

thank you so much! :)
0 OLayemii Garuba · March 16, 2015
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <ctime>
#include <math.h>
int main()
{
// get the sum of 3 dice prnt out the total
// use guess if its higher (h) or lower or the same
// roll the dice again
// if right, good job wrong, you suck
srand (time (NULL));
int i = 0;
int sum = 0;
char guess;
for(i = 0; i<3 ; i++){
sum +=(rand()%6)+1;
if(i==2){
printf("The sum is %d \n",sum);
}
}
puts("Enter your guess for the next sum:higher, lower, or the same.(h/l/s)");
scanf("%c",&guess);
int sum2 = 0;
for(i = 0; i <3;i++){
sum2 +=(rand()%6)+1;
if(i==2){
printf("The sum is %d \n",sum2);
}
}

printf("%c\t%d\t%d\n",guess,sum,sum2);
if (guess == 'h' && sum2 > sum)
{
printf("Good job!");
}
else
if (guess == 'l' && sum>sum2){
printf("Good job!");
}
else
if (guess== 's' && sum==sum2){
printf("Good job!");
}
else
{
printf("Try harder!");
}
return 0;
}
this works!!
0 OLayemii Garuba · March 16, 2015
and it keeps bringing up same sum cause you failed to seed your random numbers that's where //stand (time(NULL)) comes in place!
0 Jacky Leung · March 29, 2015
#include <stdio.h>
#include <math.h>

int ThrowDice(){
    int dice, sum, i;
    puts("Throwing dice...");
    sum = 0;
    for (i = 0; i < 3; i++)
        sum += rand() % 6 + 1;
    return sum;
}

int main()
{
    int checkpoint, thistime;
    char ans, corr;

    thistime = ThrowDice();
    puts("\n****************************\n");
    do{
        checkpoint = thistime;
        printf("Last time: %d \n", checkpoint);
        puts("How about this time: Higher(h)? Lower(l)? Same(s)? Give up(g)?");
        scanf(" %c", &ans);
        if (ans != 'h' && ans != 'l' && ans != 's')
            break;
        thistime = ThrowDice();

        printf("This time: %d \n", thistime);
        if (thistime>checkpoint){
            corr = 'h';
            printf("Higher - ");
        }else if (thistime<checkpoint){
            corr = 'l';
            printf("Lower - ");
        }else{
            corr = 's';
            printf("Same - ");
        }
        ans == corr ? puts("Congratulations! You are right!") : puts("You are wrong!");
        puts("\n****************************\n");
    }while(1);

    puts("Goodbye loser!");
    return 0;
}

this works!
0 Akshay Pimpalkar · May 13, 2015

#include
#include
#include
#include

int main()
{
    int i;
    int diceRoll1, diceRoll2, diceRoll3;
    int sum = 0;
    int newSum = 0;
    char letter;

    for(i=0; i sum ){
            printf("you are right \n\n");
            continue;
        }else{
            printf("you are wrong\n\n");
        }
        }

        if ( letter == 'l'){
        if ( newSum < sum ){
            printf("you are right \n\n");
            continue;
        }else{
            printf("you are wrong\n\n");
        }
        }

        if ( letter == 's'){
        if( newSum == sum ){
            printf("you are right\n\n");
            continue;
        }else{
            printf("you are wrong\n\n");
        }
        }

    }
    return 0;
}

check out this code for dice game guys..... let me know your comments. 
  • 1

C

107,030 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator