Why is the sum is not being calculated correctly?

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1188 Views - Last Post: 22 January 2012 - 09:24 PM Rate Topic: -----

#1 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 05:52 PM

The code should prompt the user for a price and a pricing code either H, F, Q, Z or T and then it should times the price by the discount rate based on the pricing code selected.
After entering the price as 90 and the pricing code as H the sum that is being calculated is 0.00 and after changing it around a little it comes out as 1.Ef#, the compiler being used is codeblocks 10.05.




#include <stdio.h>
int main()

{
    float price,sum,discount_rate;
    char pricing_code;

    printf("Enter the price for the article:\n");
    scanf("%f", &price);

    printf("Enter the pricing code for the article:\n");
    scanf("%o", &pricing_code);

    if (pricing_code == 'H')
    {
        sum = (discount_rate = 0.5);

    }
    else if (pricing_code == 'F')
    {
        (discount_rate = 0.6);
    }
    else if (pricing_code == 'T')
    {
        (discount_rate = 0.77);
    }
    else if (pricing_code == 'Q')
    {
        (discount_rate = 75/100);
    }
    else if (pricing_code == 'Z')
    {
        printf("No discount /n");
    }

    sum = price * discount_rate;

    printf("The sum is %.2f\n", sum);

  return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is the sum is not being calculated correctly?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 05:56 PM

scanf("%o", &pricing_code);


I'm pretty sure you meant %c there. %o reads an octal number and expects a pointer to int, not pointer to char.
Was This Post Helpful? 1
  • +
  • -

#3 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 06:01 PM

View Postsepp2k, on 22 January 2012 - 05:56 PM, said:

scanf("%o", &pricing_code);


I'm pretty sure you meant %c there. %o reads an octal number and expects a pointer to int, not pointer to char.



Tanks you for the reply, i tried that however after changing it, it now stops me from entering a pricing code
it just skips to displaying an incorrect sum

View Postsepp2k, on 22 January 2012 - 05:56 PM, said:

scanf("%o", &pricing_code);


I'm pretty sure you meant %c there. %o reads an octal number and expects a pointer to int, not pointer to char.



Tanks you for the reply, i tried that however after changing it, it now stops me from entering a pricing code
it just skips to displaying an incorrect sum
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 06:04 PM

Also be careful when doing math with constants. In the following snippet:
    else if (pricing_code == 'Q')
    {
        (discount_rate = 75/100);
    }

You are doing integer math. Which means any fractional part is truncated. This calculation will not evaluate to .75 it will evaluate to zero. To force the use of floating point math at least one of the constants must be a floating point number.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 06:08 PM

View Postjimblumberg, on 22 January 2012 - 06:04 PM, said:

Also be careful when doing math with constants. In the following snippet:
    else if (pricing_code == 'Q')
    {
        (discount_rate = 75/100);
    }

You are doing integer math. Which means any fractional part is truncated. This calculation will not evaluate to .75 it will evaluate to zero. To force the use of floating point math at least one of the constants must be a floating point number.

Jim


Therefore I should change the 75/100 to 0.75 but the discount_rate for H is 0.5 shouldn't it work if I have that.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 06:31 PM

Quote

Therefore I should change the 75/100 to 0.75 but the discount_rate for H is 0.5 shouldn't it work if I have that

I don't understand your question. I was pointing out that 75/100 is equal to zero. So replacing that with .75 will work in that situation. You could also replace one of the constants with a floating point constant 75.0/100 or 75/100.0 or 75.0/100.0.

Jim
Was This Post Helpful? 2
  • +
  • -

#7 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 06:37 PM

View Postjimblumberg, on 22 January 2012 - 06:31 PM, said:

Quote

Therefore I should change the 75/100 to 0.75 but the discount_rate for H is 0.5 shouldn't it work if I have that

I don't understand your question. I was pointing out that 75/100 is equal to zero. So replacing that with .75 will work in that situation. You could also replace one of the constants with a floating point constant 75.0/100 or 75/100.0 or 75.0/100.0.

Jim




I tried your suggestion, it printed your sum is 1.#J,
any other suggestions.
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 07:18 PM

Post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#9 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 07:24 PM

View Postjimblumberg, on 22 January 2012 - 07:18 PM, said:

Post your current code.

Jim


Okay

#include <stdio.h>
int main()

{
    float price,sum,discount_rate;
    char pricing_code;

    printf("Enter the price for the article:\n");
    scanf("%f", &price);

    printf("Enter the pricing code for the article:\n");
    scanf("%o", &pricing_code);

    if (pricing_code == 'H')
    {
        (discount_rate = 50.0/100.0);

    }
    else if (pricing_code == 'F')
    {
        (discount_rate = 0.6);
    }
    else if (pricing_code == 'T')
    {
        (discount_rate = 0.77);
    }
    else if (pricing_code == 'Q')
    {
        (discount_rate = 0.75);
    }
    else if (pricing_code == 'Z')
    {
        printf("No discount /n");
    }

    sum = price * discount_rate;

    printf("The sum is %.2f\n", sum);

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 08:06 PM

You might be having issues with left over charcters in the input buffer, try flushing the input stream before getting your price_code

example
    printf("Enter the price for the article:");
    scanf("%f", &price);

	while (getchar() != '\n');
    printf("Enter the pricing code for the article:\n");
    scanf("%c", &pricing_code);



On another node, what happens when a user does not input an appropriate price_code? You might want to think about that before doing the calculation.
Was This Post Helpful? 2
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 08:18 PM

Yes, the first scanf leaves a newline in the input stream. You could use a space at the beginning of the second scanf to remove whitespace.

    printf("Enter the price for the article: ");
    scanf("%f", &price);

    printf("Enter the pricing code for the article: ");
    scanf(" %c", &pricing_code);


Was This Post Helpful? 2
  • +
  • -

#12 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 08:19 PM

View Postjjl, on 22 January 2012 - 08:06 PM, said:

You might be having issues with left over charcters in the input buffer, try flushing the input stream before getting your price_code

example
    printf("Enter the price for the article:");
    scanf("%f", &price);

	while (getchar() != '\n');
    printf("Enter the pricing code for the article:\n");
    scanf("%c", &pricing_code);



On another node, what happens when a user does not input an appropriate price_code? You might want to think about that before doing the calculation.




thank you for the reply I tried your first suggestion by inserting the code btw I am really new to C so I have no idea what you meant anyways it did not make any difference unfortunateky it still printed the same 1.#J.
I then opted to input an incorrect pricing code and it still gave me the same 1.#J. I am about to give up on trying to find the solution.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 08:26 PM

Show your current code, changing the scanf() specifier for your pricing code should fix your problem.

Jim
Was This Post Helpful? 1
  • +
  • -

#14 RomoneMc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 22-January 12

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 09:00 PM

View Postjimblumberg, on 22 January 2012 - 08:26 PM, said:

Show your current code, changing the scanf() specifier for your pricing code should fix your problem.

Jim


If by scanf specifier you mean %o then i have changed it to %c which prevents me from entering the pricing_code therefore i have changed it back to %o

#include <stdio.h>

int main()

{

    float price,sum,discount_rate;

    char pricing_code;

    printf("Enter the price for the article:\n");

    scanf("%f", &price);

    printf("Enter the pricing code for the article:\n");

    scanf("%o", &pricing_code);


    if (pricing_code == 'H')

    {

        sum = (discount_rate = 0.5);

 

    }

    else if (pricing_code == 'F')

    {

        (discount_rate = 0.6);

    }

    else if (pricing_code == 'T')

    {

        (discount_rate = 0.77);

    }

    else if (pricing_code == 'Q')

    {

        (discount_rate = 75/100);

    }

    else if (pricing_code == 'Z')

    {

        printf("No discount /n");

    }

    sum = price * discount_rate;

    printf("The sum is %.2f\n", sum);

  return 0;

}



View PostRomoneMc, on 22 January 2012 - 08:58 PM, said:

View Postjimblumberg, on 22 January 2012 - 08:26 PM, said:

Show your current code, changing the scanf() specifier for your pricing code should fix your problem.

Jim


If by scanf specifier you mean %o then i have changed it to %c which prevents me from entering the pricing_code therefore i have changed it back to %o

#include <stdio.h>

int main()

{

    float price,sum,discount_rate;

    char pricing_code;

    printf("Enter the price for the article:\n");

    scanf("%f", &price);

    printf("Enter the pricing code for the article:\n");

     scanf("%o", &pricing_code);


    if (pricing_code == 'H')

    {

        (discount_rate = 50.0/100.0);

 

    }

    else if (pricing_code == 'F')

    {

        (discount_rate = 0.6);

    }

    else if (pricing_code == 'T')

    {

        (discount_rate = 0.77);

    }

    else if (pricing_code == 'Q')

    {

        (discount_rate = 75/100);

    }

    else if (pricing_code == 'Z')

    {

        printf("No discount /n");

    }

    sum = price * discount_rate;

    printf("The sum is %.2f\n", sum);

  return 0;

}


Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Why is the sum is not being calculated correctly?

Posted 22 January 2012 - 09:08 PM

Try and use a space before the %c.

scanf(" %c", &pricing_code);


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2