update: after working on the project i wrote this code
:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define LICENSE_PLATE_LENGTH 7
#define LICENSE_PLATE_ALPHA_LENGTH 3
#define LICENSE_PLATE_NUM_LENGTH 4
typedef struct {
int nDays;
float kmsDriven;
char carLicensePlate[LICENSE_PLATE_LENGTH+1];
char *clientName;
char chargingCategory;
} Rental;
void licence_plate_enter(Rental** rental, int counter);
int main()
{
int counter,choice,max_number;
Rental** rentals=NULL;
char del_number, del_name, show_number, show_name;
printf("Welcome to the Car Rental Charging System!\n\n");
printf("Welcome to the Car Rental Manager!\n");
printf("How many rentals do you plan to manage? ");
do
{
scanf("%d", &max_number);
if (max_number<0)
{
printf("\nInvalid number.");
printf("\nHow many rentals do you plan to manage?");
}
}
while (max_number<0);
rentals=(Rental **)malloc(max_number * sizeof(Rental));
if (rentals==NULL)
{
printf("\nRentals could not be stored");
return 1;
}
for (counter=0; counter<max_number; counter++)
{
rentals[counter]=(Rental *)malloc(sizeof(Rental));
if (rentals[counter]==NULL)
{
printf("\nRentals could not be stored");
return 1;
}
}
counter=0;
do
{
printf("\nProgram menu - Please select one of the following options:\n");
printf("1. Insert new rental\n");
printf("2. Delete rental\n");
printf("3. Show rentals\n");
printf("4. Terminate program\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Give license plate of rented car: ");
licence_plate_enter(rentals, counter);
printf("Give name of client that made the rental: ");
rentals[counter]->clientName=(char *)malloc(100*sizeof(char));
gets(rentals[counter]->clientName);//synarthsh
printf("Give days of rental: ");
scanf("%d", &rentals[counter]->nDays);
printf("Give kilometers driven: ");
scanf("%f", &rentals[counter]->kmsDriven);
printf("Give charging category for the rental: ");
scanf("%c", &rentals[counter]->chargingCategory);
counter++;
case 2:
printf("\nGive license plate for finding rentals to delete ('*' for any plate): ");
scanf("%c", &del_number);//synarthsh
printf("\nGive client name for finding rentals to delete ('*' for any client): ");
scanf("%c", &del_name);//synarthsh
case 3:
printf("Give license plate for finding rentals to show ('*' for any plate): ");
scanf("%c", &show_number);
printf("Give client name for finding rentals to show ('*' for any client): ");
scanf("%c", &show_name);//synarthsh kai gia to panw kai gia ayto
case 4:
break;
default:
printf("Invalid choice number. Try again.");
}
}
while (choice!=4);
printf("Program terminated. Bye!");
return 0;
}
void licence_plate_enter(Rental** rental, int counter)
{
int local_count;
for (local_count=0; local_count<=2; local_count++)
{
scanf("%c", rental[counter]->carLicensePlate[local_count]);
do
{
if (isalpha(rental[counter]->carLicensePlate[local_count])==0)
{
printf("\nError. You must first enter 3 letters\n");
printf("Enter the last letter again.\n");
}
}
while (isalpha(rental[counter]->carLicensePlate[local_count])==0);
}
for (local_count=3; local_count<=6; local_count++)
{
scanf("%c", rental[counter]->carLicensePlate[local_count]);
do
{
if (isdigit(rental[counter]->carLicensePlate[local_count])==0)
{
printf("\nError. Now youu must enter 3 letters\n");
printf("Enter the last letter again.\n");
}
}
while (isdigit(rental[counter]->carLicensePlate[local_count])==0);
}
local_count=0;
while (isalpha(rental[counter]->carLicensePlate[local_count]))
{
putchar((toupper(rental[counter]->carLicensePlate[local_count])));
local_count++;
}
}
dont pay attention to other cases rather than case 1 and the function...
build gives me 2 warnings and i can't understand why...tried to run the program but naturally it crashes :
edit: dont pay attention to the comments