0

back again with another question in C. i'm having some issues with my switch statements. my program is compiling and running its even storing the correct values in the correct variables and running through the switch statements as it should EXCEPT when it comes to print out specific output. please keep in mind i am trying to learn and my knowledge is very basic. i appreciate all your help in this fascinating community and i ask that your responses if any would help me learn more!

without further ado here comes a wall of coded text!

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



}
  • `memory` and `monitor` and `hd` and `gpu` contain `int` values, not `'char'` values that your `case` statements are testing. – Weather Vane Feb 13 '15 at 00:37
  • 1
    Also, some of your `scanf` calls look suspicious: `scanf("%d", hd);` should be `scanf("%d", &hd);` (the latter stores the integer it reads into `hd`; the former writes it to some garbage address that is derived from whatever value `hd` happened to contain!) – Martin Törnwall Feb 13 '15 at 00:39
  • thanks martin! i overlooked those went back and fixed em! – Fady Alkarmi Feb 13 '15 at 00:40
  • For better answers in the future, please read the article on [creating a minimal, complete and verifiable example](http://stackoverflow.com/help/mcve). – Martin Törnwall Feb 13 '15 at 00:40
  • thank you for the quick reply as well weather i'm not sure i understand fully what you're saying do switch statements need char's to run? if not did i accidentally declare them to be chars? and when declaring my variables if i replaced int with char will that help? – Fady Alkarmi Feb 13 '15 at 00:41
  • 1
    I am saying that if you input a value of `1` to an integer your case statement should test for the value `1` and not the character `'1'`. Do you see the difference? By testing for `'1'` the compiler will see that as testing for the value `49`, which is the ASCII value of the numeric character `'1'` – Weather Vane Feb 13 '15 at 00:43
  • 2
    @FadyAlkarmi: Your compiled would/should have warned about the `scanf` issue if you had warnings enabled. I *strongly* recommend using `-Wall` in GCC or CLang (check your compiler's manual if you're using some other compiler). – Tim Čas Feb 13 '15 at 00:56
  • 1
    on a switch statement, it is best to always include a 'default:' case, especially when working with input from a user, because otherwise, how is the program to know if the input was valid. Also suggest testing each input before going to the next input (possibly in a loop or by calling a sub function for each input) to assure the input values are valid. – user3629249 Feb 13 '15 at 01:10
  • always check the returned value from scanf() (and family) to assure the input/conversion operation was successful – user3629249 Feb 13 '15 at 01:10
  • Yep - I suggest you make a habit out of using `default: assert(0);` or similar in your `switch` statements so that your program immediately aborts when something unexpected happens. – Martin Törnwall Feb 13 '15 at 01:12
  • the addition of the hardcoded values to the results of the scanf() calls, generally, place the values outside/beyond the range of the switch statements. BTW: when a char is passed to a switch statement it is automatically promoted to int See the prior comment as to what that int value will contain – user3629249 Feb 13 '15 at 01:14
  • Perhaps I'm missing something (I did do a refresh of the page) I do not see where any of the corrections have been applied to the posted code – user3629249 Feb 13 '15 at 01:21
  • i havnt posted them yet im still tweaking the source code perfecting my logic and applying some of the suggestions here. unfortunately being a newbie at this most of your suggestions are just going over my head. while i do understand some of the concepts the syntax is lost on me FOR NOW! thank you all again :) – Fady Alkarmi Feb 13 '15 at 01:28

1 Answers1

0

The variables memory, hd, monitor etc, are declared as integers but your switch statements are using type char. Just remove the single quotes...

case '1': // this is a char
case 1:   // this is an integer 
studio h
  • 49
  • 1
  • 4
  • 2
    Minor nitpick: `'1'` still has type `int` - but it probably doesn't have the same value as `1`. – Martin Törnwall Feb 13 '15 at 00:41
  • Thank you very much! neither my instructor nor his notes said anything about that. this is why i really appreciate this community and hopefully one day ill be able to give back thank you again and have a great night/day! – Fady Alkarmi Feb 13 '15 at 00:47
  • @MartinTörnwall yes, `'1'` does not imply any data type, it is a way of expressing a numeric value in the context in which it is used, in this case `switch(memory)` an `int`. – Weather Vane Feb 13 '15 at 00:52
  • 1
    @WeatherVane see http://stackoverflow.com/questions/20764538/type-of-character-constant – Martin Törnwall Feb 13 '15 at 00:55
  • @MartinTörnwall I see, thank you, so in the case of say `char c = 'x'` the value gets demoted to `char`. – Weather Vane Feb 13 '15 at 01:02
  • not exactly, 'x' is already a char (one byte) when passed to something that expects an int, its contents will be promoted to int. – user3629249 Feb 13 '15 at 01:18
  • 1
    @user3629249 didn't you read Martin Törnwall's link (or these comments)? `c` is a char (1 byte) but `'x'` isn't. – Weather Vane Feb 13 '15 at 01:42
  • @user3629249 I tried `printf("%zu %zu %zu\n", sizeof(char), sizeof('x'), sizeof(int));` and came up with `1 4 4`. Certainly `'x'` has the same size as an `int`. What does your C (not C++) compiler report? – chux - Reinstate Monica Feb 13 '15 at 03:29