0

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

#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;
        }
4

2 回答 2

2

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

于 2012-09-27T03:43:17.007 回答
0

你的 scanf 和你的 printf 格式都是错误的,所以你既不能正确读取也不能正确写入值。

于 2012-09-27T03:50:49.960 回答