isdigit calculation

need to know how to calculate using isdigit

Page 1 of 1

5 Replies - 3516 Views - Last Post: 12 April 2007 - 10:03 AM Rate Topic: -----

#1 ironhorse93  Icon User is offline

  • New D.I.C Head

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

isdigit calculation

Post icon  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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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?
Was This Post Helpful? 0
  • +
  • -

#3 ironhorse93  Icon User is offline

  • New D.I.C Head

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

Re: isdigit calculation

Posted 12 April 2007 - 09:02 AM

View PostAmadeus, 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
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 ironhorse93  Icon User is offline

  • New D.I.C Head

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

Re: isdigit calculation

Posted 12 April 2007 - 09:44 AM

View PostAmadeus, 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.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1