#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; }
5 Replies  3473 Views  Last Post: 12 April 2007  10:03 AM
#1
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.
Replies To: isdigit calculation
#2
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?
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
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
Ex: (Bad input)
Purchase is $t
You did not enter a digit
#4
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
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
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.
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.
Page 1 of 1
