-1
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<conio.h>


int choice,i,j,semester_fee=2400, number=0; //number is number of buses in system
char user;//which user
FILE *bptr;


struct details{
int bus_number; 
char route[100];
char busdriver[100];
long int drivercontact;
char seat[32][14];  
};


void login(struct details array[], int number);
void bus(struct details array[], int number);//installing new bus
void record(void);//displaying existing buses
void newpassword(void);
void reservation(void);
void cancel(void);
void exit(void);



int main(){
    
    struct details array[number];
    printf("Press 'a' for accessing admin options and 's' for accessing student/passenger options\n");
    scanf(" %c", &user);
    system("cls");
    
    switch(user){
        case 'a':
        case 'A':
        login(array, number);
        system("cls");
        do{ 
        printf("Please press the corresponding number to select one of the following options:\n");
        printf("1. Install new bus\n");
        printf("2. View Details\n");
        printf("3. Update password\n");
        printf("4. Exit\n");
        scanf("%d", &choice);
        system("cls");
        
        switch(choice){
            case 1: bus(array, number);
            break;
        }}while(choice!=4);
        
        
        break;
        
  }
    
    
    return 0;
}

void login(struct details array[], int number){
    system("cls");
    char name[20]={0}, pass[20]={0}, sign_name[20]={0}, sign_pass[20]={0}, ch;
    int result_name, result_pass, option, i=0;
    FILE*fptr;
    
printf("\t\tSelect a number:\n");
printf("\t\t 1. Register\n");
printf("\t\t 2. Sign in\n");
printf("Your choice: ");
scanf("%d", &option);

if(option==1){
    fptr=fopen("Signin_details.txt", "w+");
    printf("Username: ");
    fflush(stdin);
    gets(name);
    printf("Password: ");
    fflush(stdin);
    gets(pass);
    printf("You have successfully registered!");
    fprintf(fptr, "%s\n%s", name, pass);
    fclose(fptr);
    
    printf("\nPress any key to continue ... ");
    getch();
    
    bus(array, number);
    
    
}

if(option==2){
    fptr=fopen("Signin_details.txt", "r");
    rewind(fptr); //sets pointer back to start of file
    while((ch=fgetc(fptr))==NULL) printf("\nPlease register yourself first\n");
    printf("Username: ");
    fflush(stdin);//refresh
    gets(sign_name);
    printf("Password: ");
    fflush(stdin);
    gets(sign_pass);

    char line[20];
    fgets(line, sizeof(line), fptr);
            result_name=strcmp(sign_name, line);
            fgets(line, sizeof(line), fptr);
            result_pass=strcmp(sign_pass, line);
    
     if(result_name==0 && result_pass==0) printf("\nWelcome user %s!", name);
    while(result_pass!=0 || result_name!=0){
    printf("\nWrong login details. Please enter them again.\n\n");
    printf("Username: ");
    fflush(stdin);//refresh
    gets(sign_name);
    printf("Password: ");
    fflush(stdin);
    gets(sign_pass);

    char line[20];
    rewind(fptr);
    fgets(line, sizeof(line), fptr);
            result_name=strcmp(sign_name, line);
            fgets(line, sizeof(line), fptr);
            result_pass=strcmp(sign_pass, line);
}
    getch();}
fclose(fptr);
}


void bus(struct details array[], int number){

int no;
printf("Enter the number of buses you want to add: ");
scanf("%d", &no);
number+=no;
int busno;
FILE* fptr[no];
char buffer[6];
char string1[5],ch;
char string2[5];

for(int i=0; i<no; i++){
printf("Enter the bus number: ");
scanf("%d", &busno);
sprintf(string1, "%d", busno);//converts the integer busno to text and stores it in string1
bptr=fopen("buses.txt", "a+");


for(int j=0; j<i; j++){
    if(busno==array[j].bus_number){
    while(busno==array[j].bus_number){
        printf("Bus data already exists. Enter another bus number: ");
        scanf("%d", &busno);
    }break;}
}
    fseek(bptr, 0, SEEK_SET); //sets pointer to beginning of file buses
    while(fgets(string2, 5, bptr)){
        while(strcmp(string1, string2)==0){
            printf("Bus data already exists. Enter another bus number: ");
            scanf("%d", &busno);
            sprintf(string1, "%d", busno);          
        }}
        fprintf(bptr, "%d\n", busno);
        fclose(bptr);
    
    
        array[i].bus_number=busno;
        sprintf(buffer, "%d.txt", busno);
        fptr[i]=fopen(buffer, "w+");
        printf("Enter the bus driver's name: ");
        fflush(stdin);
        gets(array[i].busdriver);
        printf("Enter bus driver contact: ");
        scanf("%ld", &array[i].drivercontact);
        printf("Enter route of the bus: ");
        fflush(stdin);
        gets(array[i].route);

fprintf(fptr[i],"Bus number: %d\tSemester fee: %d\t\tBus Driver contact number: %ld\t\tBus Driver: %s\t\tRoute: %s\n", busno, semester_fee, array[i].drivercontact, array[i].busdriver, array[i].route);




for(int n=0; n<32;n++){
char s2[6]="Empty";
strcpy(array[i].seat[n], s2);}

int l=0;
for(int j=0; j<4; j++){
    for(int k=0; k<8; k++){
fprintf(fptr, "\t%d. %s\t", ++l, array[i].seat[l]);
}
fprintf(fptr, "\n");}

fclose(fptr[i]);}

printf("Press any key to continue ... ");
getch();
system("cls");


}

I am working on a project which would reserve bus seats. This is a small piece of code. Since I am just getting started, there might be a lot of details missing. But for this piece of code I am trying to take login information from the user and inputting each bus data. Everytime I enter a bus data, it gets saved in a new file and the bus number is also saved in another file "buses.txt". However when I execute it, my login does not work, and after the input of the first bus number, the program stops executing and comes to a standstill. In the login part, it registers and saves the sign-in details in another file, but when I want to sign-in, it won't let me and says "wrong sign-in details". What am I doing wrong in the two parts?

Coding
  • 11
  • 1

1 Answers1

0

You define a global int:

int number=0; //number is number of buses in system

You then define an array ... of size 0

struct details array[number];

By the way some compilers will provide a warning for this.

Then in login you access the array, which is probably where you crash.

Angus Comber
  • 9,316
  • 14
  • 59
  • 107