Kai Lu
Kai Lu

Reputation: 1

C Program Crashing, not sure why?

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

int main () {

int dice1; //declaring variables
int dice2;
int dice3;
int dice4;
int dice5;
int rolls = 0;
int diceRolls = 0;
bool reroll1 = false;
bool reroll2 = false;
bool reroll3 = false;
bool reroll4 = false;
bool reroll5 = false;
char confirm1; //no arrays, as mentioned in the rubric :) ...
char confirm2;
char confirm3;
char confirm4;
char confirm5;

srand(time(0)); //seeding

printf("DICE 1\t"); //formatting so the users know which dice is which for later
printf("DICE 2\t");
printf("DICE 3\t");
printf("DICE 4\t");
printf("DICE 5\n");

dice1 = rand() % 6 + 1;
printf("%d\t", dice1);

dice2 = rand() % 6 + 1;
printf("%d\t", dice2);

dice3 = rand() % 6 + 1;
printf("%d\t", dice3);

dice4 = rand() % 6 + 1;
printf("%d\t", dice4);

dice5 = rand() % 6 + 1;
printf("%d\t\n", dice5);

while ((dice1 != dice2) || (dice2 != dice3) || (dice3 != dice4) || (dice4 != dice5)) { //The loop determines that if Yahtzee is not achieved, it will reroll until it is

    rolls ++;

    printf("Reroll die 1?(y/n)\n");
    scanf("%c", confirm1);
    if (tolower(confirm1) == 'y') {
       reroll1 = true;
       dice1 = rand() % 6 + 1;
    }

    printf("Reroll die 2?(y/n)\n");
    scanf("%c", confirm2);
    if (tolower(confirm2) == 'y') {
       reroll2 = true;
       dice2 = rand() % 6 + 1;
    }

    printf("Reroll die 3?(y/n)\n");
    scanf("%c", confirm3);
    if (tolower(confirm3) == 'y') {
       reroll3 = true;
       dice3 = rand() % 6 + 1;
    }

    printf("Reroll die 4?(y/n)\n");
    scanf("%c", confirm4);
    if (tolower(confirm4) == 'y') {
       reroll4 = true;
       dice4 = rand() % 6 + 1;
    }

    printf("Reroll die 5?(y/n)\n");
    scanf("%c", confirm5);
    if (tolower(confirm5) == 'y') {
       reroll5 = true;
       dice5 = rand() % 6 + 1;
    }

    if (reroll1) {
        printf("%d\t", dice1);
    } 

    if (reroll2) {
        printf("%d\t", dice2);
    }

    if (reroll3) {
        printf("%d\t", dice3);
    }

    if (reroll4) {
       printf("%d\t", dice4);
    }

    if (reroll5) {
        printf("%d\t\n", dice5);
    }       
}

system("cls"); //clear screen for a better look

diceRolls = rolls * 5; //calculating dice rolls

printf("STATS\n"); //shows user stats
printf("Amount of rolls to achieve Yahtzee!: %d\n", rolls);
printf("Number of dice rolled: %d", diceRolls);

return 0;
}

Sorry about the indenting first of all. It is not so neat. This program generates "rolls" 5 die, and tries to get a Yahtzee. The user then is given the option to decide what to reroll. No arrays are allowed in this program, or functions. Now, on to the question. I am having a problem with my code as the program just crashes the first time I try to enter something, which is right after the while loop. When I tried to debug, it seems like scanf is the issue here, but I'm not sure what to change to let the code function, and am not sure what I am doing wrong in the code, since I've been learning C at school for close to a month now.

Thank you,

Kai

Upvotes: 0

Views: 97

Answers (1)

Shivakumar
Shivakumar

Reputation: 477

scanf takes the address of the variable to write user input to the variable. You are only passing the variable name (value) and not the address. & is the operator, which when prefixed to the variable name, gives address of that variable.

So correct way of using scanf is as below and this would solve your problem. Use the same for other scanf statements in your program.

scanf("%c", &confirm1);

Upvotes: 6

Related Questions