Help with this simple task? from the tutorials

0 Pieter Post · May 30, 2015
So I was following bucky's youtube tutorial on C and i couldn't find where to post this, so i'll post it here and hope anyone who can help will see. :P



The challenge was:

Create a dice game, where the user rolls 3 dice, and then guess if the next set of rolls will be higher, lower or the same.
This is what I came up with, the only problem is for some reason i can not get the input of the guesses to work (h,l,s)
I really don't know why, if anyone could help fix this game it would be really appreciated. :)

-Bla365

The code I made:


#include
#include
#include
#include
#include

int main()
{
    int i = 0;
    int diceResult;
    int diceTotal = 0;
    int diceTotal2 = 0;
    int guess = "l";

    //Throw 1
    for (i = 0; i

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>

int main()
{
    int i = 0;
    int diceResult;
    int diceTotal = 0;
    int diceTotal2 = 0;
    int guess = "l";

    //Throw 1
    for (i = 0; i <= 2; i++)
        {
            diceResult = ( rand()%6 ) + 1;
            printf("You rolled %d! \n", diceResult);
            diceTotal += diceResult;
        }
    //End of throw 1
    i = 0;
    diceResult = 0;
    printf("You rolled %d in total this turn!\n \n", diceTotal);
    printf("Wil you roll higher, lower or the same this time?(h/l/s)\n");
    scanf("%d", guess);
    printf("%s\n", guess);

    //Throw 2
    for (i = 0; i <= 2; i++)
        {
            diceResult = ( rand()%6 ) + 1;
            printf("You rolled %d! \n", diceResult);
            diceTotal2 += diceResult;
        }
    printf("You rolled %d in total this turn!\n \n", diceTotal2);

    //Check if the answer was correct
    if (guess == 'h')
    {
        printf("H \n");
        if (diceTotal2 > diceTotal)
        {
            printf("YOU GUESSED CORRECTLY \n");
        }
        else
        {
            printf("YOU SUCKh \n");
        }
    }

    else if (guess == 'l')
    {
        printf("L \n");
        if (diceTotal2 < diceTotal)
        {
            printf("YOU GUESSED CORRECTLY \n");
        }
        else
        {
            printf("YOU SUCKl \n");
        }
    }

    else
    {
        if (diceTotal2 == diceTotal)
        {
            printf("YOU GUESSED CORRECTLY \n");
        }
        else
        {
            printf("YOU SUCKs \n");
        }
    }

    printf("diceResult: %d \n ", diceResult);
    printf("diceTotal: %d \n ", diceTotal);
    printf("diceTotal2: %d \n ", diceTotal2);
    printf("guess: %s \n ", guess);



    return 0;
}



Post a Reply

Replies

Oldest  Newest  Rating
0 c student · May 31, 2015
i don't know what the first section of your code is doing there. anyway, there are some bugs in your code that should be fixed.
your guess declaration is incorrect:
int guess = "l";    /* use single quote instead of double */

although it is valid as an int, it might be more easier and make more sense to deal with it as a character type.  also, "l" is a string consisting of the character followed by a null terminator: l\0 which is not of type int or char.

your scanf() is kinda off:
scanf("%d", guess);    

your input is of type char but you are scanning it as an int.  if you happened to check the return of scanf(), it will be 0, meaning nothing will have scanned.  also, you should be scanning into the address of the variable.  like so:
if (!scanf("%d", &guess)) {    /* this should be %c */
   fprintf (stderr, "No elements scanned\n");
   // do something about it
}

manpage for scanf(): http://linux.die.net/man/3/scanf

your next line also has an issue:
printf("%s\n", guess);

found again at the end:
printf("guess: %s \n ", guess);

you attempt to print the variable as a string but it is only of type int which isn't of type char *.  it shouldn't even compile properly.
manpage for printf(): http://linux.die.net/man/3/printf
  • 1

C

107,192 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator