gpt4 book ai didi

C编程初学者问题

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:30 26 4
gpt4 key购买 nike

好的,所以我正在尝试构建一个基本程序来计算披萨订单的价格。我想让它询问客户是否已完成订购。如果他们输入 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]|

在 while 循环开始的第 19 行。

#include <stdio.h>
#include <stdlib.h>

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 want\n");
scanf(" %d", &numberOfLargePizzas);

printf("How many large toppings do you want\n");
scanf(" %d", &numberOfLargeToppings);

printf("How many small pizza's do you want\n");
scanf(" %d", &numberOfSmallPizzas);

printf("How many small toppings do you want\n");
scanf(" %d", &numberOfSmallToppings);

printf("Would you like to order more. Enter a y or n\n");
scanf(" %i", &keepGoing);
}



}`

*****更新*****好的,感谢所有帮助,它现在运行良好。如果有人可以查看它并提供任何提示来收紧它或做我正在做的事情更容易,请告诉我。这对我来说是学习经验,我正在通过反复试验来做到这一点。该程序可以运行,但我觉得我的结构有误。这是我拥有的:

#include <stdio.h>
#include <stdlib.h>

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 want\n");
scanf(" %d", &numberOfLargePizzas);

if(numberOfLargePizzas != 0){
printf("How many large toppings do you want\n");
scanf(" %d", &numberOfLargeToppings);
}

printf("How many small pizza's do you want\n");
scanf(" %d", &numberOfSmallPizzas);
if(numberOfSmallPizzas !=0){
printf("How many small toppings do you want\n");
scanf(" %d", &numberOfSmallToppings);
}

printf("How many drinks would you like\n");
scanf(" %int", &numberOfDrinks);
printf("Would you like to order more. Enter a y or n\n");
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 和 pointer 是不兼容的类型。

关于C编程初学者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29400529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com