Tax Calculator question

looping program back to enter a new number after error is detected

Page 1 of 1

10 Replies - 1161 Views - Last Post: 31 October 2009 - 12:57 PM Rate Topic: -----

#1 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Tax Calculator question

Posted 30 October 2009 - 05:39 PM

The code below works, however, i want it to do something else and do not know how to do it. When a user enters a numeric amount, the code gives the tax for all 3 locations and enables user to exit program; however, if the user enters and letter into the purchase amount the program shows the error message and enables user to exit program. What I want the program to do is when the user enters a letter, I want the program to show the error message and allow the user to re-enter a numeric amount and hit enter to determine the tax on the numeric amount before allowing the user to exit the program. I have tried for 3 days in vain to figure this out, any help would be appreciated. Honestly, the program meets the requirements but i want it to loop back once the user enters a numeric amount, so that the user can re-enter a numeric number and obtain the tax at all 3 locations. Any help would be appreciated, thanks.



#include <stdio.h> // Include standard I/O libraries
#include <stdlib.h> // Include standard libraries
#include <math.h> // Include Math libraries

/* Defining tax rates for Kudler's three store */
	float DelMarRate = 7.25;	/* Del Mar tax rate */
	float EncinitasRate = 7.5;	/* Encintas tax rate */
	float LaJollaRate = 7.75;	/* LaJolla tax rate */
	
/* Variables for Kudler's three store's tax calculation */   
	float DelMarTax = 0.0;		/* Calculation variable for Del Mar taxes */
	float EncinitasTax = 0.0;	  /* Calculation variable for Encintas taxes */
	float LaJollaTax = 0.0;	   /* Calculation variable for LaJolla taxes */
	float Amount = 0.0;

main()
{
char Purchase[32]; /* Declare variables */

int i,Return=0; 

	/* Prompt user for Purchase Amount */
	printf("\n*********************************");
	printf("\n* Welcome to Kudler Fine foods! *");
	printf("\n*********************************");
	printf("\n\n");
	printf("\n\nPlease Enter the customer's purchase amount:$");
	scanf("%s", Purchase);

		/* Loop - Test the numeric values*/ 
		for(i=0; Purchase[i]; i++) 
		{
			if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
			{
			if (Purchase[i] == '.') /* If character is a decimal continue */
			continue;
				else
				Return=1; /* Set Non-Numerical Value flag */
				break; 
			} 
		}

			if (Return == 1) /* If Non-Numerical Value print Error Message */
			{
				printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
				fflush(stdin);
				printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
				scanf("%c\n");  /*Keeping DOS Screen open so user can view output*/
				return 0;
				}


Amount=atof(Purchase); /* Convert to Float */ 
DelMarTax=(Amount*DelMarRate)/100; /* Calculate Tax Amount for Del Mar */
EncinitasTax=(Amount*EncinitasRate)/100; /* Calculate Tax Amount for Encinitas */
LaJollaTax=(Amount*LaJollaRate)/100; /* Calculate Tax Amount for La Jolla */

	/* Display Tax Calculation output to the screen */
 	printf("\n\n\n");
 	printf("Del Mar\t\tEncinitas\tLa Jolla\t\n");
 	printf("*******************************************\n");
 	printf("$%.2f\t\t$%.2f\t\t$%.2f\n", DelMarTax, EncinitasTax, LaJollaTax);
 	printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
	fflush(stdin);
	
	/*Keeping DOS Screen open so user can view output*/
		scanf("%c\n");	
	
		return 0;
	
}




Is This A Good Question/Topic? 0
  • +

Replies To: Tax Calculator question

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Tax Calculator question

Posted 30 October 2009 - 07:05 PM

implement a do-while statement. Or use a goto statement. Now mind you, a lot of people will tell you not to use a goto statement. But there is nothing wrong with them, just make sure you comment them so people know it is there and what it is used for.
Was This Post Helpful? 0
  • +
  • -

#3 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Tax Calculator question

Posted 30 October 2009 - 08:19 PM

Now i have the program looping back for the first wrong input; however, I do not know how to make it keep looping back to allow the user to enter a numberic value more than once. What I want the program to do is if the user enters a letter instead of a numeric value several times, I want it to show the error message each time and ask the user each time to re-enter the customer's purchase amount. I am new to C programming and really want this to work this way, any help would be appreciated, thanks. The edited code is below:


#include <stdio.h> // Include standard I/O libraries
#include <stdlib.h> // Include standard libraries
#include <math.h> // Include Math libraries

/* Defining tax rates for Kudler's three store */
	float DelMarRate = 7.25;	/* Del Mar tax rate */
	float EncinitasRate = 7.5;	/* Encintas tax rate */
	float LaJollaRate = 7.75;	/* LaJolla tax rate */
	
/* Variables for Kudler's three store's tax calculation */   
	float DelMarTax = 0.0;		/* Calculation variable for Del Mar taxes */
	float EncinitasTax = 0.0;	  /* Calculation variable for Encintas taxes */
	float LaJollaTax = 0.0;	   /* Calculation variable for LaJolla taxes */
	float Amount = 0.0;

int main(void)
{
char Purchase[32]; /* Declare variables */

int i,Return=0; 

	/* Prompt user for Purchase Amount */
	printf("\n*********************************");
	printf("\n* Welcome to Kudler Fine foods! *");
	printf("\n*********************************");
	printf("\n\n");
	printf("\n\nPlease Enter the customer's purchase amount:$");
	scanf("%s", Purchase);

		/* Loop - Test the numeric values*/ 
		for(i=0; Purchase[i]; i++) 
		{
			if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
			{
			if (Purchase[i] == '.') /* If character is a decimal continue */
			continue;
				else
				Return=1; /* Set Non-Numerical Value flag */
				break; 
			} 
		}

			if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
			{
				printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
				fflush(stdin);
				printf("\n\nPlease Re-enter Purchase Amount:$");
				scanf("%s", Purchase);  /*Keeping DOS Screen open so user can view output*/				
			}


Amount=atof(Purchase); /* Convert to Float */ 
DelMarTax=(Amount*DelMarRate)/100; /* Calculate Tax Amount for Del Mar */
EncinitasTax=(Amount*EncinitasRate)/100; /* Calculate Tax Amount for Encinitas */
LaJollaTax=(Amount*LaJollaRate)/100; /* Calculate Tax Amount for La Jolla */

	/* Display Tax Calculation output to the screen */
 	printf("\n\n\n");
 	printf("Del Mar\t\tEncinitas\tLa Jolla\t\n");
 	printf("*******************************************\n");
 	printf("$%.2f\t\t$%.2f\t\t$%.2f\n", DelMarTax, EncinitasTax, LaJollaTax);
 	printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
	fflush(stdin);
	
	/*Keeping DOS Screen open so user can view output*/
		scanf("%c\n");	
	
		return 0;
	
}



Was This Post Helpful? 0
  • +
  • -

#4 jbeme  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 93
  • Joined: 30-October 09

Re: Tax Calculator question

Posted 30 October 2009 - 10:02 PM

Look up Do...While statements and also For statements.
Was This Post Helpful? 0
  • +
  • -

#5 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Tax Calculator question

Posted 30 October 2009 - 10:08 PM

I have looked up do while statements and for statements an am totally lost on where to add these conditions to the program to make the program work. I have tried adding them in several places to no avail. A sample of a similar program would really help. Oh well it really doesnt matter anyway except for next weeks assignment as I already handed the assignment in. However, I would really like to understand where to put the conditional statment so that the program continues to loop back to the error statement, enabling users to continue to enter data till the error checking is satisfied. This way I can start on my next weeks assingment. thanks.

View Postjbeme, on 30 Oct, 2009 - 09:02 PM, said:

Look up Do...While statements and also For statements.

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Tax Calculator question

Posted 31 October 2009 - 08:51 AM

I started to write a whole long explanation of loops but you know what? They're already out there if you bother to look for them. For example, here.

So, is University of Phoenix still requiring that you use Miracle C as your compiler?
Was This Post Helpful? 0
  • +
  • -

#7 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Tax Calculator question

Posted 31 October 2009 - 08:57 AM

Yes, UoP is still requiring us to use miracle C as our compiler and I have tried to add an additional loop to the program to no avail. I took a C++ course before and used visual studio and had no problems with the loops but for some reason everytime I add the do while loop to the program i get a ton of errors. I already handed the program in but next weeks program is built on this weeks program that is why i am so frustrated. I don't like Miracle C its sucks. I understand that u add the do then the statements then the while and the expression but its not working on my program, I don't know why is it the compiler or have i gotten stupid here or is something wrong with my code that will not work with the do while statement. Thanks and any help would be appreciated as this is my only programming course and I only have 1 assignment to do.

View PostJackOfAllTrades, on 31 Oct, 2009 - 07:51 AM, said:

I started to write a whole long explanation of loops but you know what? They're already out there if you bother to look for them. For example, here.

So, is University of Phoenix still requiring that you use Miracle C as your compiler?

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Tax Calculator question

Posted 31 October 2009 - 09:01 AM

While I agree on the whole with the futility of using a non-standard compiler, a true craftsman does not blame his tools.

You can't tell us your code doesn't work or causes errors and expect decent help without providing the exact code causing such errors and the exact errors you are experiencing.
Was This Post Helpful? 0
  • +
  • -

#9 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Tax Calculator question

Posted 31 October 2009 - 09:45 AM

Okay below is the code that includes my do while statement. The problem is the program does compile and run, but it is not right. When u enter a numeric value it gives u the tax for all 3 locations; however, when you enter a letter or letters it lets u loop back to re enter a correct number and it will loop back several times if u continue to enter letters instead of numbers. The problem is if after u enter letters and it loops back, if you enter a number the program still thinks its an invalid entry. So what am i doing wrong. Any help in how to fix this issue would be great. I do know that I am missing something in the do while loop but am not sure what it is, thanks.

#include <stdio.h> // Include standard I/O libraries
#include <stdlib.h> // Include standard libraries
#include <math.h> // Include Math libraries

/* Defining tax rates for Kudler's three store */
	float DelMarRate = 7.25;	/* Del Mar tax rate */
	float EncinitasRate = 7.5;	/* Encintas tax rate */
	float LaJollaRate = 7.75;	/* LaJolla tax rate */
	
/* Variables for Kudler's three store's tax calculation */   
	float DelMarTax = 0.0;		/* Calculation variable for Del Mar taxes */
	float EncinitasTax = 0.0;	  /* Calculation variable for Encintas taxes */
	float LaJollaTax = 0.0;	   /* Calculation variable for LaJolla taxes */
	float Amount = 0.0;

int main(void)
{
char Purchase[32]; /* Declare variables */

int i,Return=0; 

	/* Prompt user for Purchase Amount */
	printf("\n*********************************");
	printf("\n* Welcome to Kudler Fine foods! *");
	printf("\n*********************************");
	printf("\n\n");
	printf("\n\nPlease Enter the customer's purchase amount:$");
	scanf("%s", Purchase);

do{
		/* Loop - Test the numeric values*/ 
		for(i=0; Purchase[i]; i++) 
		{
			if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
			{
			if (Purchase[i] == '.') /* If character is a decimal continue */
			continue;
				else
				Return=1; /* Set Non-Numerical Value flag */
				break; 
			} 
		}

			if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
			{
				printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
				fflush(stdin);
				printf("\n\nPlease Re-enter Purchase Amount:$"); 
				scanf("%s", Purchase); 
				
			} 
							 
}while (Return == 1);

Amount=atof(Purchase); /* Convert to Float */ 
DelMarTax=(Amount*DelMarRate)/100; /* Calculate Tax Amount for Del Mar */
EncinitasTax=(Amount*EncinitasRate)/100; /* Calculate Tax Amount for Encinitas */
LaJollaTax=(Amount*LaJollaRate)/100; /* Calculate Tax Amount for La Jolla */

	/* Display Tax Calculation output to the screen */
	printf("\n\n\n");
	printf("Del Mar\t\tEncinitas\tLa Jolla\t\n");
	printf("*******************************************\n");
	printf("$%.2f\t\t$%.2f\t\t$%.2f\n", DelMarTax, EncinitasTax, LaJollaTax);
	printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
	fflush(stdin);
	
	/*Keeping DOS Screen open so user can view output*/
		scanf("%c\n");	
	
		return 0;
	
}



This post has been edited by mistymoon1966: 31 October 2009 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6031
  • View blog
  • Posts: 23,414
  • Joined: 23-August 08

Re: Tax Calculator question

Posted 31 October 2009 - 12:51 PM

Just because a program compiles DOES NOT MEAN IT RUNS PROPERLY, it only means it's syntactically correct. Logic errors are not found by the compiler (although they may be hinted at by turning up warnings). The computer does exactly what you tell it to do.
Was This Post Helpful? 0
  • +
  • -

#11 mistymoon1966  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 17-May 07

Re: Tax Calculator question

Posted 31 October 2009 - 12:57 PM

Actaully, all I forgot in the do while statement was a return = 0; right after the do statement. It was not that i did not know this was needed it was just that I spent too many hours stressing over it and after walking away for a bit, I figured the problem out in 1 sec. As I said i have done some C++ programming but i mainly stick to web development which i do for a living. The program works beautifully and i did it all on my own. Thanks though for responding. The problem wasnt that I did not know how to do loops i do, it was just an issue with forgetting something as i do not do C or C++ programming often. Thanks again..


View PostJackOfAllTrades, on 31 Oct, 2009 - 11:51 AM, said:

Just because a program compiles DOES NOT MEAN IT RUNS PROPERLY, it only means it's syntactically correct. Logic errors are not found by the compiler (although they may be hinted at by turning up warnings). The computer does exactly what you tell it to do.



By the way if anyone in my class is reading these forums DO NOT COPY MY CODE as this is my own original code and I will show this thread to the instructor if need be, thanks.

Amanda Smith
CSS 561
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1