Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,148 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,063 people online right now. Registration is fast and FREE... Join Now!




isdigit calculation

 
Reply to this topicStart new topic

isdigit calculation, need to know how to calculate using isdigit

ironhorse93
12 Apr, 2007 - 07:47 AM
Post #1

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 9


My Contributions
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.

CODE

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

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Isdigit Calculation
12 Apr, 2007 - 07:54 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

ironhorse93
RE: Isdigit Calculation
12 Apr, 2007 - 08:02 AM
Post #3

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 9


My Contributions
QUOTE(Amadeus @ 12 Apr, 2007 - 08:54 AM) *


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

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Isdigit Calculation
12 Apr, 2007 - 08:14 AM
Post #4

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

ironhorse93
RE: Isdigit Calculation
12 Apr, 2007 - 08:44 AM
Post #5

New D.I.C Head
*

Joined: 12 Apr, 2007
Posts: 9


My Contributions
QUOTE(Amadeus @ 12 Apr, 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.


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.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Isdigit Calculation
12 Apr, 2007 - 09:03 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month