# Why is the sum is not being calculated correctly?

• (2 Pages)
• 1
• 2

## 16 Replies - 1679 Views - Last Post: 22 January 2012 - 09:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=263861&amp;s=84aad36afce430b7b162d34f8796ab52&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 RomoneMc

Reputation: 0
• 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

• D.I.C Lover

Reputation: 2579
• Posts: 4,114
• 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.

### #3 RomoneMc

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

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

Posted 22 January 2012 - 06:01 PM

sepp2k, 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

sepp2k, 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

### #4 jimblumberg

Reputation: 5447
• Posts: 16,972
• 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

### #5 RomoneMc

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

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

Posted 22 January 2012 - 06:08 PM

jimblumberg, 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.

### #6 jimblumberg

Reputation: 5447
• Posts: 16,972
• 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

### #7 RomoneMc

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

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

Posted 22 January 2012 - 06:37 PM

jimblumberg, 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

any other suggestions.

### #8 jimblumberg

Reputation: 5447
• Posts: 16,972
• Joined: 25-December 09

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

Posted 22 January 2012 - 07:18 PM

Jim

### #9 RomoneMc

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

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

Posted 22 January 2012 - 07:24 PM

jimblumberg, on 22 January 2012 - 07:18 PM, said:

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

```

### #10 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• 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.

### #11 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• 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);

```

### #12 RomoneMc

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

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

Posted 22 January 2012 - 08:19 PM

jjl, 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.

### #13 jimblumberg

Reputation: 5447
• Posts: 16,972
• Joined: 25-December 09

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

Posted 22 January 2012 - 08:26 PM

Jim

### #14 RomoneMc

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

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

Posted 22 January 2012 - 09:00 PM

jimblumberg, on 22 January 2012 - 08:26 PM, said:

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;

}

```

RomoneMc, on 22 January 2012 - 08:58 PM, said:

jimblumberg, on 22 January 2012 - 08:26 PM, said:

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;

}

```

### #15 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• 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);

```