Thread: i need alil help with my switch question

  1. #1
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Unhappy i need alil help with my switch question

    I have a question here that i cant solve...

    4.19 A mail order house sells five different products whose retail price are shown in the following


    Product Number ------ Retail Price

    1 ------ $ 2.98
    2 ------ $ 4.50
    3 ------ $ 9.98
    4 ------ $ 4.49
    5 ------ $ 6.87

    Write a programm that reads a series of pairs of numbers as follows:

    1. Product number
    2. Quantity sold for one day

    Your program should use a switch statment to help determine the retail price for each priduct.Your programm should calculate and display the total retail values of all products sold last week.

    This question is from a book which i am learning C from...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > I have a question here that i cant solve...

    Perhaps if you actually tried, you'd be able to solve it? I definately do not see your attempt posted.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Quaza my code

    #include <stdio.h>

    int main()
    {
    int cnt;
    float num,num1, num2, num3, num4, num5,nu1,nu2,nu3,nu4,nu5;
    num1 = 2.98;
    num2 = 4.50;
    num3 = 9.98;
    num4 = 4.48;
    num5 = 6.87;
    clrscr();

    printf ("Enter a product number ");
    printf ("Enter -1 to end");
    scanf ("%d", & cnt );

    while ( cnt != -1 ){

    switch ( num ){

    case '1':
    nu1+=num1;
    break;

    case '2':
    nu2+=num2;
    break;

    case '3':
    nu3+=num3;
    break;

    case '4':
    nu4+=num4;
    break;

    case '5':
    nu5+=num5;
    break;

    default:
    printf ("Incorrect product number");
    printf ("\nEnter a product number");
    break;
    } }

    printf (" The totals are: ");
    printf ("1: %f", nu1 );
    printf ("2: %f", nu2 );
    printf ("3: %f", nu3 );
    printf ("4: %f", nu4 );
    printf ("5: %f", nu5 );



    getch();
    return 0;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In the switch test, you can't use a float, which is what you have tried to do (the num var is a float).

    There are some other bits I'd normally point out, but I expect you're still developing this code, so I'll keep quiet for now

    Also, code tags please ....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Quzah my code (I did try it)

    #include <stdio.h>

    int main()
    {
    int cnt;
    float num,num1, num2, num3, num4, num5,nu1,nu2,nu3,nu4,nu5;
    num1 = 2.98;
    num2 = 4.50;
    num3 = 9.98;
    num4 = 4.48;
    num5 = 6.87;
    clrscr();

    printf ("Enter a product number ");
    printf ("Enter -1 to end");
    scanf ("%d", & cnt );

    while ( cnt != -1 ){

    switch ( num ){

    case '1':
    nu1+=num1;
    break;

    case '2':
    nu2+=num2;
    break;

    case '3':
    nu3+=num3;
    break;

    case '4':
    nu4+=num4;
    break;

    case '5':
    nu5+=num5;
    break;

    default:
    printf ("Incorrect product number");
    printf ("\nEnter a product number");
    break;
    } }

    printf (" The totals are: ");
    printf ("1: %f", nu1 );
    printf ("2: %f", nu2 );
    printf ("3: %f", nu3 );
    printf ("4: %f", nu4 );
    printf ("5: %f", nu5 );



    getch();
    return 0;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    '5' is not the same as 5.

    case '5': /* the character value of 5 */
    case 5: /* the decimal value of 5 */

    You don't want the character value of 5.
    Here is the difference:

    printf("5 is %d. '5' is %d.\n", 5, '5' );

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You've posted exactly the same code again

    Look:
    Code:
    float num 
    <snip lots>
    switch ( num ){ 
    <snip lots more>
    Now read my previous post.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    yeah guys i will try to complete the code

    Thanks for the points hammer and quzah....

  9. #9
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    what about this hammer( is it better ??)

    /*
    #include <stdio.h>

    int main()
    {
    int swt,num1, num2, num3, num4, num5;
    float awn1, awn2, awn3, awn4, awn5, total;
    clrscr();

    printf ("Enter the product number: ");
    scanf ("%d", &swt );
    while ( swt != -1 ){

    switch ( swt ){
    case 1:
    printf ("Enter the amount for No.1: ");
    scanf ("%d",&num1 );
    awn1 = num1 * 2.98;
    break;

    case 2:
    printf ("Enter the amount for No.2: ");
    scanf ("%d", &num2 );
    awn2 = num2 * 4.50;
    break;

    case 3:
    printf ("Enter the amount for No.3: ");
    scanf ("%d", &num3 );
    awn3 = num3 * 9.98;
    break;

    case 4:
    printf ("Enter the amount for No.4: ");
    scanf ("%d", &num4 );
    awn4 = num4 * 4.49;
    break;

    case 5:
    printf ("Enter the amount for No.5: ");
    scanf ("%d", &num5 );
    awn5 = num5 * 6.87;
    break;

    default:
    printf ("Incorrect number\n");
    printf ("Enter your product number:");
    break;
    } }

    total = awn1+awn2+awn3+awn4+awn5;
    printf ("\nThe total for you products is: %d",total);

    getch();
    return 0;
    }
    */

    But it gives me an error it keeps on repeting the case .Example if i enter 2 ..It will ask me for the quantity after entering the quantity it wont break????
    help me out

  10. #10
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    what happend to code tags this time? you umm forgot?
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One problem is you assign your variables 'num1' etc, a value without actually initializing them first:

    int num1;
    float answ;

    answ = num1 * 3.14157;

    Ok, so 'num1' holds what value at this time? See? It ends up that it may just have some random junk value in it first. You should initialize 'num1' to one, or whatever it is that you want, before you use it.

    Another problem may be that your buffer contains junk after your scanf, so your loop doesn't exit right.

    Actually, the problem is that your test is not set up correctly. You check for a -1 at the betinning and not the end. This could be improved upon.

    [edit]
    Actually... the reason it doesn't break is because you don't set the "breaking value". You need to set the while loop check value to "the break number" inside valid case statements.

    ACTUALLY... the best way to do it would be to modify your check, and only set the "don't break" value in the default case.
    [/edit]

    Quzah.
    Last edited by quzah; 06-28-2002 at 07:12 PM.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    hey quzah .Help me out

    Well i declared num a value 1.But i dont still understand why isnt the break stuff working..
    /*
    #include <stdio.h>

    int main()
    {
    int swt,num1=1, num2=1, num3=1, num4=1, num5=1;
    float awn1, awn2, awn3, awn4, awn5, total;
    clrscr();

    printf ("Enter the product number: ");
    scanf ("%d", &swt );
    while ( swt != -1 ){

    switch ( swt ){
    case 1:
    printf ("Enter the amount for No.1: ");
    scanf ("%d",&num1 );
    awn1 = num1 * 2.98;
    break;

    case 2:
    printf ("Enter the amount for No.2: ");
    scanf ("%d", &num2 );
    awn2 = num2 * 4.50;
    break;

    case 3:
    printf ("Enter the amount for No.3: ");
    scanf ("%d", &num3 );
    awn3 = num3 * 9.98;
    break;

    case 4:
    printf ("Enter the amount for No.4: ");
    scanf ("%d", &num4 );
    awn4 = num4 * 4.49;
    break;

    case 5:
    printf ("Enter the amount for No.5: ");
    scanf ("%d", &num5 );
    awn5 = num5 * 6.87;
    break;

    default:
    printf ("Incorrect number\n");
    printf ("Enter your product number:");
    break;
    } }

    total = awn1+awn2+awn3+awn4+awn5;
    printf ("\nThe total for you products is: %d",total);

    getch();
    return 0;
    }*/

    well the while loop is working properly .When i enter -1 the sentinel value.The loop does exit and the statment that is not in the loop is executed with is alright..When i enter a number say 2 it then printf the statment that says to enter the quantity needed after i enter the quantity instead of it should break , it repeats the whole statment again .....And ya how do you insert code tags

    help me out

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: hey quzah .Help me out

    ..When i enter a number say 2 it then printf the statment that says to enter the quantity needed after i enter the quantity instead of it should break , it repeats the whole statment again .....And ya how do you insert code tags
    This is because of the way you've coded the loop. The break statement exits the switch, not the while loop. The only point at which you can leave the while loop is when swt if -1, but you only update the value of swt BEFORE the while loop starts.

    If you want it to ask for multiple product numbers, try adding
    Code:
    printf("Enter the product number: ");
    scanf("%d", &swt);
    as the last statements within the while loop.

    Also, the following variables will need initial values
    >awn1,awn2,awn3,awn4 and awn5
    Can you see why?

    Originally post by quzah
    One problem is you assign your variables 'num1' etc, a value without actually initializing them first:

    int num1;
    float answ;

    answ = num1 * 3.14157;
    All of the num1-5 variables don't need initialising to 1. They are initialised when they are used within scanf(). The thing that is missing is validation that scanf() actually worked properly (need to test the return code).
    [EDIT] Sorry quzah, I see the original code has changed a bit compared to the latest post, and your comments were originally correct! Still, in the latest version, these variables don't need initialising.
    Last edited by Hammer; 06-29-2002 at 05:22 PM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    if you havn't yet ready hammer's sig do so now and you'll have 1 less question to worry about.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  15. #15
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    What about this is it better than the one before??

    #include <stdio.h>

    int main()
    {
    int cnt,swt,num1, num2, num3, num4, num5;
    float awn1, awn2, awn3, awn4, awn5, total;
    clrscr();

    printf ("Enter the product number: ");
    while (( swt = getchar()) != EOF ){


    switch ( swt ){

    case '1':
    printf ("Enter the amount for No.1: ");
    scanf ("%d",&num1 );
    awn1 = num1 * 2.98;
    break;

    case '2':
    printf ("Enter the amount for No.2: ");
    scanf ("%d", &num2 );
    awn2 = num2 * 4.50;
    break;

    case '3':
    printf ("Enter the amount for No.3: ");
    scanf ("%d", &num3 );
    awn3 = num3 * 9.98;
    break;

    case '4':
    printf ("Enter the amount for No.4: ");
    scanf ("%d", &num4 );
    awn4 = num4 * 4.49;
    break;

    case '5':
    printf ("Enter the amount for No.5: ");
    scanf ("%d", &num5 );
    awn5 = num5 * 6.87;
    break;

    default:
    printf ("Incorrect number\n");
    printf ("Enter your product number:");


    } }

    total = awn1+awn2+awn3+awn4+awn5;
    printf ("\nThe total for you products is: %d",total);

    getch();
    return 0;
    }


    Well it still doent work but it does seem improved ..I am stuck on this question..Why dont you right the whole code for me.....
    If i know how it really works i will be able to do the other switch questions in my book..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. easy question about switch
    By zdzislavv in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2008, 09:20 AM
  3. Replies: 7
    Last Post: 05-25-2006, 12:51 PM
  4. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM