This is a simple program in C for taking in a person's choice of lotto numbers, asking them if they want to change their mind and if not printing them to the screen. It works but to get it to work, on the line marked * I had to increment the dummy variable "index" by 6. I would have thought by 1 would be sufficent. I seems by the time the program has got to line * the index has been zeroed. How has it been zeroed?
Again to reiterate it works as shown below but why must index be increment ed by 6/ why/how has the index been zeroed by line * ?
Thank you
# include<stdio.h>
main()
{
// DECLARE ARRAY WITH 6 ELEMENTS
int numb[5];
// DECLARE VARIABLES
/* 'Index' IS USED FOR COUNTING THROUGH THE ARRAY AND FOR
PRINTING “ENTER NUMBER 1”, “ENTER NUMBER 2” ETC. */
int index;
// CH IS USED FOR ACCEPTING ‘Y’ OR ‘N’ FROM USER
char ch;
printf("\n\t\t\t The National Lottery\n\n");
printf("\n\t\t\t You could be a winner \n\n");
// DO LOOP FOR TAKING IN 6 NUMBERS FROM THE USER WHILE INDEX IS <=5.
do
{
printf("Please choose your six numbers (0 - 60)\n\n");
// BEGINNING AT 0; CONTINUE WHILE INDEX IS <=5;INCREMENT
// INDEX BY 1 EACH TIME THROUGH THE LOOP IF ALL IS OK
for (index=0; index<=5; index=index+1)
{
/* INDEX+1 BECAUSE INDEX IS AT 0 (FOR ENTERING INTO THE ARRAY) BUT INDEX IS
ALSO USED TO ASK THE USER TO “ENTER NUMBER ?” SO IT SHOULD BE AT 1 SO THAT
“ENTER NUMBER 1” IS PRINTED, NEXT TIME THROUGH THE LOOP IT WILL PRINT “ENTER
NUMBER 2” THE CODE “INDEX + 1” ACHIEVES THIS.*/
printf("Enter number %d : ", index+1);
// SCAN IN THE NUMBER AND PUT IT IN THE ARRAY (INDEX IS CURRENTLY AT 0)
scanf("%d", &numb[index]);
/*WHEN THE USER ENTERS A NUMBER WHICH IS NOT BETWEEN 1-60, ASK THEM TO RE-ENTER */
if(numb[index]<1 || numb[index]>60)
{
printf("Please re-enter number %d (between 0 - 60)!!!\n", index+1);
index=index-1;
}
}
/*ASK THE USER IF THEY WANT TO ENTER 6 DIFFERENT LOTTO NUMBERS AGAIN, OR NOT*/
printf("\nDo you wish to enter six different lotto numbers [Y/N] ? ");
// SCAN THE USER'S REPLY, ENSURINGNO WHITESPACE CHARACTERS ARE ACCEPTED ("%LS")
// AND PUT THE ENTRY IN CH
scanf("%ls",&ch);
// USER WANTS 6 NEW NUMBERS
if (ch == 'y' || ch == 'Y')
{
index=0;
printf("\n");
}
// USER WANTS TO STICK WITH THEIR 6 NUMBERS
else if (ch == 'n' || ch == 'N') /*IF THE ENTER IS N, EXIT THE PROGRAM*/
{
// HERE IS WHERE INDEX IS INCREMENTED, FOR ADDING TO THE ARRAY AND PRINTING
// “Enter number 1, Enter number 2, Enter number 3 etc”
index=index+6; *
printf("\n");
// MEANS THAT WE LEAVE THE MAIN DO LOOP AND PRINT THE ENTERED NUMBERS (UNDER THE LOOP)
}
// WRONG ENTRY BY THE USER
else
{
// PUT INDEX TO 0 AND START AGAIN AT THE BEGINNING OF THE LOOP
index=0;
printf("\nYour entry must be 'Y' or 'N'. ");
printf("\nPlease re-enter your numbers.\n\n");
}
}
while(index<=5);
/*OUTPUT THE SIX FINAL NUMBERS*/
printf("\nThe six lotto numbers you have chosen are :");
printf("\n%5d %5d %5d %5d %5d %5d", numb[0],numb[1],numb[2],numb[3],numb[4],numb[5]); }
Edited by WingedPanther, 30 April 2009 - 07:10 AM.
add code tags (the # button)


Sign In
Create Account

Back to top









