Untitled

 avatar
unknown
plain_text
2 years ago
5.1 kB
1
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max_unsize 20
#define max_passsize 20
#define MAX_LOCATIONS 100
struct User
{
    char username[max_unsize];
    char password[max_passsize];

};

struct Location
 {
    char name[50];
    int available;
    float dist;
}; 

void registerUser() 
{
	system("cls");
	printf("\n\n\t\t\t\t\tREGISTRATION\n\n");
	struct User user;  
	printf("\t\t\t\tEnter your username (maximum %d characters): ",max_unsize);
	scanf("%s", user.username);   
	printf("\t\t\t\tEnter a password (maximum %d characters): ", max_passsize);
	scanf("%s", user.password);    
    FILE* fp = fopen("users.txt", "a");    
    fprintf(fp, "%s %s\n", user.username, user.password);   
    fclose(fp);
    printf("\t\t\t\tRegistration successful!\n");
}
void loginUser() 
{
	system("cls");
	printf("\n\n\t\t\t\t\tLOGIN\n\n");
    struct User user;
    char username[max_unsize];
    char password[max_passsize];   
    printf("\t\t\t\tEnter your username: ");
    scanf("%s", username);   
    printf("\t\t\t\tEnter your password: ");
    scanf("%s", password);  
    FILE* fp = fopen("users.txt", "r");    
    if(fp == NULL) 
	{
        printf("\t\t\t\tUser data fp does not exist.\n");
        return;
    }   
    while(fscanf(fp, "%s %s", user.username, user.password) == 2)
	{        
        if(strcmp(user.username, username) == 0 && strcmp(user.password, password) == 0) 
		{
            printf("\t\t\t\tLogin successful!\n");
            fclose(fp);
            return;
        }
    }
    printf("\t\t\t\tInvalid user.\n");  
    printf("\t\t\t\tPress [1].to Register or [2].to exit\n");
    int a;
    scanf("%d",&a);
    if(a==1)
    registerUser();
    else if(a==2)
    {
    	printf("\t\t\t\tGoodbye!");
    	exit(10);
    }
    else("\t\t\t\tinvalid choice");
    fclose(fp);
}
int main() 
{	
	
	struct User user;
    int choice;
	start:   
	printf("\n\n\n\t\t\t\tWelcome to XYZ cab booking\n\n");
    printf("\t\t\t\t1. Login\n");
    printf("\t\t\t\t2. Register\n");
    printf("\t\t\t\t3. Exit\n");
    printf("\t\t\t\tEnter your choice: ");
    scanf("\t\t\t\t%d", &choice);

    switch(choice) 
	{
		case 1:
            loginUser();
            break;
        case 2:
            registerUser();
            break;        
        case 3:
            printf("\t\t\t\tGoodbye!\n");
            exit(11);
            break;
        default:
            printf("\t\t\t\tInvalid input.\n");
            system("cls");
            goto start;
    }
    system("cls");
    printf("\n\n\t\t\t\t\tCAB BOOKING\n\n");
    
    FILE *fp, *bill_fp;
    struct Location locations[MAX_LOCATIONS];
    int num_locations = 0;
    int i;

    // Read locations from file
    fp = fopen("locations.txt", "r");
    if (fp == NULL) {
        printf("Error opening file\n");
        return 1;
    }
    while (fscanf(fp, "%s %d %f", locations[num_locations].name, &locations[num_locations].available, &locations[num_locations].dist) != EOF) {
        num_locations++;
    }
    fclose(fp);

    // Print available locations
    printf("Available locations:\n");
    for (i = 0; i < num_locations; i++) {
        printf("%d. %s\n", i+1, locations[i].name);
    }

    // Get pickup location from user
    int pickup_index = 0;
    while (pickup_index < 1 || pickup_index > num_locations) {
        printf("Enter pickup location number: ");
        scanf("%d", &pickup_index);
    }
    struct Location pickup = locations[pickup_index-1];
    pickup.available--;

    // Get dropoff location from user
    int dropoff_index = 0;
    while (dropoff_index < 1 || dropoff_index > num_locations) {
        printf("Enter dropoff location number: ");
        scanf("%d", &dropoff_index);
    }
    struct Location dropoff = locations[dropoff_index-1];
    dropoff.available--;

    // Calculate distance and bill amount
    float distance = dropoff.dist - pickup.dist;
    float bill_amount = distance * 10;

    // Update file with new location availability
    fp = fopen("locations.txt", "w");
    for (i = 0; i < num_locations; i++) {
        fprintf(fp, "%s %d %f\n", locations[i].name, locations[i].available, locations[i].dist);
    }
    fclose(fp);
    
    printf("Username: %s\n", user.username);
    printf("Pickup: %s\n", pickup.name);
    printf( "Dropoff: %s\n", dropoff.name);
    printf( "Distance: %.2f km\n", distance);
    printf( "Bill amount: $%.2f\n", bill_amount);

    // Store bill in file
    bill_fp = fopen("bill.txt", "a");
    if (bill_fp == NULL) {
        printf("Error opening file\n");
        return 1;
    }
    fprintf(bill_fp,"Username: %s\n", user.username);
    fprintf(bill_fp, "Pickup: %s\n", pickup.name);
    fprintf(bill_fp, "Dropoff: %s\n", dropoff.name);
    fprintf(bill_fp, "Distance: %.2f km\n", distance);
    fprintf(bill_fp, "Bill amount: $%.2f\n", bill_amount);
    fclose(bill_fp);

    printf("Bill generated and stored in file\n");
       
	return 0;
}

Editor is loading...