开关盒不打印c

时间:2015-02-13 00:33:04

标签: c switch-statement printf output

再次回答C中的另一个问题。 我的switch语句有些问题。我的程序正在编译并运行它甚至将正确的值存储在正确的变量中并运行switch语句,因为在打印输出特定输出时它应该是EXCEPT。请记住我正在努力学习,而且我的知识非常基础。我很感谢你在这个迷人的社区中的所有帮助,并且我要求你的回答,如果有的话可以帮助我了解更多!

没有进一步的麻烦,这里出现了一段编码文字!

// 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;



}

1 个答案:

答案 0 :(得分:0)

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

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