0

When I run the code, my ide, VSCODE, stated zsh: bus error ./a.out. From my reading, bus error is caused by pointers. Correct me if I'm wrong

This is a college assignment, I need to write a program in C containing:

-Control Structure (Selection & repetition)

-Functions ( return & non-return value functions)

-Arrays

-Pointers

-structures

-File

If you have any suggestions for my code, do let me know. This is my first time learning C.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SAFARIADULT 70
#define SAFARIKIDS 50
#define WATERPARKADULT 100
#define WATERPARKKIDS 80
#define LEGOLANDADULT 150
#define LEGOLANDKIDS 130

void display1();
char option();
void safari1(int amount[2], char location[10] );
void waterpark1(int amount[2], char location[10] );
void legoland1(int amount[2], char location[10] );

struct data
{
    int promo;
    char name[20];
    int date_in;
    int date_out;
    int totalday;
    float total;
    float afterpromo;
};

void get_data(struct data user);
void promo(struct data user);
void display2(struct data user, char location[10], int amount[2]);

int main()
{
    char choice;
    int amount[2];
    char location[10];

    display1();
    choice = option ();
    switch (choice)
    {
    case '1':
        safari1(amount, location);
        break;
    case '2':
        waterpark1(amount, location);
        break;
    
    case '3':
        legoland1( amount, location);
        break;

    default:
        printf("Invalid choice! Program will terminate. Thank you");
        exit (0);
    }

}

void display1()
{
    printf("\n");
    printf("******[ Welcome To Theme Park MMU ]******");
    printf("\n              Ticket Website             ");
    printf("\n-----------------------------------------");
    printf("\n Here is the option to choose: ");
    printf("\n        Place       \tAdult \tKid");
    printf("\n \t1- Safari \tRm%d \tRm%d", SAFARIADULT, SAFARIKIDS);
    printf("\n \t2- Waterpark \tRm%d \tRm%d", WATERPARKADULT, WATERPARKKIDS);
    printf("\n \t3- Legoland \tRm%d \tRm%d", LEGOLANDADULT, LEGOLANDKIDS);
    printf("\n-----------------------------------------");
    printf("\n");
}

char option()
{
    char option;
    printf("Your Choice (1-3) : ");
    scanf("%c", &option);
    return option;
}

void safari1(int amount[2], char location[10])
{
    strcpy("Safari", location);
    int child, adults;
    amount[0] = child;
    amount[1] = adults;
    printf("Amount of child : ");
    scanf("%d", &child);
    printf("Amount of adult : ");
    scanf("%d", &adults);
    struct data user;
    get_data(user);
    user.total = child * SAFARIKIDS + adults * SAFARIADULT;
    promo(user);
    display2(user, location, amount);
}

void legoland1(int amount[2], char location[10])
{
    strcpy("Legoland", location);
    int child, adults;
    amount[0] = child;
    amount[1] = adults;
    printf("Amount of child : ");
    scanf("%d", &child);
    printf("Amount of adult : ");
    scanf("%d", &adults);
    struct data user;
    get_data(user);
    user.total = child * LEGOLANDKIDS + adults * LEGOLANDADULT;
    promo(user);
    display2(user, location, amount);
}

void waterpark1(int amount[2], char location[10])
{
    strcpy("Waterpark", location);
    int child, adults;
    amount[0] = child;
    amount[1] = adults;
    printf("Amount of child : ");
    scanf("%d", &child);
    printf("Amount of adult : ");
    scanf("%d", &adults);
    struct data user;
    get_data(user);
    user.total = child * WATERPARKKIDS + adults * WATERPARKADULT;
    promo(user);
    display2(user, location, amount);
}

void get_data(struct data user)
{
    fflush(stdin);
    printf("Name : ");
    gets(user.name);
    printf("Date In : ");
    scanf("%d", &user.date_in);
    printf("Date Out : ");
    scanf("%d", &user.date_out);
    user.totalday = user.date_out - user.date_in;
}

void promo(struct data user)
{

    char code[5];
    fflush(stdin);
    printf("Promo Code : ");
    gets(code);
    if (strcmp(code,"promo")==0)
    {
        user.afterpromo = user.total * 0.9;
        printf("Your code is valid!!");
        user.promo = 1;
    }
    else
    {
        printf("Invalid Code !\n");
        user.promo = 0;
    }   
}

void display2(struct data user, char location[10], int amount[2])
{
    printf("-----------------------------------");
    printf("Name \t: %s", user.name);
    printf("Location \t: %s", location);
    printf("Date in \t: %d", user.date_in);
    printf("Date out \t: %d", user.date_out);
    printf("Total day \t: %d days", user.totalday);
    printf("Number of kids : %d", amount[0]);
    printf("Number of adults : %d", amount[1]);
    printf("Total Price \t: RM%.2f", user.total);
    if (user.promo == 1)
    {
       printf("Promo Code \t: Yes");
    }
    else
    {
        printf("Promo Code \t: No");
    }
    printf("Final Price \t: RM%.2f", user.afterpromo);
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • My compiler gives many warnings for this code, mostly about uninitialised variables. Please enable full compiler warnings. – Weather Vane Dec 21 '21 at 08:59
  • "Bus error" is a *crash* where you use memory and pointers the wrong way. First of all build with extra warnings enabled (I recommend `-Wall -Wextra`) and treat all warnings as errors that must be fixed properly (and not just silenced). Then also add debug information to your application (build with the `-g` flag). Lastly use a *debugger* to catch the crash and locate when and where in your code it happens. Also use the debugger to examine the variables and their values and the time and location of the crash. – Some programmer dude Dec 21 '21 at 09:00
  • Off-topic: If you just need a single character you might prefer [`getchar`](https://en.cppreference.com/w/c/io/getchar) over `scanf`, that's less complicated (`char option = getchar();`) and as a bonus even more efficient. – Aconcagua Dec 21 '21 at 09:02
  • 1
    ...for example, you have `amount[0] = child;` *before* the input `scanf("%d", &child);` is obtained. – Weather Vane Dec 21 '21 at 09:02
  • `fflush(stdin);` is [undefined behaviour](https://stackoverflow.com/a/2979217/1312382) – don't do it! – Aconcagua Dec 21 '21 at 09:03
  • Another problem is that function `get_data(struct data user)` inputs to the passed structure, which is discarded on return from the function. – Weather Vane Dec 21 '21 at 09:04
  • Naming a local variable equal to a function is questionable – at very least. You might rather avoid. – Aconcagua Dec 21 '21 at 09:05
  • @Aconcagua mixing `scanf` with `getchar` will only create more problems for a beginner. Better is `scanf(" %c", &option);` with the added space. – Weather Vane Dec 21 '21 at 09:06
  • @Aconcagua But `getchar` returns an **`int`** to be able to handle `EOF` correctly (which the OP doesn't do, but should). – Some programmer dude Dec 21 '21 at 09:07
  • 1
    Error handling is lacking entriely anyway (see @Someprogrammerdude) – if using getchar or scanf, in either case you should check for the error conditions. For `scanf` you should compare the return value against the the number of arguments you scanned – if both do not match, some error occurred (most likely invalid user input). – Aconcagua Dec 21 '21 at 09:13
  • 1
    Your strcpy calls are wrong too, you've swapped the order of the parameter. Overall, you cannot program by trial & error while taking a chance at the syntax. You must be confident that you know what every single line of code you write actually does. – Lundin Dec 21 '21 at 09:36
  • 1
    Also the presence of `gets` means that you are using a completely outdated source of learning. Your current book/teacher/tutorial needs to be retired in favour for something that was updated at least once during the past 20 years. – Lundin Dec 21 '21 at 09:39

0 Answers0