【问题标题】:C formula can't get correct valueC公式不能得到正确的值
【发布时间】:2016-07-14 17:13:15
【问题描述】:

我需要有关以下代码公式的帮助:

#include <stdio.h>
void main()
{
    int MAGNUM, AK47, Knife, Axe, total;
    char choice, choice2, confirm;

    MAGNUM = 75;
    AK47 = 150;
    Knife = 40;
    Axe = 20;

    printf("Welcome to weapon store.\n");
    printf("Over here, we sell cheap weaponry you may be interested in.\n");
    printf("Currently, this is what we have for sale:\n\n\n");
    printf("(A) .44 MAGNUM\n");
    printf("(B) AK-47\n");
    printf("(C) Laredo Bowie knife\n");
    printf("(D) Tomahawk axe\n\n\n");
    printf("Pick one to buy, just type the letter in caps of the item and press enter.\n");
    printf("ITEM SELECTED:");
    scanf("%c", &choice);   //First input//

    if (choice == 'A')
    {
        printf("That will be $%d.\n", MAGNUM);
    }

    if (choice == 'B')
    {
        printf("That will be $%d.\n", AK47);
    }
    if (choice == 'C')
    {
        printf("That will be $%d.\n", Knife);
    }
    if (choice == 'D')
    {
        printf("That will be $%d.\n", Axe);
    }
    printf("Do you want to buy anything else?<Y//N>\n");    //Second input//
    scanf(" %c", &choice2);

    if (choice2 == 'Y')
    {
        printf("What else do you want to buy:");    //Third input//
        scanf(" %c", &confirm);
        if (confirm == 'A')
        {
            printf("That will be $%d.\n ", MAGNUM);
        }
        else if (confirm == 'B')
        {
            printf("That will be $%d.\n", AK47);
        }
        else if (confirm == 'C')
        {
            printf("That will be $%d.\n", Knife);
        }
        else if (confirm == 'D')
        {
            printf("That will be $%d.\n", Axe);
        }

        total = choice + confirm;   //Need help with formula here. total equals first input + third input//

        printf("Total cost is $%d. Thank you.\n", total);
    }
    else
    {
        printf("Thank you for shopping with us.\n");    
    }
}

调试时,将输入 1 和 3 加在一起时不会显示正确的值。

例如,如果我分别为输入 1 和 3 选择 D,然后再次选择 D,我应该得到总共 40 美元,但我得到了不同的值 136 美元。

这可能是我目前缺乏 C 知识,因为我 2 天前才开始。这段代码只是我对 if else 语句等基础知识的测试,所以如果代码中的内容可能冒犯任何人,我深表歉意。

【问题讨论】:

  • 虽然我不喜欢武器,但这不是你问题的冒犯部分。请查看如何提供minimal reproducible example
  • 您不是在添加价格,而是在添加用户输入的字母。 'D' 是字符代码68,所以choice + confirm = 136
  • 添加另一个变量,如totalPrice 或其他东西。然后+=每个 if 的价格。
  • “缺乏 C++ 知识”。这是 C,不是 C++。
  • @πάνταῥεῖ 您已经在这里待了足够长的时间,知道我们几乎没有得到它,这只是名义上的要求。这个问题比大多数“它不起作用”的问题要好得多。他运行了这个程序。他给出了输入 D 和 D。他没有得到预期的结果 40,而是得到了 136。

标签: c printf formula scanf


【解决方案1】:

您添加的是字符值而不是价格。

68 是 'D' 解释为整数。 68+68=136。

我会在您的 if 语句中添加价格,例如

if (choice == 'D')
{
printf("That will be $%d.\n", Axe);
total=total+Axe;
}

【讨论】:

  • 别忘了初始化total
【解决方案2】:

(代表 OP 发布)

把 C++ 作为标题是我的错误。奇怪地没有意识到这一点。以前也不知道字符值。

初始化总计 = total1 + total2,其中 total1 表示输入 1 的选择结果的值,total2 表示输入 3 的选择结果的值,解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2020-07-20
    相关资源
    最近更新 更多