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]);

    }

}

Like

  • Where does it occur
  • What was the program's output
  • How about reading the annoucements, especially this one

>>I keep getting segmentation faults when running this program. Could anyone help me with this?

Yes -- get a clean compile before attempting to run it. your compiler should not create an executable file if it encounters syntax errors, but some older compilers will.

Some of your problems:

line 62 and line 86:
Two different functions with same name.

line 67 "counter" and "number" are two variables that the function
doesn't see from inside main fuction and the loop do-while.

line 61 and 85 two end main?.
line 71: At this point your curly brackets are off.

line 75 where's the start do in this do-while loop?

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.