Challenge #2 done? Also I have a few questions!

+1 Jacky L · February 19, 2015
So here is my code:


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

    int main(void)
    {
        int i;
        int roll1;
    int roll2;
    int sum1 = 0;
    int sum2 = 0;
    char input;

    for( i = 0; i < 3; i++ ) {
        roll1 = ( rand()%6 ) + 1;
        printf( "Roll number #%d. ", i+1);
        printf( "You rolled a %d.\n", roll1);
        sum1 += roll1;
    }

    printf("Sum: %d\n", sum1);

    printf("Will your next dice roll sum be higher, lower, or the same as %d? (h/l/s)\n", sum1);
    scanf("%s", &input);

    i = 0;
    for( i = 0; i < 3; i++ ) {
        roll2 = ( rand()%6 ) + 1;
        printf( "Roll number #%d. ", i+1);
        printf( "You rolled a %d.\n", roll2);
        sum2 += roll2;
    }

    printf("New sum: %d\n", sum2);

    //higher
    if (sum2 > sum1){
        if(input == 'h'){
            printf("Lucky guess\n");
        }
        else{
            printf("You suck!\n");
        }
    }

    //lesser
    if (sum2 < sum1){
        if(input == 'l'){
            printf("Lucky guess!\n");
        }
        else{
            printf("You suck!\n");
        }
    }

    //same
    if (sum2 == sum1){
        if(input == 's'){
            printf("Lucky guess!\n");
        }
        else{
            printf("You suck!\n");
        }
    }

    return 0;
}


So I don't really get a random number each time my program is executed. I keep getting 6,6,5 as my first roll and then 6,5,6 for my second roll. Why is this happening?
Also when am I supposed to use the '&' symbol? At first I didn't have it and the code wasn't working, and then I tried changing my 'char input' to 'char input[1]'. Why do these not work?

Post a Reply

Replies

Oldest  Newest  Rating
0 Jacky L · February 19, 2015
Sorry this is a duplicate post, tried to delete/edit last post but I have no idea if it's possible.
  • 1

C

106,953 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator