• 0

[C] trouble with "isdigit" validity check


Question

Hey im hoping to get some insight to this.

Using Miracle C compiler.

The program is supposed to receive purchase amount input, check validity, then calculate and display taxes and total at 3 locations.

The problem is I cant get it to compile when I trying to check input as being a digit. The program works fine otherwise, but I need this additional check or when a non-digit character is entered the program returns taxes and totals as 0.00.

if isdigit(fPurchase) { //this gives me a compile error expecting "(".

which seems that Miracle C wants me to change it to:

if (isdigit(fPurchase)) { //But that gives me: (null) parse error, expecting 'WHILE' ' '

which I do not understand.

Also, I may not be correct with the following 2 statements either, but if I can get it to compile the 'if' condition then I can probably figure that out.

Here's my code, all tips or corrections are appreciated greatly!!!

//HEADER FILES

#include <stdio.h>

#include <ctype.h>

//GLOBAL VARIABLES

#define DM_TaxRate 0.0725

#define E_TaxRate 0.075

#define LJ_TaxRate 0.0775

//MAIN FUNCTION

main()

{

//LOCAL VARIABLES

float fPurchase = 0.0; //input variable

float fPurchConfirm = 0.0; //input variable for validity check

do { //start do while loop

printf("\nPlease enter the purchase amount: $ "); //request input

scanf("%f", &fPurchase); //store input

printf("\nPlease confirm the purchase amount: $ "); //request input again to check validity

scanf("%f", &fPurchConfirm); //store input

if (isdigit(fPurchase)) {

continue;

printf("\nYou have entered a letter, please try again.");

}

while(fPurchase != fPurchConfirm); //checks validity

while(fPurchase == fPurchConfirm) { //condition

printf("\nThe tax at Del Mar is $%.2f.", fPurchase*DM_TaxRate); //calculates and displays Del Mar tax

printf("\nThe total at Del Mar is $%.2f.", fPurchase*DM_TaxRate+fPurchase); //calculate and display Del Mar total

printf("\n\nThe tax at Encinitas is $%.2f.", fPurchase*E_TaxRate); //calculate and display Encinitas tax

printf("\nThe total at Encinitas is $%.2f.", fPurchase*E_TaxRate+fPurchase); //calculate and display Encinitas total

printf("\n\nThe tax at La Jolla is $%.2f.", fPurchase*LJ_TaxRate); //calculate and display La Jolla tax

printf("\nThe total at La Jolla is $%.2f.", fPurchase*LJ_TaxRate+fPurchase); //calculate and display La Jolla total

break; //stops loop

}

getchar(); //leaves program open for review

} //end of main function

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

if (isdigit(fPurchase)) {

continue;

printf("\nYou have entered a letter, please try again.");

}

whats with the continue being there.

isdigit returns 0 if its not a digit, so that printf seems out of place.

Even if the logic was correct, continue would just skip the printf and jump to the conditional statement.

other errors include:

main needs a type, i.e. int main(){..}

your missing a close bracket for the main

while(fPurchase == fPurchConfirm) { //condition

...

break; //stops loop

}

this might as well be an if statement if you are going to place a break.

theres probably more errors that i've missed!

Link to comment
Share on other sites

  • 0

The function isdigit() takes a character in argument and returns true (that is, a non-zero integer) if that character represents a number ('1', '2', etc), false if not. You cannot pass it a float as you are doing now.

When you input fPurchase, you are already using a float and specifying float in the scanf call by using "%f". By doing this, you are assuming the user will input a float. There is no point doing any validation afterwards; if the user didn't input a float, scanf failed!. If you want to validate the input, take it as a string of characters, then check if those characters represent a float, then if so, convert the string to the appropriate number :

1. Take input as string of chars using fgets()

2. Validate that it's a number (using isdigit() on each character, for instance)

3. Convert it to a float (using sscanf for instance).

Edited by Dr_Asik
Link to comment
Share on other sites

  • 0

Thanks guys for your responses, it was very helpful.

I've made some corrections and I don't have it working yet, but your suggestions definitely made me see where I was going wrong.

Thanks again!!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.