Hello,
I keep getting segmentation faults when running this program. Could anyone help me with this?
#include<stdio.h>
#define SIZE 5
void clearTicket(int ticket[SIZE]);
void printTicket(int ticket[SIZE]);
int main()
{
int ticket[SIZE];
int number;
int sentinel;
int i;
/* Simple instruction prompts for the user's benefit */
printf("\nWelcome to the CSCI 230 Lottery!\n\n");
printf("Instructions: \n\n");
printf("Each ticket consists of 5 numbers.\n");
printf("Please enter each number one at a time\n");
/* Time to start making tickets*/
do
{
clearTicket(ticket); /*ensures the ticket is empty.*/
printf("\nLet's make a ticket!\n");
int counter = 0;
do
{
printf("Please enter a number:\n");
scanf("%d",&number);
if(number > 0 && number < 51)
{
for(i = 0; i < SIZE; i++)
{
/*checks to ensure the number has not already been entered*/
while(ticket[i] == number || number < 1 || number > 50)
{
if(number < 1 || number > 50)
printf("Invalid entry. ");
else
printf("%d has already been entered.\n", number);
printf("Please enter a number between 1-50:\n");
scanf("%d",&number);
} /* end while*/
} /*end for*/
/*place the number in the ticket*/
ticket[counter] = number;
counter++;
} /* end if*/
else
{
printf("Invalid entry. Please enter number between 1-50:\n");
}
} while(counter < 5); /*end nested do while*/
printf("The folloing is your ticket:\n");
/* send our ticket to the print function*/
printTicket(ticket);
printf("\nWould you like to make another ticket?\n");
printf("Enter -1 to Quit. Any other integer to continue.\n");
scanf("%d",&sentinel);
} while(sentinel != -1); /* end our sentinel do-while*/
printf("Good luck!\n");
printf("Thanks for playing!\n");
return 0;
} /* end main*/
void clearTicket(int ticket[SIZE])
{
int i;
for(i = 0; i < SIZE; i++)
/*place the number in the ticket*/
ticket[counter] = number;
counter++;
} /* end if*/
else
{
printf("Invalid entry. Please enter number between 1-50:\n");
}
} while(counter < 5); /*end nested do while*/
printf("The folloing is your ticket:\n");
/* send our ticket to the print function*/
printTicket(ticket);
printf("\nWould you like to make another ticket?\n");
printf("Enter -1 to Quit. Any other integer to continue.\n");
scanf("%d",&sentinel);
} while(sentinel != -1); /* end our sentinel do-while*/
printf("Good luck!\n");
printf("Thanks for playing!\n");
return 0;
} /* end main*/
void clearTicket(int ticket[SIZE])
{
int i;
for(i = 0; i < SIZE; i++)
{
ticket[i]= 0;
} /* end for*/
}/* end clearTicket function*/
void printTicket( int ticket[SIZE])
{
int i;
for(i = 0; i < SIZE; i++ )
{
printf("%d ", ticket[i]);
}
}