2009-05-19 94 views
1

//夥計們我有我的代碼問題,並試圖解決這個問題,一直在撕裂我的頭髮。這個問題是,我試圖驗證我的代碼,因此它不計算負數。如果有人能夠幫助理順這個計劃,我會非常感激。請。程序驗證問題。請幫忙!

//稅計算器程序的目標是用C編程calcualte銷售稅對每個Kudler精美食品商店(德爾馬,恩西尼塔斯和拉霍亞) //標準輸入/輸出處理

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


//Starting point  
int main() 
{ 
//Variables defined 
//st = sales tax, tax rated = tr, pa = total purchase amount, sa = purchase amoount 

    float fstDelmar; 
    float ftrDelmar; 
    float fpaDelmar; 
    float fstEncinitas; 
    float ftrEncinitas; 
    float fpaEncinitas; 
    float fstLajolla; 
    float ftrLajolla; 
    float fpaLajolla; 
    float fsaPurchaseAmount; 
    int fStoreselect; 

//Variable initializations for tax rates and purchase amount 
    ftrDelmar = .0725; 
    ftrEncinitas = .075; 
    ftrLajolla = .0775; 
    fsaPurchaseAmount = 0; 


//Header & Introduction 
    printf("\n***********************************************************************"); 
    printf("\n*       Kudler Fine Foods       *"); 
    printf("\n*      Sales Tax Calculator       *"); 
    printf("\n*       Version 3.0        *"); 
    printf("\n***********************************************************************\n"); 


//Ask user to select store. 

    printf ("\n     STORE LOCATION \n"); 
    printf ("\n     1) Del Mar \n"); 
    printf ("\n     2) Encinitas \n"); 
    printf ("\n     3) La Jolla \n"); 

    printf ("\n Please select the store location (1-3): \n"); 
    scanf ("%d", &fStoreselect);  

//Validate store selection. 
    while (fStoreselect < 1 || fStoreselect > 3) { 
     fflush (fStoreselect); 
     printf ("INVALID SELECTION! Please select a valid location (1-3): \n"); 
     scanf ("%d", &fStoreselect); 
     } 
//Ask user to enter in total purchase amount. 

    printf ("\n What was your purchase amount? "); 
    scanf("$%f", &fsaPurchaseAmount); //user enters variable amount 


//Validation to ensure that user's enter in purchase amounts using correct format. 

    while (fsaPurchaseAmount <= 0.0) 
     { 
     fflush(fsaPurchaseAmount);           
     printf("\n INVALID SELECTION! Please enter a valid purchase amount greater than zero.\n"); 
     printf("\n The purchase amount is: $ "); 
     scanf("%f", &fsaPurchaseAmount);  
     } 

//Calculation of sales tax in dollars for each of the store locations 
    fstDelmar = fsaPurchaseAmount * ftrDelmar; 
    fstEncinitas = fsaPurchaseAmount * ftrEncinitas; 
    fstLajolla = fsaPurchaseAmount * ftrLajolla; 

//Calculation of total sales amount for each of the locations 

    fpaDelmar = fsaPurchaseAmount + fstDelmar; 
    fpaEncinitas = fsaPurchaseAmount + fstEncinitas; 
    fpaLajolla = fsaPurchaseAmount + fstLajolla; 

//Displaying sales amount for purchase for each of the different locations 

    switch (fStoreselect) { 

     case 1: 
    //for Delmar location 
     printf("\n  Purchase Amount  Sales Tax  Total Sales Amount "); 
     printf("\n  _______________  _________  _________________ "); 
     printf("\n   $%.2f    $%.2f   $%.2f",fsaPurchaseAmount, fstDelmar, fpaDelmar); 
     break; 

     case 2: 
    //for Encinitas location 
     printf("\n  Purchase Amount  Sales Tax  Total Sales Amount "); 
     printf("\n  _______________  _________  _________________ "); 
      printf("\n   $%.2f    $%.2f   $%.2f",fsaPurchaseAmount, fstEncinitas, fpaEncinitas); 
      break; 

      case 3: 
     //for La Jolla location 
      printf("\n  Purchase Amount  Sales Tax  Total Sales Amount "); 
     printf("\n  _______________  _________  _________________ "); 
      printf("\n   $%.2f    $%.2f   $%.2f",fsaPurchaseAmount, fstLajolla, fpaLajolla); 
     break; 
     } 

    printf("\n Hit the ENTER key to exit program\n"); 
//Pause the screen and wait for user to hit the ENTER key 
    getchar(); 

//EOF  
} 
+0

你看到了什麼問題? – 2009-05-19 00:12:23

+0

當我運行代碼時,我可以選擇商店並且驗證效果很好,但是當我輸入購買金額時,我收到了無效的消息,並且仍然能夠輸入負值。不知道我的while語句是否有問題? – Sara 2009-05-19 00:15:11

+0

我想知道爲什麼在`fsaPurchaseAmount`上使用`fflush()`。 – beef2k 2009-05-19 00:17:32

回答

2

此行有錯誤:

的scanf( 「$%F」,& fsaPurchaseAmount); //用戶輸入變量量

它應該是:

的scanf( 「%F」,& fsaPurchaseAmount); //用戶輸入變量量

1

以下是有關的代碼中的一些觀察所呈現:

  • 不要使用浮點錢。它只會在以後引起悲傷。理想情況下,從會計專業人員那裏獲得建議。同時,以美分算術,並按比例縮放以顯示。

  • 像銷售稅計算可能正確地做一個浮點乘法,但要確保你遵循接受的做法四捨五入到整個美分。再次向會計專業人士諮詢。

  • fflush()不會做你認爲的事情。對於打開的文件描述符(例如stdout),它保證該描述符上的所有輸出已完成。它在之後使用,在之前打印提示並且調用諸如scanf()的東西來讀取輸入。一個例子是:printf("What is your favorite color? "); fflush(stdout); fgets(color, sizeof(color), stdin);

  • 總是檢查返回值scanf()。它返回成功的轉換次數。如果它與格式說明符的數量不匹配,那麼只有那些成功的值纔會寫入命名變量。

  • 請注意除scanf()格式字符串中出現的空格以外的字面字符。這些必須完全由輸入文本匹配,否則轉換失敗。所以像"$%f"這樣的格式只能通過包含字面美元符號的輸入進行匹配。這可能不是您的用戶期望的。格式"%f"對用戶來說更容易。如果你想允許一個可選的美元符號,那麼scanf()可能不是最好的選擇。