# isdigit calculation

Page 1 of 1

## 5 Replies - 4090 Views - Last Post: 12 April 2007 - 10:03 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26428&amp;s=74c897a288f85c9b68d7eb0b29c78292&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ironhorse93

Reputation: 0
• Posts: 9
• Joined: 12-April 07

# isdigit calculation

Posted 12 April 2007 - 08:47 AM

I'm trying to calculate the total tax amount and total purchase with validating the user input. So far if you enter a letter the right answer comes out however, when I input a number it shows what it's should show but does not calculate the right amount. How can I make it to where it does the right calculation using isdigit. Thanks.

```#include <stdio.h>
#include <ctype.h>

main()
{

int iPurchase;
float fDel_Mar;

iPurchase='\0';
fDel_Mar=.0725;

/* total=(purchase*taxrate)+purchase */

printf("\n\t\t\t  Kudler Store Tax Rates\n\t\t\t  ----------------------\n");

printf("\nPurchase is \$");
scanf("%c", &iPurchase);

if (isdigit(iPurchase))
{
printf("\nDel Mar tax rate is 7.25%% ");
printf("\nTotal tax amount is \$%.2f\n", iPurchase * fDel_Mar);
printf("The Total for the Del Mar Store is \$%.2f\n", (iPurchase * fDel_Mar) + iPurchase);
}
else
printf("\nYou did not enter a digit\n");

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: isdigit calculation

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: isdigit calculation

Posted 12 April 2007 - 08:54 AM

isdigit() does not calculate anything...it returns a zero if if the provided parameter is not between 0 and 9, a non zero otherwise.

you have declared iPurchase as an integer, but are assigning it the value of a string terminator.

what exactly are you trying to accomplish? Can you provide an example using sample input?

### #3 ironhorse93

Reputation: 0
• Posts: 9
• Joined: 12-April 07

## Re: isdigit calculation

Posted 12 April 2007 - 09:02 AM

Amadeus, on 12 Apr, 2007 - 08:54 AM, said:

what exactly are you trying to accomplish? Can you provide an example using sample input?

Okay. If you input a letter the response is "You did not enter a digit". Now if you input an dollar amount then it should calculate for each of the stoes the total tax and the total amount.
Ex: (Good input)
Purchase is \$150.00

Del Mar tax rate is 7.25%
Total tax amount is \$10.88
The total for the Del Mar Store is \$160.88

Purchase is \$t

You did not enter a digit

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: isdigit calculation

Posted 12 April 2007 - 09:14 AM

You are taking the input as a character, and trying to place it in an integer variable. The two types are not compatible, especially if you plan to perform mathematical operation on them. Furthermore, if you are expecting any decimal entries, you'll need to use a float or double variable, not an integer. If you really want to take the input from the user as a set of characters as opposed to a number, you'll have to convert it to a number before performing the operations.

### #5 ironhorse93

Reputation: 0
• Posts: 9
• Joined: 12-April 07

## Re: isdigit calculation

Posted 12 April 2007 - 09:44 AM

Amadeus, on 12 Apr, 2007 - 09:14 AM, said:

You are taking the input as a character, and trying to place it in an integer variable. The two types are not compatible, especially if you plan to perform mathematical operation on them. Furthermore, if you are expecting any decimal entries, you'll need to use a float or double variable, not an integer. If you really want to take the input from the user as a set of characters as opposed to a number, you'll have to convert it to a number before performing the operations.

I want the input from the user to be numbers not characters.

So with what your saying I should try a different path than the isdigit function? Thanks for your help on this, as you can tell I'm pretty new with this.

### #6 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: isdigit calculation

Posted 12 April 2007 - 10:03 AM

Well you can have the user input a char cIn, then if isDigit(cIn) is true, you can convert cIn to an integer with value = cIn - '0';

You could also just input an integer iIn. If you only wanted a 1 digit integer, you could make sure than iIn >=0 and iIn <= 9.