好的,所以我正在尝试建立一个基本程序来计算披萨订单的价格。 我想要询问客户是否订购了。 如果他们输入y然后我希望循环继续,如果输入任何其他字符我希望它停止。 当我输入任何字符时,程序只是连续打印出我的所有printf语句。 我正在使用代码块。 这是我的代码。 我得到2个警告。
warning: initialization makes integer from pointer without a cast [enabled by default] at line 17 where i declare the keepgoing variable. warning: comparison between pointer and integer [enabled by default]|
在第19行,while循环开始。
#include #include main() { #define LARGEPIZZAPRICE #define SMALLPIZZAPRICE #define LARGEPIZZATOPPING #define SMALLPIZZATOPPING #define DRINK int numberOfLargePizzas; int numberOfSmallPizzas; int numberOfLargeToppings; int numberOfSmallToppings; int numberOfDrinks; int keepGoing = "y"; while (keepGoing == "y") { printf("How many large pizza's do you wantn"); scanf(" %d", &numberOfLargePizzas); printf("How many large toppings do you wantn"); scanf(" %d", &numberOfLargeToppings); printf("How many small pizza's do you wantn"); scanf(" %d", &numberOfSmallPizzas); printf("How many small toppings do you wantn"); scanf(" %d", &numberOfSmallToppings); printf("Would you like to order more. Enter ay or nn"); scanf(" %i", &keepGoing); } }`
*****更新*****好的感谢所有的帮助,它现在运行良好。 如果有人可以看一下并给出任何提示来收紧或做我正在做的事情,请告诉我。 这对我来说是学习经验,我通过反复试验来做到这一点。 该程序运行,但我有一种感觉,我正在构建错误。 这就是我所拥有的:
#include #include main() { #define LARGEPIZZAPRICE 12 #define SMALLPIZZAPRICE 10 #define LARGEPIZZATOPPING 2 #define SMALLPIZZATOPPING 1.50 #define DRINK 1.50 #define TAXRATE .05 int numberOfLargePizzas; int numberOfSmallPizzas; int numberOfLargeToppings; int numberOfSmallToppings; int numberOfDrinks; char keepGoing ='y'; float largePizzaTotal; float smallPizzaTotal; float drinkTotal; while (keepGoing == 'y') { printf("How many large pizza's do you wantn"); scanf(" %d", &numberOfLargePizzas); if(numberOfLargePizzas != 0){ printf("How many large toppings do you wantn"); scanf(" %d", &numberOfLargeToppings); } printf("How many small pizza's do you wantn"); scanf(" %d", &numberOfSmallPizzas); if(numberOfSmallPizzas !=0){ printf("How many small toppings do you wantn"); scanf(" %d", &numberOfSmallToppings); } printf("How many drinks would you liken"); scanf(" %int", &numberOfDrinks); printf("Would you like to order more. Enter ay or nn"); scanf(" %c", &keepGoing); } largePizzaTotal = (LARGEPIZZAPRICE*numberOfLargePizzas)+(LARGEPIZZATOPPING*numberOfLargeToppings); smallPizzaTotal=(SMALLPIZZAPRICE*numberOfSmallPizzas)+(SMALLPIZZATOPPING*numberOfSmallToppings); drinkTotal = DRINK*numberOfDrinks; printf("Subtotal: %2f", largePizzaTotal + smallPizzaTotal + drinkTotal);
}
您无法比较c中的字符串,可能就是您的意思
char keepGoing = 'y'; if (keepGoing == 'y')
但是你应该修复scanf()
scanf(" %c", &keepGoing);
该
int keepGoing = "y";
如果禁用编译器警告,编译好,但是错了。
您的编译器确实告诉您它是错误的,因为int
和指针是不兼容的类型。
您正在将integer
变量与character
数据进行比较。
替换int keepGoing = "y";
同
char keepGoing = 'y';
并替换
while (keepGoing == "y")
与while (keepGoing == 'y')
或者如果您想照顾病例,请更换
while ( keepGoing == 'y' || keepGoing == 'Y')
你问了一些提示,以收紧你正在做的事情。 我花了一点时间,概述了Pizza Shop Menu
周围的Pizza Shop Menu
。 在每个循环中,这只是一个小菜单,而不是提示每个循环中有多少 – 这样你只选择L
为大, S
为小而D
为饮料,然后填写数量。 ( 菜单条目可以是大写或小写 )不允许使用无效的菜单选项(不显示错误,菜单只是重新显示)
我没有等到最后总和和总数,而是更容易简单地保持每种类型的销售总额,小计,税金等。您可以比较两种方法并查看您更喜欢的方法。
此外,在每次scanf
输入(格式字符串不使用newline
)之后,清空输入缓冲区以防止scanf
出现跳过条目。 如果您有任何疑问,请查看并告诉我。 有许多方法可以做到这一点,没有比下一个更好的方法(只要它们是正确的)。 这一切都归结为那时的品味和效率。 ( 注意:如果是限制,则不包含其他头文件,因此有更简单的方法可以使用strchr
等检查有效条目。使用string.h
等)
#include #include /* defines are preprocessor commands that simply cause the * preprocessor to do a global search-and-replace of the * labels with values. They traditionally go at the top. * You can add storage type suffixes to the numbers to * prevent ambiguity. (eg F float, U unsigned, ...) */ #define LARGEPIZZAPRICE 12.0F #define SMALLPIZZAPRICE 10.0F #define LARGEPIZZATOPPING 2.0F #define SMALLPIZZATOPPING 1.50F #define DRINK 1.50F #define TAXRATE .05F /* empty input buffer after reading from stdin */ void flush_stdin () { int c = 0; while ((c = getchar()) != 'n' && c != EOF); } int show_menu () { int c = 0; /* quick & dirty menu */ printf ("n Pizza Shop Menunn"); printf (" L) Large Pizzan"); printf (" S) Small Pizzan"); printf (" D) Drinkn"); printf (" --------------------n"); printf (" C) Checkoutnn"); printf (" Choice: "); c = getchar (); flush_stdin (); /* return only upper case letters */ return c > 'Z' ? c - 'a' + 'A' : c; } int main (void) { /* ALWAYS INITIALIZE ALL VARIABLES * accidentally reading from an uninitialized variable * is Undefined Behavior. */ int numberOfLargePizzas = 0; int numberOfSmallPizzas = 0; int numberOfLargeToppings = 0; int numberOfSmallToppings = 0; int numberOfDrinks = 0; float price = 0.0; float subtotal = 0.0; float tax = 0.0; while (1) { int n = 0; switch (show_menu()) { case 'L' : printf ("n How many large pizza's do you want : "); scanf ("%d", &n); flush_stdin (); if (n) { numberOfLargePizzas += n; price = n * LARGEPIZZAPRICE; tax += price * TAXRATE; subtotal += price; n = 0; printf (" How many large toppings do you want: "); scanf ("%d", &n); flush_stdin (); if (n) { numberOfLargeToppings += n; price = n * LARGEPIZZATOPPING; tax += price * TAXRATE; subtotal += price; } } printf ("n Subtotal : %.2fn Tax : %.2fn Total : %.2fn", subtotal, tax, subtotal + tax); break; case 'S' : printf ("n How many small pizza's do you want : "); scanf ("%d", &n); flush_stdin (); if (n) { numberOfSmallPizzas += n; price = n * SMALLPIZZAPRICE; tax += price * TAXRATE; subtotal += price; n = 0; printf (" How many small toppings do you want: "); scanf ("%d", &n); flush_stdin (); if (n) { numberOfSmallToppings += n; price = n * SMALLPIZZATOPPING; tax += price * TAXRATE; subtotal += price; } } printf ("n Subtotal : %.2fn Tax : %.2fn Total : %.2fn", subtotal, tax, subtotal + tax); break; case 'D' : printf ("n How many drinks would you like: "); scanf ("%d", &n); flush_stdin (); if (n) { numberOfDrinks += n; price = n * DRINK; tax += price * TAXRATE; subtotal += price; } printf ("n Subtotal : %.2fn Tax : %.2fn Total : %.2fn", subtotal, tax, subtotal + tax); break; case 'C' : printf ("nOrder:n"); printf (" ------------------------n"); printf (" Large Pizzas : %2dn Large Toppings : %2dn", numberOfLargePizzas, numberOfLargeToppings); printf (" Small Pizzas : %2dn Small Toppings : %2dn", numberOfSmallPizzas, numberOfSmallToppings); printf (" Drinks : %2dn", numberOfDrinks ); printf (" ------------------------n"); printf (" Subtotal : %6.2fn Tax : %6.2fn Total : %6.2fn", subtotal, tax, subtotal + tax); printf (" ------------------------nn"); return 0; default: /* uncomment to show bad menu entry */ // printf (" <--invalid entry-->n"); break; } } return 0; }
例:
$ ./bin/pizzas Pizza Shop Menu L) Large Pizza S) Small Pizza D) Drink -------------------- C) Checkout Choice: l How many large pizza's do you want : 2 How many large toppings do you want: 2 Subtotal : 28.00 Tax : 1.40 Total : 29.40 Pizza Shop Menu L) Large Pizza S) Small Pizza D) Drink -------------------- C) Checkout Choice: d How many drinks would you like: 4 Subtotal : 34.00 Tax : 1.70 Total : 35.70 Pizza Shop Menu L) Large Pizza S) Small Pizza D) Drink -------------------- C) Checkout Choice: c Order: ------------------------ Large Pizzas : 2 Large Toppings : 2 Small Pizzas : 0 Small Toppings : 0 Drinks : 4 ------------------------ Subtotal : 34.00 Tax : 1.70 Total : 35.70 ------------------------
以上就是c/c++开发分享C编程初学者问题相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/519666.html