我正在尝试找出如何为我的学校项目创建餐厅菜单的方法,我被困在customer1
的循环部分(仍然有2位客户要去),无法弄清楚是什么问题。
我的程序将在第一次组合后跳过组合,直接进入数量。而且我的税也行不通。
有人知道怎么做吗?这是我正在进行的程序。
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define COMBOA 8.50
#define COMBOB 10.50
#define COMBOC 18.00
#define COMBOD 32.50
#define SSTAX 0.01
void main()
{
void logo();
void menu();
char cus1();
char comboA[] = "8.50";
char comboB[] = "10.50";
char comboC[] = "18.00";
char comboD[] = "32.50";
char member, combo, repeat;
char error[] = "INVALID_INPUT";
logo();
menu();
cus1();
system("pause");
}
void logo()
{ //My logo for my restaurant }
void menu()
{
printf("---------------------------------< MENU >---------------------------------------\n");
printf("Combo A\n> 1x Burger + 1x Drink\nPrice : RM8.50\n\n");
printf("Combo B\n> 2x Burger + 2x Drinks + 1x Salad + 1x Nugget\nPrice : RM10.50\n\n");
printf("Combo C\n> 3x Burger + 3x Drinks + 2x Salad + 1x Nugget\nPrice : RM18.00\n\n");
printf("Combo D\n> 4x Burger + 4x Drinks + 2x Salad + 2x Nugget + 2x MashPotato\nPrice : RM32.50\n\n");
printf("--------------------------------------------------------------------------------\n");
}
char cus1()
{
char combo, repeat,member;
double purchase, discount, discountRate, total, finaltot, tax, amountReceived, change, profit, combo1;
int quan, counter;
printf("Customer No:1\n");
do
{
printf("Please select Combo A/B/C/D (Enter 'X' to exit) : ");
combo = getchar();
printf("\nQuantity : ");
scanf("%d", &quan);
switch (combo)
{
case 'A':
case 'a':
combo1 = 8.50;
break;
case 'B':
case 'b':
combo1 = 10.50;
break;
case 'C':
case 'c':
combo1 = 18.00;
break;
case 'D':
case 'd':
combo1 = 32.50;
break;
default:
printf("Invalid input");
}
total = (double)quan * combo1;
printf("\t\tCombo %c : %d @ RM%.2f = RM %.2f\n\n", combo, quan, combo1, total);
printf("---------------------------------------------------------------\n");
} while (combo != 'X' && combo != 'x');
printf("\nIs customer a member? (Y/N) : ");
scanf("%c", &member);
rewind(stdin);
printf("\nEnter amount purchase(RM) :");
scanf("%f", &purchase);
rewind(stdin);
printf("\nEnter amount received from customer(RM) : ");
scanf("%f", &amountReceived);
rewind(stdin);
if (member == 'Y' || member == 'y')
{
//Customer is a member
if (purchase >= 100.00)
discountRate = 0.30; //30% discount
else
discountRate = 0.10; //10% discount
}
else
{
//Customer is not a member
if (purchase >= 100.00)
discountRate = 0.10; //10% discount
else
discountRate = 0.00; //No discount
}
tax = purchase * SSTAX;
discount = discountRate * purchase;
finaltot = purchase - discount - SSTAX;
change = amountReceived - finaltot;
printf("COMBO CHARGES = RM%.2f\n", total);
printf("ADD 10% SST = RM%.2f\n", tax);
printf("FINAL TOTAL = RM%.2f\n", finaltot);
printf("AMOUNT RECEIVED = RM%.2f\n", amountReceived);
printf("CHANGE DUE = RM%.2f\n", change);
return 0;
}
答案 0 :(得分:0)
在scanf()
调用中的格式代码前添加一个空格。
scanf("%c", &member);
//Becomes
scanf(" %c", &member);
问题来自以下事实:换行符(\n
)留在流中,因此下一个scanf()
会读取它并在没有任何用户输入的情况下立即通过。在格式代码之前添加空格可防止这种情况发生。
此外,使用rewind(stdin)
并没有用,因为您无法在控制台流中进行搜索。
答案 1 :(得分:0)
我对您的程序进行了一些更改,看它是否有效:-
#include<stdio.h>
#include<stdlib.h>
#pragma warning(disable:4996)
#define COMBOA 8.50
#define COMBOB 10.50
#define COMBOC 18.00
#define COMBOD 32.50
#define SSTAX 0.01
int main()
{
void logo();
void menu();
char cus1();
char comboA[] = "8.50";
char comboB[] = "10.50";
char comboC[] = "18.00";
char comboD[] = "32.50";
char member, combo, repeat;
char error[] = "INVALID_INPUT";
logo();
menu();
cus1();
system("pause");
return 0;
}
void logo()
{ //My logo for my restaurant }
}
void menu()
{
printf("---------------------------------< MENU >---------------------------------------\n");
printf("Combo A\n> 1x Burger + 1x Drink\nPrice : RM8.50\n\n");
printf("Combo B\n> 2x Burger + 2x Drinks + 1x Salad + 1x Nugget\nPrice : RM10.50\n\n");
printf("Combo C\n> 3x Burger + 3x Drinks + 2x Salad + 1x Nugget\nPrice : RM18.00\n\n");
printf("Combo D\n> 4x Burger + 4x Drinks + 2x Salad + 2x Nugget + 2x MashPotato\nPrice : RM32.50\n\n");
printf("--------------------------------------------------------------------------------\n");
}
char cus1()
{
char combo, repeat,member;
double purchase, discount, discountRate, total, finaltot, tax, amountReceived, change, profit, combo1;
int quan, counter;
printf("Customer No:1\n");
do
{
printf("Please select Combo A/B/C/D (Enter 'X' to exit) : ");
fflush(stdin);
combo = getchar();
if(combo=='X')
break;
printf("\nQuantity : ");
scanf("%d", &quan);
switch (combo)
{
case 'A':
case 'a':
combo1 = 8.50;
break;
case 'B':
case 'b':
combo1 = 10.50;
break;
case 'C':
case 'c':
combo1 = 18.00;
break;
case 'D':
case 'd':
combo1 = 32.50;
break;
default:
printf("Invalid input");
}
total = (double)quan * combo1;
printf("\t\tCombo %c : %d @ RM%.2f = RM %.2f\n\n", combo, quan, combo1, total);
printf("---------------------------------------------------------------\n");
} while (combo != 'X' && combo != 'x');
printf("\nIs customer a member? (Y/N) : ");
scanf("%c", &member);
rewind(stdin);
printf("\nEnter amount purchase(RM) :");
scanf("%f", &purchase);
rewind(stdin);
printf("\nEnter amount received from customer(RM) : ");
scanf("%f", &amountReceived);
rewind(stdin);
if (member == 'Y' || member == 'y')
{
//Customer is a member
if (purchase >= 100.00)
discountRate = 0.30; //30% discount
else
discountRate = 0.10; //10% discount
}
else
{
//Customer is not a member
if (purchase >= 100.00)
discountRate = 0.10; //10% discount
else
discountRate = 0.00; //No discount
}
tax = purchase * SSTAX;
discount = discountRate * purchase;
finaltot = purchase - discount - SSTAX;
change = amountReceived - finaltot;
printf("COMBO CHARGES = RM%.2f\n", total);
printf("ADD 10 SST = RM%.2f\n", tax);
printf("FINAL TOTAL = RM%.2f\n", finaltot);
printf("AMOUNT RECEIVED = RM%.2f\n", amountReceived);
printf("CHANGE DUE = RM%.2f\n", change);
return 0;
}
请参阅语句fflush(stdin);
,每当您尝试将字符作为输入时,它将从标准输入缓冲区中获取输入。这就是为什么语句combo = getchar();
从缓冲区中自动获取字符,从而跳过了您输入数量的原因。为了解决这个问题,我们fflush(stdin)
从当前缓冲区中删除了数据,使您可以输入字符。
还要检查您的行printf("ADD 10% SST = RM%.2f\n", tax);
,该行包含两个%符号。 10后的%符号不正确。您无法像这样打印%符号,因为只要它在printf中找到%符号,就会将其视为占位符(如%d或%c等)。因此,您的税额未打印。