【发布时间】:2020-08-18 09:18:07
【问题描述】:
我正在阅读 C 编程绝对初学者指南第 3 版来学习编程,我在 Chapter8ex2.c 遇到了问题,我的程序在哪里获得成本,然后应该获得最高。相反,它会绕过它并重用第一个变量,而不是使用正确的第二个变量。
这是我的代码
// This is a sample program that asks users for some basic data and prints it on
// the screen in order to show what was entered
#include <stdio.h>
int main()
{
char topping[24];
int slices;
int month, day, year;
float cost;
// The first scanf will look for a floating-point variable, the cost of the pizza
// If the user doesn't enter a $ before the cost, it could cause problems
printf("How much does a pizza cost in your area?");
printf("enter as $XX.XX)\n");
scanf(" $%f", &cost);
// The pizza topping is a string, so your scanf doesn't need an &
printf("What is your favorite one-word pizza topping?\n");
scanf(" %s", topping);
printf("How many slices of %s pizza", topping);
printf("can you eat in one sitting?\n");
scanf("%i", &slices);
printf("What is today's date (enter it in XX/XX/XX format).\n");
scanf(" %i/%i/%i", &month, &day, &year);
printf("\n\nWhy not treat yourself to dinner on %i/%i/%i", month, day, year);
printf("\nand have %i slices of %s pizza!\n", slices, topping);
printf("It will only cost you $%.2f!\n\n\n", cost);
return(0);
}
这是正在运行的代码
How much does a pizza cost in your area?enter as $XX.XX)
15.00
What is your favorite one-word pizza topping?
How many slices of 15.00 pizzacan you eat in one sitting?
【问题讨论】:
-
“绝对初学者”和“scanf”不能一起使用。在您充分理解该语言并知道为什么不需要它之前,请不要触摸 scanf。
-
@WilliamPursell scanf 没有问题,这对整个 C 来说也不会有问题。
-
@user3121023 成功了!感谢您提供了惊人的有用评论,您能否将其发布为答案,我会这样标记它!
-
@MintCollie 删除了什么?什么“骚扰”? (一年后?)
-
@MintCollie 如果有人骚扰你,可以解决;没有必要删除问题。 (这个问题没有什么特别的问题。)