-1

i was trying to create a program that manages car fixing records,and everything was working fine until case 6 of this code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
    int day;
    int month;
    int year;
}date;
typedef struct{
    char pie_name[50];
    int pie_num;
    float pie_price;
}pie_st;
typedef struct{
    long car_num;
    char car_type[20];
    char car_colour[20];
    char owner_name[50];
    long phone_num;
}own_cars_st;
typedef struct{
    int management_num;
    long car_num;
    date date_of_management;
    int pieces_num;
    pie_st pie_rec[1000];
    float management_fees;
    float total_price;
}management_st;
void check_file(FILE *ptf);
void add_car_rec();
void add_management_rec();
int delete_car_rec(long search);
int search_own(char owner[]);
int car_man(long search);
int main(){
    int option,c=0,found=1;
    long search;
    char owner[50];
    while(c==0)
    {
        printf("\nplease enter your choice:\n");
        printf("1.add new car record\n");
        printf("2.add new car management record\n");
        printf("3.delete a car record by number\n");
        printf("4.view all car numbers for a specific owner\n");
        printf("5.display all management records\n");
        printf("6.view a specific car's management records and the total cost\n");
        printf("7.view the most used gear piece's number\n");
        printf("8.view a list of all cars that had management after a specific date\n");
        printf("9.view all cars with changed tires\n");
        printf("10.exit\n");
        scanf("%d",&option);
        switch(option)
        {
            case 1:
                add_car_rec();
                break;
            case 2:
                add_management_rec();
                break;
            case 3:
                printf("please enter the number of the car you want to delete its number:\t");
                scanf("%ld",&search);
                found=delete_car_rec(search);
                if(found==1)
                    printf("there's no record available for this number!\n");
                else
                    printf("record has been deleted!\n");
                break;
            case 4:
                printf("please enter the name of the owner:\t");
                fflush(stdin);
                scanf("%[^\n]s",owner);
                found=search_own(owner);
                if(found==1)
                    printf("owner was not found!\n");
                break;
            case 5:
                break;
            case 6:
                printf("please enter the number of the car you want to view its management records:\t");
                scanf("%ld",&search);
                printf("7");
                car_man(search);
                printf("8");
                if(found==1)
                    printf("this car number has no management records!\n");
                break;
            case 7:
                break;
            case 8:
                break;
            case 9:
                break;
            case 10:
                c=10;
                break;
            default:
                printf("please choose one of the available options\n");
        }
    }
    return 0;
}
void add_car_rec(){
    own_cars_st new_car,old_cars[100];
    printf("please enter the car information:\n");
    printf("car number:\t");
    scanf("%ld",&new_car.car_num);
    printf("car type:\t");
    fflush(stdin);
    scanf("%[^\n]s",new_car.car_type);
    printf("car colour:\t");
    fflush(stdin);
    scanf("%[^\n]s",new_car.car_colour);
    printf("owner name:\t");
    fflush(stdin);
    scanf("%[^\n]s",new_car.owner_name);
    printf("phone number:\t");
    scanf("%ld",&new_car.phone_num);
    FILE *ptf;
    int n=0,i,j,fc;
    ptf=fopen("owners and cars.bin","ab+");
    if(ptf!=NULL){
        fseek(ptf,0,2);
        fc=ftell(ptf);
        rewind(ptf);
        if(fc==0)
        {
            fwrite(&new_car,sizeof(own_cars_st),1,ptf);
            fclose(ptf);
        }
        else{
            while(1==fread(&old_cars[n],sizeof(own_cars_st),1,ptf))
            {
                n++;
            }
            for(i=0;i<n+1;i++)
            {
                if(new_car.car_num<old_cars[i].car_num)
                {
                    for(j=n+1;j>i;j--)
                    {
                        old_cars[j]=old_cars[j-1];
                    }
                    break;
                }
            }
            old_cars[i]=new_car;
            fclose(ptf);
            ptf=fopen("owners and cars.bin","wb");
            check(ptf);
            for(i=0;i<n+1;i++)
            {
                fwrite(&old_cars[i],sizeof(own_cars_st),1,ptf);
            }
            fclose(ptf);
        }
    }
}
void add_management_rec(){
    FILE *ptf;
    int i,count=0,empty;
    float sum=0;
    management_st new_manage_rec,last_manage_rec;
    printf("please enter the new management record information:\n");
    printf("fixed car's number:\t");
    scanf("%ld",&new_manage_rec.car_num);
    printf("date of management:\n");
    printf("day:\t");
    scanf("%d",&new_manage_rec.date_of_management.day);
    printf("month:\t");
    scanf("%d",&new_manage_rec.date_of_management.month);
    printf("year:\t");
    scanf("%d",&new_manage_rec.date_of_management.year);
    printf("the number of the used pieces types:\t");
    scanf("%d",&new_manage_rec.pieces_num);
    printf("please enter each type's information:\n");
    for(i=0;i<new_manage_rec.pieces_num;i++)
    {
        printf("piece #%d\n",i+1);
        printf("piece name:\t");
        scanf("%s",new_manage_rec.pie_rec[i].pie_name);
        printf("number of used pieces:\t");
        scanf("%d",&new_manage_rec.pie_rec[i].pie_num);
        printf("piece's price:\t");
        scanf("%f",&new_manage_rec.pie_rec[i].pie_price);
        sum+=new_manage_rec.pie_rec[i].pie_price*(float)new_manage_rec.pie_rec[i].pie_num;
    }
    printf("management fees:\t");
    scanf("%f",&new_manage_rec.management_fees);
    new_manage_rec.total_price=new_manage_rec.management_fees+sum;
    ptf=fopen("management.bin","ab+");
    if(ptf!=NULL)
    {
        fseek(ptf,0,2);
        empty=ftell(ptf);
        rewind(ptf);
        if(empty==0){
            new_manage_rec.management_num=1;
            fwrite(&new_manage_rec,sizeof(management_st),1,ptf);}
        else
        {
            fseek(ptf,-sizeof(management_st),2);
            fread(&last_manage_rec,sizeof(management_st),1,ptf);
            new_manage_rec.management_num=last_manage_rec.management_num+1;
            fwrite(&new_manage_rec,sizeof(management_st),1,ptf);
        }
    }
    fclose(ptf);
}
void check(FILE *ptf){
    if(ptf==NULL)
    {
        printf("error!\n");
        exit -1;
    }
}
int delete_car_rec(long search){
    FILE *ptf;
    int count=0,i,j,f=1;
    own_cars_st avai_cars[100];
    ptf=fopen("owners and cars.bin","rb");
    check(ptf);
    while(fread(&avai_cars[count],sizeof(own_cars_st),1,ptf)==1)
    {
        count++;
    }
    fclose(ptf);
    if(count==1)
    {
        if(search==avai_cars[0].car_num)
        {
            f=0;
            ptf=fopen("owners and cars.bin","wb");
            fclose(ptf);
        }
    }
    else
    {
        for(i=0;i<count;i++)
        {
            if(search==avai_cars[i].car_num)
            {
                f=0;
                for(j=i+1;j<count;j++)
                {
                    avai_cars[i]=avai_cars[j];
                }
            }
        }
        ptf=fopen("owners and cars.bin","wb");
        check(ptf);
        for(i=0;i<count;i++)
            fwrite(&avai_cars[i],sizeof(own_cars_st),1,ptf);
        fclose(ptf);
    }
    return f;
}
int search_own(char owner[]){
    FILE *ptf;
    int found=1,count=0,i;
    own_cars_st avai_cars[100];
    ptf=fopen("owners and cars.bin","rb");
    check(ptf);
    while(1==fread(&avai_cars[count],sizeof(own_cars_st),1,ptf))
        count++;
    fclose(ptf);
    for(i=0;i<count;i++)
    {
        if(strcmp(avai_cars[i].owner_name,owner)==0)
        {
            found=0;
            printf("car#%d:\t%ld\n",i+1,avai_cars[i].car_num);
        }
    }
    return found;
}
int car_man(long search){
    printf("*\n");
    FILE *ptf;
    management_st avai_man[100];
    int i,count=0,found=1,j;
    float total_cost=0;
    printf("0\n");
    ptf=fopen("management.bin","rb");
    check(ptf);
    printf("1\n");
    while(1==fread(&avai_man[count],sizeof(management_st),1,ptf))
        count++;
    fclose(ptf);
    printf("2\n");
    for(i=0;i<count;i++)
    {
        if(search==avai_man[i].car_num)
        {
            found=0;
            printf("date of management:\n");
            printf("day:\t");
            printf("%d",avai_man[i].date_of_management.day);
            printf("month:\t");
            printf("%d",avai_man[i].date_of_management.month);
            printf("year:\t");
            printf("%d",avai_man[i].date_of_management.year);
            printf("the number of the used pieces types:\t");
            printf("%d",avai_man[i].pieces_num);
            printf("please enter each type's information:\n");
            for(j=0;j<avai_man[i].pieces_num;j++)
            {
                printf("piece #%d\n",i+1);
                printf("piece name:\t");
                printf("%s",avai_man[i].pie_rec[j].pie_name);
                printf("number of used pieces:\t");
                printf("%d",avai_man[i].pie_rec[j].pie_num);
                printf("piece's price:\t");
                printf("%f",avai_man[i].pie_rec[j].pie_price);
            }
            printf("management fees:\t");
            printf("%f",avai_man[i].management_fees);
        }
        total_cost+=avai_man[i].management_fees;
    }
    printf("3\n");
    printf("total cost for this car:\t%f",total_cost);
    return found;
}

the program seems to crash when calling the function car_man,ive writted few printf to trace when the program crashes and it only reaches printf("7\n"). any help would be appreciated ^.^ p.s. there's no warnings or any thing in the compiler

Ajay Brahmakshatriya
  • 8,213
  • 3
  • 19
  • 41
Ammar Brk
  • 1
  • 2
  • 2
    If it's crashing, you're getting an error. It would help to see that error. – Carcigenicate May 16 '18 at 11:55
  • @Carcigenicate sure , it says: a problem caused the program to stop working correctly.wnidows will close the program and notify you if a solutions available , and the program returns this : -1073741571 – Ammar Brk May 16 '18 at 11:59
  • 2
    What is `management_st`? Please post a *complete* example. – Andrew Henle May 16 '18 at 12:00
  • ive edited the question ^^" @AndrewHenle – Ammar Brk May 16 '18 at 12:13
  • 1
    Please [edit] your questtion and show us a [MCVE], that is a piece of code we ca copy/paste, compile and run. – Jabberwocky May 16 '18 at 12:18
  • Please indent the code. It is painful to look at it right now. – Ajay Brahmakshatriya May 16 '18 at 12:35
  • sorry ive edited it again and put the full code ^^" im fairly new to this and thought i would be saving you time by not inserting the whole code XD @MichaelWalz – Ammar Brk May 16 '18 at 12:35
  • Your code does nwot compile. Copy/paste your code verbatim. – Jabberwocky May 16 '18 at 12:36
  • `exit -1;` what is this? – Ajay Brahmakshatriya May 16 '18 at 12:47
  • @AjayBrahmakshatriya as far as i was tought in my class it was supposed to do "return" job , but ive noticed it dosent do anything so i wont be surprised to know that ^^" – Ammar Brk May 16 '18 at 12:49
  • `exit` is a function. You need to call it if you need to stop the program. So something like `exit(-1)` would make sense. – Ajay Brahmakshatriya May 16 '18 at 12:51
  • `scanf("%[^\n]s",owner);` -- the format specifier doesn't match the value provided. Infact the parameter is not even an address. – Ajay Brahmakshatriya May 16 '18 at 12:52
  • [`flush(stdin);` is undefined behavior](https://stackoverflow.com/questions/2979209/using-fflushstdin). – Ajay Brahmakshatriya May 16 '18 at 12:53
  • ooh ! thank you very much for pointing at it ! it was written exit -1 in my college lectures guess it was copying mistakes , but do you have any idea why it crashes ?? @AjayBrahmakshatriya – Ammar Brk May 16 '18 at 12:53
  • @AmmarBrk that is not the reason for the crash. There are many more mistakes with your program. Fixing this won't fix the crash. – Ajay Brahmakshatriya May 16 '18 at 12:54
  • @AjayBrahmakshatriya i see ! sorry to bother you more but i have a question , im using a microsoft visual studio , does that affect the fflush undefined behavior ? ive read that its only considered this in c99 standards only is that correct ? – Ammar Brk May 16 '18 at 12:57
  • @AmmarBrk yes, this is Undefined behavior according to the C standard (which Microsoft Visual Studio also follows in most cases). However, it adds the functionality of allowing `fflush` on `stdin`. Still, you shouldn't use that because your code wouldn't be portable. I tried your code on my Windows machine with `clang` (instead of MSVC) and the input all went haywire. It's best practice to write code that is portable. – Ajay Brahmakshatriya May 16 '18 at 13:00
  • @AjayBrahmakshatriya aaah i see , thank you very mush !! ^.^ – Ammar Brk May 16 '18 at 13:02

1 Answers1

1
management_st avai_man[1000];

That is a really big array to be creating on the stack; you may be running out of stack-space.

Consider moving the array to dynamically allocated memory, or making it a static.

abelenky
  • 58,532
  • 22
  • 99
  • 149
  • At most, it's a handful of MB. Probably not a problem on today's machines with GB of memory available. – Andrew Henle May 16 '18 at 12:14
  • 1
    @AndrewHenle stack space can be pretty low even on multi GB computers. Typically on WIndows it's just 1Mb by default, 8 MB on Linux I believe. – Jabberwocky May 16 '18 at 12:17
  • i tried now to make it avai_man[100] but still crashed, and also i tried the avai_man[1000] with other functions and it worked perfectly, i also tried making the arrays in the other functions [100] to make sure but it crashed also – Ammar Brk May 16 '18 at 12:17
  • @AndrewHenle: Exactly what Walz said: Stack space is ***not*** the same thing as available memory! Per-thread stack space can be shockingly limited. – abelenky May 16 '18 at 14:11