Hi there y'all I have written a simple program that lets you roll a dice. So in one way I am passing this and in another I would like anyone capable of finding a bug, or check my code if I could have used less lines of code or Tell me if I have used bad programming practices. Thanks in advance! :)

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

void Dice(int[],int); // dice function
int main(int argc, const char * argv[])
{
   //Declaring variables for getting input, storing input in an array, and x for initializing the for loop which will initialize the array to be used
    int iInput;
    int diceArray[6];
    int x;

    //Getting input from user as to the number of dices he or she want rolled
    printf("Enter the number of dice to roll(1-6): ");
    scanf("%d", &iInput);

    //for loop for initializing the array the array to be used
    for( x = 0; x < 6; x++)
        diceArray[x] = x;

    Dice(&diceArray[x], iInput);//calling the array to caluculate the dice number and print out the stored dices

    return 0;
}//end main

void Dice(int diceArray[6], int iInput)
{
    //Initializing srand() inorder to user rand() to generate random numbers
    srand((unsigned)time(NULL));

    //Declaring the variable to be used
    int x;
    int diceroll;

    //Variable diceroll is initialized to calculate the random dice numbers to be calculated
    diceroll = (rand() % iInput) + 1;


    if (iInput <= 6 && iInput > 0){//used if statement to make sure the user has only option to enter 1 - 6 as the different times to roll the dice

        for ( x = 0; x < 6 ; x++ ){//for loop to initialize the array and store the value in the array

        diceroll = (rand() % iInput) + 1;
            diceArray[x] = diceroll;}
    }//end of if

    if (iInput <= 6 && iInput > 0){

        for( x = 0; x < iInput; x++ )//for loop for printing out the stored dice values
        printf("%d\n", diceArray[x]);
    }//end of if
    }// of Dice()

What is iInput meant to be? Name it for what it stands, perhaps it is number of guesses, number of throws. I don't know. If it is number of tries then call it Ntries or even better numberOfTries
After a few months when you look again at your program you will not know what iInput stands for, but numberOfTries will.

The if on line 39 should be used as test after the scan on line 15. You should loop there until the input is correct. Don't test for input in some function that does not input, but uses the input. Also let user know with a message what are the legal values to input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.