gpt4 book ai didi

c - 程序终止后变量保留值,如何防止? C

转载 作者:行者123 更新时间:2023-11-30 14:23:42 24 4
gpt4 key购买 nike

这是一个要求用户输入有关销售自行车的运输信息的程序,非常蹩脚。最后,当它打印出自行车订单数量和总成本时,数字被搞砸了。先前输入的金额似乎已牢牢地留在内存中。我该如何解决?如果这不是问题,我不介意被告知:)

#include <stdio.h>
#include <math.h>
//structure
typedef struct
{char cust_name[25];
char add_one[20];
char add_two[20];
}ORDER;

ORDER order;

int main(void){
fflush(stdin);
system ( "clear" );

//initialize variables
double number_ordered = 0;
double price;
char bike;
char risky;
double m = 359.95;
double s = 279.95;
//inputs for order
printf("Enter Customer Information\n");
printf("Customer Name: ");
scanf(" %[^\n]s", &order.cust_name);

printf("\nEnter Street Address: ");
scanf(" %[^\n]s", &order.add_one);

printf("\nEnter City, State, and ZIP: ");
scanf(" %[^\n]s", &order.add_two);

printf("\nHow Many Bicycles Are Ordered: ");
scanf(" %d", &number_ordered);

printf("\nWhat Type Of Bike Is Ordered\n M Mountain Bike \n S Street Bike");
printf("\nChoose One (M or S): ");
scanf(" %c", &bike);

printf("\nIs The Customer Risky (Y/N): ");
scanf(" %c", &risky);

system ( "clear" );

//print order
printf("\n**********Shipping Instructions**********");
printf("\nTo: %s\n %s\n %s", order.cust_name, order.add_one, order.add_two);

if (bike == 'M' || bike == 'm')
printf("\n\nShip: %d Mountain Bikes", number_ordered);
else
printf("\n\nShip: %d Street Bikes", number_ordered);

if (bike == 'M' || bike == 'm')
price = number_ordered * m;
else
price = number_ordered * s;

if (risky == 'Y' || risky == 'y')
printf("\nBy Freight, COD %d\n", price);
else
printf("\nBy Freight, And Bill The Customer %d\n", price);


printf("*****************************************\n");
return 0;
}

最佳答案

您正在使用 %d 打印 number_orderedprice,它们是 double%d 仅适用于整数类型。使用 %lfprintfscanf double 。

关于c - 程序终止后变量保留值,如何防止? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613732/

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