繁体   English   中英

程序验证问题。 请帮忙!

[英]Issues with program validation. Please help!

//伙计们,我的代码有问题,一直在试图解决这个问题。 问题是我正在尝试验证我的代码,因此它不会计算负数。 如果有人可以帮助简化此程序,我将非常感激。 请。

//税收计算器程序的目标是使用C编程计算每个Kudler Fine Food商店(DelMar,Encinitas和La Jolla)的营业税//标准输入/输出处理

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

这行有错误:

scanf(“ $ %f”,&fsaPurchaseAmount); //用户输入可变金额

应该是:

scanf(“%f”,&fsaPurchaseAmount); //用户输入可变金额

以下是有关所提供代码的一些观察结果:

  • 不要为了钱而使用浮点数。 只会在以后引起悲伤。 理想情况下,请咨询会计专家。 同时,请以美分为单位进行算术运算,然后缩放至与美元成比例显示。

  • 诸如营业税之类的计算可能是通过浮点乘数正确完成的,但是请确保您遵循公认的惯例将其四舍五入至整分。 同样,请向会计专业人士寻求建议。

  • fflush()不会执行您认为的操作。 对于打开的用于写入的文件描述符(例如stdout ),它保证该描述符上的所有输出均已完成。 打印提示之后并调用诸如scanf()以读取输入之前使用。 一个示例是: printf("What is your favorite color? "); fflush(stdout); fgets(color, sizeof(color), stdin); printf("What is your favorite color? "); fflush(stdout); fgets(color, sizeof(color), stdin);

  • 始终检查scanf()的返回值。 它返回成功的转换数。 如果它与格式说明符的数量不匹配,则只有那些成功的格式说明符才会将值写入命名变量。

  • 警惕在scanf()格式字符串中出现的空格以外的文字字符。 这些必须与输入文本完全匹配,否则转换将失败。 因此,像"$%f"这样的格式仅与包含文字美元符号的输入匹配。 这可能不是您的用户所期望的。 格式"%f"对用户来说更容易。 如果要允许可选的美元符号,则scanf()可能不是最佳选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM