【问题标题】:Logic Error in C Program on xcodexcode 上的 C 程序中的逻辑错误
【发布时间】:2016-01-14 00:01:42
【问题描述】:

我为杂货店编写了这段代码。这段代码对我来说非常有意义。但是,我不断收到逻辑错误。每次用户输入账单然后按 -1 退出时,他都会返回到主菜单。当用户按下选项 2 退出程序时,程序没有退出,他出于某种原因回到案例 1。请你帮助我好吗?谢谢!

#include <stdio.h>

int main(void){
    double prices[7];
    prices[0]=2.55;
    prices[1]=12.07;
    prices[2]=2.00;
    prices[3]=0.55;
    prices[4]=5.35;
    prices[5]=8.65;
    prices[6]=2.55;
    int choice;
    int productCode;
    int quantity;
    char stop[3];
    int compare;
    double price;
    double totalPrice=0;

    do{
        printf("\n1. Create new bill");
        printf("\n2. EXIT");
        printf("\n\nEnter choice: ");
        choice=scanf("%d", &choice);

        switch(choice){
            case 1:{
                do{
                    printf("\nEnter product code: ");
                    scanf("%d",&productCode);
                    printf("\nEnter quantity of product: ");
                    scanf("%d",&quantity);
                    price=prices[productCode]*quantity;
                    totalPrice=totalPrice+price;
                    printf("\nTo stop entering products enter -1.. to continue press any other character ");
                    scanf("%s", &stop);
                    compare=strcmp(stop, "-1");
                }while(compare!=0);
                break;
            }
            case 2: break;

            default: printf("\nInvalid choice");
        }
    }while(choice!=2);

    getchar();
    return 0;
}

【问题讨论】:

  • 调试:添加断点,显示变量值,跟踪执行。
  • 你应该使用return而不是中断,在这种情况下“case 2:”应该返回0来结束程序
  • “每次用户输入账单然后按-1退出时,他都会被带回主菜单”。 case 块中的 break 将退出封闭的 switch。在您的情况下,它将通过评估 while(choice!=2) 来继续 do 循环。该条件成立,因为 choice1。因此主菜单再次打印出来。
  • @kaylum 问题不在于重新打印主菜单。它是关于我按数字 2 退出但程序没有退出的事实。相反,它又回到了案例 1。感谢您尝试提供帮助! :)
  • 您还应该检查产品代码是否在price=prices[productCode]*quantity;之前的范围内。

标签: c xcode logic


【解决方案1】:

代替

choice=scanf("%d", &choice);

scanf("%d", &choice);

scanf返回值为:

成功时,函数返回参数的项目数 列表成功填写。这个计数可以匹配预期的数量 由于匹配失败,项目或更少(甚至为零),读数 错误,或到达文件末尾。

如果发生读取错误或到达文件结尾同时 阅读时,设置了正确的指标(feof 或 ferror)。而且,如果有的话 在成功读取任何数据之前发生,返回 EOF。

如果在解释宽字符时发生编码错误, 函数将 errno 设置为 EILSEQ。 http://www.cplusplus.com/reference/cstdio/scanf/

for (;;){

    printf("\n1. Create new bill");
    printf("\n2. EXIT");
    printf("\n\nEnter choice: ");

    scanf("%d", &choice); 

    if(choice == 2 ){
        break;
    } else if(choice == 1){

        do{

            printf("\nEnter product code: ");
            scanf("%d",&productCode);
            printf("\nEnter quantity of product: ");
            scanf("%d",&quantity);
            price=prices[productCode]*quantity;
            totalPrice=totalPrice+price;
            printf("\nTo stop entering products enter -1.. to continue press any other character ");
            scanf("%s", &stop);
            compare=strcmp(stop, "-1");

        }while(compare!=0);

    } else {
        printf("\nInvalid choice");
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 2022-09-27
    • 1970-01-01
    相关资源
    最近更新 更多