home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
magazine
/
c_news
/
17
/
dice.tc
< prev
next >
Wrap
Text File
|
1989-09-01
|
2KB
|
52 lines
/* DICE.C -- A program to simulate dice rolling. The program rolls
the number of dice specified by the user on the command line.
This program is part of an article illustrating command-line
parameters in issue 17 of the C News. */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main(int argc, char *argv[]) /* Pass the number of dice to be rolled
when calling the program. The number
will be passed as argv[1]. (argv[0]
is the program name.) */
{
int roll, dice, count = 0; /* Initialize variables for each roll
(roll), the number of dice (dice),
and a counter (count) which is set
to 0. */
if(argc == 1) /* If no parameter for the number of dice was
passed when calling the program, print instruc-
tions for use and terminate program. */
{
puts("\nProper format is:");
puts("\n dice n");
puts("\nand n is an integer representing the number of ");
puts("dice to be rolled.\n");
exit(0);
}
dice = atoi(argv[1]); /* Convert the string value for the number,
passed from the command-line as argv[1],
of dice to an integer value. */
randomize(); /* Initialize the random number generator with a
random number. */
while(count < dice) /* Using the number of dice, generate a
random number for each "roll" of the
dice and display the value to the
screen. When the variable count equals
the number of dice requested, the loop
terminates (as does the program). */
{
roll = random(6); /* Use standard six-sided dice. */
printf("The roll of dice number %d is %d", count+1, ++roll);
printf("\n");
count++;
}
}