【问题标题】:Grabbing Switch Statements from For loops从 For 循环中获取 Switch 语句
【发布时间】:2011-10-26 03:31:39
【问题描述】:
#include <stdio.h>

int i, party;
char x = ' ';
float total = 0, perCost = 0;
main(){

            switch (toupper(x)) {
                case 'A':
                    printf("Combo A: Friend Chicken with slaw [price: 4.25]");
                    perCost = 4.24;             
                    break;
                case 'B':
                    printf("Combo B: Roast beef with mashed potato [price: 5.75]");
                    perCost = 5.75;
                    break;
                case 'C':
                    printf("Combo A: Fish and chips [price: 5.25]");
                    perCost = 5.25;
                    break;
                case 'D':
                    printf("Combo A: soup and salad [price: 3.74]");
                    perCost = 3.75;
                    break;
                default:
                    perCost = 0;
                    break;
            }

    printf("Enter Party Total: ");
    scanf("%d", &party);
    for (i = 0; i < party; i++) {
            printf("Enter item ordered [A/B/C/D/X]: ");
            scanf("%c%*c", &x);
    }
    total = total + perCost;
    printf("%f\n", total);
}

是什么导致我的程序无法从 switch 语句中抓取?

【问题讨论】:

  • 这是 C,无论您如何看待它,即使您包含 C++ 标头。重新标记并编辑了标题。

标签: c for-loop switch-statement


【解决方案1】:

根据给定的代码,当第一次执行到switch() 时,x 的值是' ',所以通过执行switch()perCost = 0 执行switch() 中的默认条件让你相信程序 din 抢到了switch()。(注意执行永远不会再回到这里)

要实现您的预​​期,请在 for (i = 0; i &lt; party; i++) 循环内提供 switch(),具体来说,在您的 scanf 下方。

请注意,total = total + perCost; 放错了位置,到目前为止,它不会计算总数,而只会给出您订购的最后一个组合的 perCost。这也应该在循环内。

您的程序中需要#include &lt;cctype.h&gt;

【讨论】:

  • 哦,对了,我忘了 那是这不起作用的唯一原因。哈哈 %c%*c 就是去掉缓冲区中的回车键。
【解决方案2】:

我认为你的 switch 语句需要在循环内:

for (i = 0; i < party; i++) {
    printf("Enter item ordered [A/B/C/D/X]: ");
    scanf("%c%*c", &x);


    //  Put your switch here.
    switch (toupper(x)) {
            case 'A':
                printf("Combo A: Friend Chicken with slaw [price: 4.25]");
                perCost = 4.24;             
                break;
            case 'B':
                printf("Combo B: Roast beef with mashed potato [price: 5.75]");
                perCost = 5.75;
                break;
            case 'C':
                printf("Combo A: Fish and chips [price: 5.25]");
                perCost = 5.25;
                break;
            case 'D':
                printf("Combo A: soup and salad [price: 3.74]");
                perCost = 3.75;
                break;
            default:
                perCost = 0;
                break;
        }



    total = total + perCost;  // Move this into the loop.

}

【讨论】:

  • 是的。就目前而言,在您进入循环之前,您的开关只被评估一次,当 x' ' 时,永远不会再被评估。
  • 他还应该将“total = total + perCost”放在循环内部和切换之后。
  • 这仍然没有计算 perCost.. 它完全符合我的程序。
  • 您是否检查过scanf 是否正确读取输入?阅读后尝试打印出这些字母。
【解决方案3】:

您似乎从未将逻辑重定向回您的交换机。尝试将你的 switch 块移动到一个函数中并从你的 for 循环中调用它。或者,在收到输入后将其粘贴到 for 循环中。

【讨论】:

    【解决方案4】:

    因为你的 switche statemnt 的顺序是错误的......它需要在 scanf 之后的循环内;

    更重要的是,我认为您将两件事混为一谈... .1) 需要显示的内容和 2) 需要处理的内容...试试这个:

    #include <stdio.h>
    using namespace std;
    
    int main() {
    
        int i, party;
        char x = ' ';
        float total = 0, perCost = 0;
    
        printf("Enter Party Total: ");
        scanf("%d", &party);
    
        printf("Combo A: Friend Chicken with slaw [price: 4.25]");
        printf("Combo B: Roast beef with mashed potato [price: 5.75]");
        printf("Combo A: Fish and chips [price: 5.25]");
        printf("Combo A: soup and salad [price: 3.74]");
    
        for (i = 0; i < party; i++) {
             printf("Enter item ordered [A/B/C/D]: ");
             scanf("%c%*c", &x);
    
             switch (toupper(x)) {
                    case 'A':
                        perCost = 4.24;             
                        break;
                    case 'B':
                        perCost = 5.75;
                        break;
                    case 'C':
                        perCost = 5.25;
                        break;
                    case 'D':
    
                        perCost = 3.75;
                        break;
                    default:
                        perCost = 0;
                        break;
                }
    
                total = total + perCost;
        }
        printf("Total is: %f\n", total);
    }
    

    【讨论】:

    • 这还不算总数。
    猜你喜欢
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多