为啥我的账单计算功能不起作用?
Posted
技术标签:
【中文标题】为啥我的账单计算功能不起作用?【英文标题】:Why is my bill calculation function won't work?为什么我的账单计算功能不起作用? 【发布时间】:2022-01-03 20:28:38 【问题描述】:我正在为酒店管理系统编写程序,但在为客户计算账单时发现了问题。 这是我的代码:
#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);
我尝试调试它,发现问题出在房间类型上。无论我选择什么类型的房间,它总是以第一个房间计算,即200美元。 这是输出的图片:
getBill()
函数可以正确计算天数和房间数量,但无法计算我选择的房间类型。
【问题讨论】:
=
是赋值; ==
是比较。正确配置的编译器应该会警告您该常见错误。
【参考方案1】:
改变
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;
到
if(t == 1) // comparison
同样,或者更好的是,使用switch
语句。
【讨论】:
以上是关于为啥我的账单计算功能不起作用?的主要内容,如果未能解决你的问题,请参考以下文章