I am making a program for a hotel management system, but I found a problem when calculating the bill for the customers. This is my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<conio.h>
//Declaration
//Struct for the customers
struct Identity
{
char name_first[50];
char name_last[50];
int phone;
int IC[10];
char email[50];
int age;
};
struct Date
{
int day;
int month;
int year;
};
struct Customer
{
struct Identity iden;
int room_number;
int *room_type;
int bill;
struct Date check_in;
struct Date check_out;
int day_num;
int cleaning;
};
//Function to count the check out and check in date for customer
//and also for billing
const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int countLeapYear(struct Date d)
{
int years = d.year;
if(d.month <= 2)
{
years--;
}
return years / 4 - years / 100 years / 400;
}
int getDays(struct Date d1, struct Date d2)
{
long int n1 = d1.year * 365 d1.day;
for(int i = 0; i < d1.month - 1; i )
{
n1 = monthDays[i];
}
n1 = countLeapYear(d1);
long int n2 = d2.year * 365 d2.day;
for(int i = 0; i < d2.month - 1; i )
{
n2 = monthDays[i];
}
n2 = countLeapYear(d2);
return (n2 - n1);
}
//Function to count the bill of the customers room
int getBill(int days, int type, int num_of_room)
{
int t = type;
if(t = 1)
{
printf("\n200\n");
return 200 * days * num_of_room;
}
else if(t = 2)
{
printf("500\n");
return 500 * days * num_of_room;
}
else if(t = 3)
{
printf("1200\n");
return 1200 * days * num_of_room;
}
else
{
return 0;
}
}
int main()
{
int n = 0;
printf("Input number of customers:\n");
scanf("%d", &n);
struct Customer cust[n];
int num_of_room = 0;
printf("\n***Please choose your room type***\n");
printf("How many room do you want?\n");
scanf("%d", &num_of_room);
printf("Which type of room do you want?\n");
printf("1.Standard Room ($200/night)\n");
printf("2.Deluxe Room ($500/night)\n");
printf("3.Ocean view room ($1200/night\n");
printf("Input the type of room number (1,2,3):\n");
scanf("%d", &cust->room_type);
int type = cust->room_type;
printf("***Please Confirm The Length of Your Stay***\n");
printf("Input check-in date (DD,MM,YYYY):\n");
scanf("%d,%d,%d", &cust->check_in.day, &cust->check_in.month, &cust->check_in.year);
printf("\nInput check-out date (DD,MM,YYYY):\n");
scanf("%d,%d,%d", &cust->check_out.day, &cust->check_out.month, &cust->check_out.year);
struct Date D1;
struct Date D2;
D1.day = cust->check_in.day;
D1.month = cust->check_in.month;
D1.year = cust->check_in.year;
D2.day = cust->check_out.day;
D2.month = cust->check_out.month;
D2.year = cust->check_out.year;
int days = getDays(D1, D2);
printf("The total days you are staying is %d days, type of room %d, number of room %d", days, type, num_of_room);
cust->bill = getBill(days, type, num_of_room);
printf("\nYou need to pay for $%d", cust->bill);
}
I tried debugging it and found out that the problem is with the type of room. No matter what type of room I choose, it will always be calculated as the first room, which is 200 dollars. This is the picture of the output:
The getBill()
function calculates the days and the number of rooms correctly, but it can't calculate the type of room I chose.
CodePudding user response:
Change
if(t = 1) //assignment
// assigns a value 1 to `t` and thereby if (1) is true.
// then, it always executes the block
{
printf("\n200\n");
return 200 * days * num_of_room;
}
to
if(t == 1) // comparison
and likewise, or better, use a switch
statement.