【问题标题】:switch case not printing c开关盒不打印 c
【发布时间】:2015-02-13 00:33:04
【问题描述】:

再次回到 C 中的另一个问题。 我的 switch 语句有一些问题。我的程序正在编译和运行它,甚至将正确的值存储在正确的变量中并运行 switch 语句,因为它应该除了打印出特定的输出。请记住,我正在努力学习,我的知识非常基础。感谢您在这个迷人的社区中提供的所有帮助,我希望您的回复能帮助我了解更多信息!

不用多说,这里是一堵编码文本!

// Point of sale software that lists a set of packages with a subset of possible upgrades for each
/*This C program was written with a zoom value of 100 and with the use of wordwrap
the native resolution of the screen is set to 1920x1080
for easiest veiwing make sure wordwrap is up*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
    int  package = 0, memory = 0, hd = 0, monitor = 0, gpu = 0, another = 0;
    float  price = 0, memprice = 0, hdprice = 0, monprice = 0, gpuprice = 0, subtotal = 0, hst = 0, total = 0;
    printf("Welcome to the IPC Company's Computer System Calculator\nthe package desired\n(1: basic, 2 : professional, 3 : game system) : ");
    scanf("%d", &package);
    do // the condition to break the loop is to have the user input 0 into the variable another
    {
        if (package == 1) //selects the basic package and stores user selections of upgrades
        {
            price = 599;
            printf("Enter additional memory required\n(0: 4 GB included, 1 : 8 GB, 2 : 12 GB) : ");
            scanf("%d", &memory);
            printf("Enter monitor required\n(0: 21 inch LED included, 1 : 27 inch LED) : ");
            scanf("%d", &monitor);
            printf("Enter Hard Drive drive required\n(0: 512 GB included, 1 : 128 GB SSD) : ");
            scanf("%d", &hd);
            printf("\n\n========================================\n");
            printf("Basic Package                     599.00\n");
        }
        else if (package == 2)// selects the professional package and stores user selections of upgrades
        {
            price = 899;
            printf("Enter additional memory required\n(0: 8 GB included, 1 : 16 GB) : ");
            scanf("%d", &memory);
            memory += 3;
            printf("Enter monitor required\n(0: 27 inch LED included, 1 : 32 inch LED) : ");
            scanf("%d", &monitor);
            monitor += 2;
            printf("Enter Hard Drive drive required\n(0: 1 TB included, 1 : 256 GB SSD, 2 : 512 GB SSD) : ");
            scanf("%d", &hd);
            hd += 2;
            printf("Enter Video Card required\n(0: IGP included, 1 : 2 GB Discrete) : ");
            scanf("%d", &gpu);
            gpu += 1;
            printf("\n\n========================================\n");
            printf("Professional Package              899.00\n");
        }
        else
        {
            price = 1499; //selects the gaming package and stores user selections for upgrades
            printf("Enter additional memory required\n(0: 16 GB included, 1 : 32 GB) : ");
            scanf("%d", &memory);
            memory += 5;
            printf("Enter monitor required\n(0: 32 inch LED included, 1 : 28 inch 4K HD) : ");
            scanf("%d", &monitor);
            monitor += 4;
            printf("Enter Hard Drive drive required\n(0: 256 GB SSD included, 1 : 512 GB SSD, 2 : 1 TB SSD) : ");
            scanf("%d", &hd);
            hd += 5;
            printf("Enter Video Card required\n(0: 2 GB Discrete included, 1 : 4 GB Discrete) : ");
            scanf("%d", &gpu);
            gpu += 3;
            printf("\n\n========================================\n");
            printf("Gaming Package                   1499.00\n");
            //values for each variable is upcast by an appropriate amount to keep input either 0 1 2 while storing them as 0 to 8+ to allow 
            //the use of four switches as apposed to a switch for each individual package/component combination
            //partial credit to professor joseph hughes for shedding light on this methodology
        }
        switch (memory)//4 switch catagories based on memory hd gpu and monitor each containing a pre defined price
        {
        case 0:
            memprice = 0;
            printf("4 GB Memory:                        0.00\n");
            break;
        case 1:
            memprice = 99;
            printf("8 GB Memory:                       99.00\n");
            break;
        case 2:
            memprice = 189;
            printf("12 GB Memory:                     189.00\n");
            break;
        case 3:
            memprice = 0;
            printf("8 GB Memory:                        0.00\n");
            break;
        case 4:
            memprice = 189;
            printf("16 GB Memory:                     189.00\n");
            break;
        case 5:
            memprice = 0;
            printf("16 GB Memory:                       0.00\n");
            break;
        case 6:
            memprice = 389;
            printf("32 GB Memory:                     389.00\n");
            break;
        }
        switch (monitor)
        {
        case 0:
            monprice = 0;
            printf("21 inch LED Monitor                 0.00\n");
            break;
        case 1:
            monprice = 199;
            printf("27 inch LED Monitor               199.00\n");
            break;
        case 2:
            monprice = 0;
            printf("27 inch LED Monitor                 0.00\n");
            break;
        case 3:
            monprice = 199;
            printf("32 inch LED Monitor               199.00\n");
            break;
        case 4:
            monprice = 0;
            printf("32 inch LED Monitor                 0.00\n");
            break;
        case 5:
            monprice = 299;
            printf("28 inch 4K LED Monitor            299.99\n");
            break;
        }
        switch (hd)
        {
        case 0:
            hdprice = 0; printf("512 GB Hard Drive                   0.00\n");
            break;
        case 1:
            hdprice = 119;
            printf("128 GB SSD                        119.00\n");
            break;
        case 2:
            hdprice = 0;
            printf("1 TB Hard Drive                     0.00\n");
            break;
        case 3:
            hdprice = 189;
            printf("256 GB SSD                        189.00\n");
            break;
        case 4:
            hdprice = 399;
            printf("512 GB SSD                        399.99\n");
            break;
        case 5:
            hdprice = 0;
            printf("256 GB                              0.00\n");
            break;
        case 6:
            hdprice = 299;
            printf("512 GB SSD                        299.00\n");
            break;
        case 7:
            hdprice = 599;
            printf("1TB SSD                           599.00\n");
            break;
        }
        switch (gpu)
        {
        case 0:
            gpuprice = 0;
            break;
        case 1:
            gpuprice = 0;
            printf("IGP Video                           0.00\n");
            break;
        case 2:
            gpuprice = 209;
            printf("Discrete 2GB Video                209.00\n");
            break;
        case 3:
            gpuprice = 0;
            printf("Discrete 2GB Video                  0.00\n");
            break;
        case 4:
            gpuprice = 399;
            printf("Discrete 4GB Video                399.00\n");
            break;
        }

        subtotal = price + memprice + monprice + gpuprice + hdprice;
        hst = subtotal*.13;
        total = subtotal + subtotal*.13;

        printf("========================================\n");
        printf("Sub Total:                       %.2f\n", subtotal);
        printf("HST:                              %.2f\n", hst);
        printf("========================================\n");
        printf("Total:                           %.2f\n", total);

        printf("Do you wish to choose another computer package? (1: YES or 0: NO): ");
        scanf("%d", &another);
        // note most prices are registerd as string literals and not variable calls with the exclusion of subtotal hst and total
    } while (another == 1);


    return 0;



}

【问题讨论】:

  • memorymonitorhdgpu 包含 int 值,而不是 'char' 值,您的 case 语句正在测试。
  • 另外,你的一些scanf 调用看起来很可疑:scanf("%d", hd); 应该是scanf("%d", &amp;hd);(后者将它读取的整数存储到hd;前者将它写入某个垃圾地址派生自 hd 碰巧包含的任何值!)
  • 我的意思是,如果您将值1 输入一个整数,您的case 语句应该测试值1 而不是字符'1'。你看得到差别吗?通过测试'1',编译器将看到测试值49,这是数字字符'1'的ASCII值
  • @FadyAlkarmi:如果您启用了警告,您编译的将/应该警告scanf 问题。我强烈建议在 GCC 或 CLang 中使用 -Wall(如果您正在使用其他编译器,请查看您的编译器手册)。
  • 在 switch 语句中,最好总是包含一个 'default:' 情况,尤其是在处理来自用户的输入时,否则,程序如何知道输入是否有效。还建议在进入下一个输入之前测试每个输入(可能在循环中或通过为每个输入调用子函数)以确保输入值有效。

标签: c switch-statement printf output


【解决方案1】:

变量memory, hd, monitor 等被声明为整数,但您的switch 语句使用类型char。只需删除单引号...

case '1': // this is a char
case 1:   // this is an integer 

【讨论】:

  • 次要 nitpick:'1' 仍然有类型 int - 但它可能没有与 1 相同的值。
  • 非常感谢!我的导师和他的笔记都没有提到这一点。这就是为什么我真的很感激这个社区,并希望有一天我能够再次回馈感谢,并度过一个美好的夜晚/一天!
  • @MartinTörnwall 是的,'1' 不暗示任何数据类型,它是在使用它的上下文中表达数值的一种方式,在这种情况下是 switch(memory)int .
  • @user3629249 您没有阅读 Martin Törnwall 的链接(或这些 cmets)吗? c 是一个字符(1 字节),但 'x' 不是。
猜你喜欢
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多