2012-09-27 33 views
0

這是一個程序,要求用戶輸入有關銷售自行車的運輸信息,非常跛腳。最後,當它打印出自行車訂單的數量和總成本時,數字就會被搞砸。之前輸入的數量似乎在記憶中持續存在。我該如何解決?如果這是沒有問題的,我不會介意被告知這樣:)程序結束後變量保留值,如何預防? C

#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; 
     } 
+1

@JesusRamos該標記已被棄用。請參閱http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated?cb=1 –

+0

@AaronDufour錯過這個職位,謝謝:) –

+0

你能後的輸出,你越來越? – Jay

回答

2

您打印number_orderedprice,這是double S,使用%d%d僅適用於整數類型。使用%lfprintfscanf雙打。

+0

這樣做並沒有給我瘋狂的數字!但它確實給我所有零:( – Church

+0

你必須使用'%lf'雙打讀了。 – nneonneo

+0

好,改變number_ordered整數,並使用這些%d的工作。我如何使用%LF,而不必?0最近連續數個瘋狂的字符串 – Church

0

爲您的scanf函數和你的printf的格式是錯誤的,所以你既不讀也不寫正確的價值觀。