• 0

[C] "isdigit" validation problems


Question

Hello! I have a small program which calculates tax. I've got all the requirements down except for one which is racking my brain. I tried using "isdigit" to validate the user input for the menu so entering a letter doesn't make it go nuts but I can't get it to work. I have a "while" loop validating the entry to make sure it's within the range of possible options (1-4), just need to weed out the letters. The code is posted below. Any help would be greatly appreciated!

/******************************************************************
*  Source File Name: Tax Calculator.c				  *
*  Date Final Submmitted: 3/9/09				  *
*								  *
*  Description: 						  *
*  This program takes values assigned to variables and calculates *
*  the tax and for each of the Kudler Fine Foods locales	  *
*  Initial program has all of the values hard coded.		  *
*-----------------------------------------------------------------*
*  Date	Modified by   Description of Change			  *
*  ------  ------------  -----------------------------------------*
*  022309  Change Request #1 to prompt the user for *
*		  the grocery amount, and calculate and display total	  *
*		  sales amount.					  *
*								  *
*  030209  Change Request #2 to add menu selection  *
*		  for Location and display only the tax and total for	  *
*		  that location.					  *
*******************************************************************/

/******************************************************************
*																 *
*  Here are the INCLUDES that are needed						  *
*																 *
*******************************************************************/

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

/******************************************************************
*																 *
*  Here are the Function Prototypes that are needed			   *
*																 *
*******************************************************************/

void getfAmount();
void printTableHeader();
void printLocMenu();
float calcSalesTax(float, float);
float calcTotal(float, float);

/******************************************************************
*  Float variables:			 						*
*  fAmount is the amount of the groceries		 		  *
*  fDelMarTax is the tax rate for DelMar					*
*  fEncinitasTax is the tax rate for Encinitas			  *
*  fLaJollaTax is the tax rate for La Jolla				*
*******************************************************************/

float fAmount=0.00;
int iSelection=0;
int iValidate=0;
float fDelMarTax=7.25;
float fEncinitasTax=7.50;
float fLaJollaTax=7.75;
float fDelTaxTotal=0.00;
float fEncTaxTotal=0.00;
float fLaJTaxTotal=0.00;

main() //Start main function
{

   printf("\t\tKudler Fine Foods\n");
   printf("\t\tTax Calculator\n\n");

   //Location selection menu
   printf("\nLocation Menu\n");
   printf("\n1\tDel Mar");
   printf("\n2\tEncinitas");
   printf("\n3\tLa Jolla");
   printf("\n4\tQuit\n");
   printf("\n\nEnter your selection (1-4): ");
   scanf("%d", &iSelection);	 

//Need verification to weed out letters using isdigit if possible

	  while (iSelection < 1 || iSelection > 4) {   
		 printf("\nPlease enter a valid number (1-4): ");
		 scanf("%d", &iSelection);
	  }

	  if (iSelection == 1) {
		 getfAmount();
		 fDelTaxTotal=calcSalesTax(fAmount, 7.25);
		 printTableHeader();
		 printf("\n\nDel Mar\t\t%.2f\t\t$%.2f\t\t%.2f\n", fDelMarTax, fDelTaxTotal, calcTotal(fAmount, fDelTaxTotal));
	  }

	  if (iSelection == 2) {
		 getfAmount();
		 fEncTaxTotal=calcSalesTax(fAmount, 7.5);
		 printTableHeader();
		 printf("\nEncinitas\t%.2f\t\t$%.2f\t\t%.2f\n", fEncinitasTax, fEncTaxTotal, calcTotal(fAmount, fEncTaxTotal));
	  }

	  if (iSelection == 3) {
		 getfAmount();
		 fLaJTaxTotal=calcSalesTax(fAmount, 7.75);
		 printTableHeader();
		 printf("\nLa Jolla\t%.2f\t\t$%.2f\t\t%.2f\n", fLaJollaTax, fLaJTaxTotal, calcTotal(fAmount, fLaJTaxTotal));
	  }

	  if (iSelection == 4) {
		 printf("\n\n\tThank You.");
	  }

} //End main function

/******************************************************************
*																 *
*  Here are the Function Definitions that are needed			  *
*																 *
*******************************************************************/

void getfAmount()
{

   printf("\n\nEnter the purchase amount: $");
   scanf("%f", &fAmount);

   //While loop for positive number verification
   while ( fAmount <= 0 ) {
	  printf("\nInvalid purchase amount.");
	  printf("\nPlease enter a valid purchase amount: $");
	  scanf("%f", &fAmount);
   } //End while loop

} //End function definition

void printTableHeader()
{

   printf("\n\n\nLocation\tSales Tax\tTax Amount\tTotal Price\n");

} //End function definition

float calcSalesTax(float fAmount, float locTax)
{

   return (fAmount*locTax)/100;

} //End function definition

float calcTotal(float fAmount, float locTotalTax)
{

   return (fAmount+locTotalTax);

} //End function definition

Link to comment
Share on other sites

Recommended Posts

  • 0
I think it's the compiler that's different. It compiles just fine for me as I posted it without the int before main, etc. I hand edited my code to incorporate the fixes you came up with and it works perfectly. :D

Thanks a million!!!!

You're very welcome :) Glad it's now working how you want it!

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.