Thread: problem with if-else math

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    1

    problem with if-else math

    i am having problems getting my math to work in the if-else statements. i am getting no errors when i run the program but the math for everything past my first if statement shows outrageous numbers in the printf function.


    Code:
     #include <stdio.h>
     int main()
    {
    
      float total;
      int hotdogs, hamburgers, salad, pickles;
      float hotdogval, hamburgerval, saladval, pickleval;
      hotdogval=1.00, hamburgerval=0.50, saladval=0.25, pickleval=0.01;
      int picnic()
      {
         while(total != 0)
         {
             if (total>=1.00)
             {
                hotdogs=total*hotdogval;
                total=total-hotdogs;
                break;
             }
             else if(total>=0.50||total<1.00)
             {
                total=total-hotdogs;
                hamburgers=total*hamburgerval;
                total=total-hamburgers;
                break;
             }
             else if(total>=0.25||total<0.50)
             {
                 total=total-hamburgers;
                 salad=total*saladval;
                 total=total-salad;
                 break;
             }
             else
             {
                 total=total-salad;
                 pickles=total*pickleval;
                 break;
             }
         };
      };
      printf("Enter the amount of money to spend:");
      scanf("%f", &total);
      picnic(total);
      printf("\nthe number of hotdogs you have is %d", hotdogs);
      printf("\nthe number of hamburgers you have is %d", hamburgers);
      printf("\nthe number of salads you have is %d", salad);
      printf("\nthe number of pickles you have is %d", pickles);
    return 0;
    };

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You have not specified any thing to be passed into your function, yet in main you pass it a variable.

    Code:
    int picnic(float total)
    That is how your function implementation should be, however these changes with total will not reflect back to main unless you return the variable. How about putting the print statement within the function itself?

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Given that C does not allow functions to be implemented inside function bodies, your code is not valid C.

    That aside, look at the first "else if" clause
    Code:
         else if(total>=0.50||total<1.00)
    No value exists for which this condition is false. Remember || means OR, not AND.

    Because of this, the second "else if" and the "else" will NEVER be exercised.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math Problem
    By kanuvas in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2007, 11:13 AM
  2. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  3. math problem
    By unixOZ in forum Linux Programming
    Replies: 4
    Last Post: 10-19-2002, 12:17 AM
  4. Please help me with a math problem
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-07-2002, 01:42 PM
  5. Little math problem
    By Thantos in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-27-2001, 07:44 PM