Thread: Help please, simple program doesn't crunch numbers properly

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Help please, simple program doesn't crunch numbers properly

    I'm having trouble getting total to solve properly. Something seems to be messing up the value of price after it leaves the switch structure. Please forgive all the test code.


    Code:
    #include <stdio.h>
    
    int main()
       {
    
       int sentinel = 0, product = 0, quantity;
       float total = 0, price = 0;
    
    
       while (sentinel != 1)
          {
          printf("Enter product number or 6 to exit: ");
          scanf("%d", &product);
    
    
          switch (product)
             {
             case 1:
                price = 2.98;
    /* ---------Test code----------- */
                printf("price: %.2f\n", price);
                printf("product: %d\n", product);
    /* ---------End of test-----------*/
                break;
    
             case 2:
                price = 4.50;
                break;
    
             case 3:
                price = 9.98;
                break;
    
             case 4:
                price = 4.49;
                break;
    
             case 5:
                price = 6.87;
                break;
    
             case 6:
                sentinel = 1;
                price = 0;
                break;
    
             default:
                printf("Enter 1 through 6 only\n");
                break;
             }
    
    /* -----------test code------------------------------------------------ */
       printf("-----values after switch-----\n");
       printf("%price: .2f\n", price);
       printf("product: %d\n", product);
    /* ----------------End of test ----------------------------------*/
    
       total = ( float ) quantity * price + total;      
    
       if (product >= 6)
          break;
    
    /* Moved to bottom so you can exit without this interfering */
    
       printf("Enter quantity: ");
       scanf("%d", &quantity);
    
    /* -----------------test code----------------- */
       printf("quantity: %d\n", quantity);
    /* -----------------End of test--------------- */
    
    
          }
    
       printf("The total value is: %.2f\n", total);
    
    /* -----------------test code----------------- */
    
       printf("quantity: %d", quantity);
       printf("\nprice: %.2f\n", price);
    
    /* end of test code */
    
       return 0;
       }

    It's been a while since I've last picked up my C book at least half a year or so. I'm thinking maybe it's something I've forgotten, but I could have sworn C doesn't use local and global variables.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It does use both local and global. In fact, you can even do this:
    Code:
    int x; /* global */
    int main( void )
    {
        int x; /* local */
        ...
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Very simple, quantity is used uninitialised - its causing problems
    with this line:

    total = ( float ) quantity * price + total;

    notice how you scan for a value for quantity after this line?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Your test code has a little problem too - see if you can find it:

    Code:
    printf("%price: .2f\n", price);
    ~/

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    182
    Thanks for the help!
    Working flawlessly

    I don't know how I could have even considered C to not have local and global, but I guess that's what happens when you don't practice for a long time.

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Guess you're not answering kermits question, are you?
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    182
    I didn't realize kermit was asking a question.

    But thank you very much, fixing that test error cleared up alot of conusion.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C program displaying all numbers divisible by 6
    By DaniiChris in forum C Programming
    Replies: 25
    Last Post: 07-06-2008, 12:25 PM
  2. Perfect Numbers C Program
    By SlayerBlade in forum C Programming
    Replies: 2
    Last Post: 08-28-2005, 05:11 PM
  3. Simple Math Program
    By RazielX in forum C Programming
    Replies: 3
    Last Post: 03-04-2004, 06:22 PM
  4. simple program 2
    By Gav D in forum C Programming
    Replies: 7
    Last Post: 08-06-2003, 01:12 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM