Thread: Tax Calculation Help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    Tax Calculation Help

    Hey fellow programmers....

    I am working on a basic tax calcualtion for a class and I am having a little trouble. I keep getting an error when trying to execute the program. I am also trying to make the program calculate and display the total amount for all three locations and not just one each time. I know there has to be a simple command I can insert to it calculates all three at one and time and displays the total. Anyone experienced programmers point out what I might be doing wrong? Thanks so much to anyone that can help.

    Here is the code I have:

    Code:
    #include <stdio.h>//header
    #include <ctype.h>//isdigit header
    main()
    {//start
    int iResponse = 0;//town selection
    float fCost;//calculation operator
    fCost = 0;
    printf ("\n\tThank you for using the Kudler tax calculation utility.\n");
    printf ("\n\tPlease select your location to recieve your taxable total.\n");
    Printf ("\n	1\tDel Mar\n");
    Printf ("	2\tEncinitas\n");
    Printf ("	3\tLa Jolla\n");
    printf("\n	Please select your location (1-3): ");
    for(;;){
    	if (scanf("&#37;d", &iResponse)==0)
    		break;
    		{
    		        puts("Invalid number, please try again");
            
            /* clear the input buffer */
            while((iResponse = getchar()) != '\n' && iResponse != EOF) {}
    	
    switch (iResponse)
    
    	case 1:
    		printf("\n	Welcome Del Mar Associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("\n%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    		break;
    		getchar();
    		
    	case 2:
    		printf("\n	Welcome Encinitas associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .075);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    		break;
    		getchar();
    
    	case 3:
    		printf("\n	Welcome La Jolla associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");		
    		printf("	Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    		break;
    		getchar();
    		}// end calc						
    		getchar();//keeps application viewable on screen
    	}// end selection
    Last edited by Cyberdine; 04-09-2007 at 07:48 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    For one thing, you've typed "Printf" instead of "printf" on 3 lines.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Ah ok I see I had....

    Printf ("\n 1\tDel Mar\n");
    Printf (" 2\tEncinitas\n");
    Printf (" 3\tLa Jolla\n");

    So I made the change to "printf". Not sure what I was thinking there as it must have been a habit for typing the capital "P".

    The error I am getting is:

    line 25: Parse Error, expecting `'{''
    'switch (iResponse) case 1: printf("\n Welcome Del Mar Associate.\n")'

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You need a left brace on the switch line:
    Code:
      switch (iResponse) { // <- you don't have this
    and the corresponding right brace afterwards. Also, you're not indenting properly at all. When writing code, always follow these two rules:

    1) When you type a left brace, IMMEDIATELY type the corresponding closing right brace. Don't wait.
    2) When you start typing statements after each left brace, indent them by your chosen amount of white space.

    If you follow these two rules, your code will always be properly indented as you write it, and it will help you avoid mistakes.
    Last edited by robatino; 04-09-2007 at 07:54 AM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Another thing.
    all this getchar() after break have no effect.
    Kurt

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Ok I have entered the left brace...example:

    switch (iResponse) { //

    case 1:
    printf("\n Welcome Del Mar Associate.\n");
    printf(" Please enter your sub-total: $");

    Is that what you were meaning? Also I know I am to put the "right" brace right after it? But wouldn't it not be on down in the statement? Where would I put it then?

    Thanks robatino.....

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    I meant that while you're in the middle of writing code, you write the corresponding right brace a few lines down, at the same offset from the left. For example, you can start your program like this:
    Code:
    int main(void) {
    
    }
    Notice that I immediately type the right brace. I do NOT write the entire program and then type the closing right brace last. Of course, when you start adding lines, they go between the two braces. Now I start adding lines, each indented (I personally use 2 spaces). It doesn't matter how much you use. Just choose something and use it consistently.
    Code:
    #include <stdio.h>
    
    int main(void) {
      int i;
      float j;
    
    }
    Now, if I want to add a loop, that has its own braces. So when I type the left brace, I also type the right brace:
    Code:
    #include <stdio.h>
    
    int main(void) {
      int i;
      float j;
      for (i=0; i<5; i++) {
    
      }
    
    }
    and so on. Notice that at every step the code is properly indented. This helps prevent mistakes.
    Last edited by robatino; 04-09-2007 at 08:05 AM.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Quote Originally Posted by ZuK View Post
    Another thing.
    all this getchar() after break have no effect.
    Kurt
    Thanks....yes if they are not necessary then I will delete them out.....

    Let me update my code and then I will repost a cleaned up version.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Ok here is a better or cleaned up version:

    Code:
    #include <stdio.h>//header
    #include <ctype.h>//isdigit header
    main()
    {//start
    int iResponse = 0;//town selection
    float fCost;//calculation operator
    fCost = 0;
    printf ("\n\tThank you for using the Kudler tax calculation utility.\n");
    printf ("\n\tPlease select your location to recieve your taxable total.\n");
    printf ("\n	1\tDel Mar\n");
    printf ("	2\tEncinitas\n");
    printf ("	3\tLa Jolla\n");
    printf("\n	Please select your location (1-3): ");
    for(;;){
    	if (scanf("&#37;d", &iResponse)==0)
    		break;
    		{
    		        puts("Invalid number, please try again");
            
            /* clear the input buffer */
            while((iResponse = getchar()) != '\n' && iResponse != EOF) {}
    	
    switch (iResponse) { //
    		}
    	case 1:
    		printf("\n	Welcome Del Mar Associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("\n%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    		break;
    		
    	case 2:
    		printf("\n	Welcome Encinitas associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .075);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    		break;
    
    	case 3:
    		printf("\n	Welcome La Jolla associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");		
    		printf("	Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    		break;
    		}// end calc						
    		getchar();//keeps application viewable on screen
    	}// end selection

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    How about deleting code until you have something which compiles?

    Or produces only a few error messages?

    >
    Code:
    while((iResponse = getchar()) != '\n' && iResponse != EOF) {}
    This while loop does nothing

    >
    Code:
    switch (iResponse) { //
    		}
    This switch has no cases

    Remember, write a bit, compile it, test it.

    Not write everything, compile and get 100's of errors then dump on a message board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Quote Originally Posted by Salem View Post
    Not write everything, compile and get 100's of errors then dump on a message board.
    Ah I did have the whole thing working and I am not getting a 100 errors as well. I made a few changes with some of the code to get to try and calculate all three stores and display them all three at once.

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Ok got some more bugs worked out. I need to add a loop so it calculates all three stores and shows me all three store totals. How can I or where would I add the loop to do this? I got an example but am still a little confused,

    Code:
    int main()
    { 
      int x = 0;  /* Don't forget to declare variables */
      
      while ( x < 10 ) { /* While x is less than 10 */
          printf( "&#37;d\n", x );
          x++;             /* Update x so the condition can be met eventually */
      }
      getchar();
    }

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Cyberdine View Post
    Ok got some more bugs worked out. I need to add a loop so it calculates all three stores and shows me all three store totals. How can I or where would I add the loop to do this? I got an example but am still a little confused,

    Code:
    int main()
    { 
      int x = 0;  /* Don't forget to declare variables */
      
      while ( x < 10 ) { /* While x is less than 10 */
          printf( "&#37;d\n", x );
          x++;             /* Update x so the condition can be met eventually */
      }
      getchar();
    }
    You could do something like that, but there's an easier way - if you're game for it.

    As you probably know, control "falls through" each of the cases in a switch case statement, unless it has a break statement.

    So no break statements, and POOF!, it all gets printed up! Of course, we can't do something that rash, but we could add another an if statement to the bottom of each case statement, and basically tell the program "If the case is not 4, then break out".

    Meaning of course, if the case IS 4, then we just want it to "fall through" to print out the next customer's info.

    Code:
     
    
    switch (iResponse) { //
    		}
    	case 1: case 4:
    		printf("\n	Welcome Del Mar Associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("\n%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    		if (case != 4) break;
    		
    	case 2: 
    		printf("\n	Welcome Encinitas associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .075);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    		break;
    
    	case 3:
    		printf("\n	Welcome La Jolla associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");		
    		printf("	Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    		break;
    		}// end calc						
    		
                    getchar();//keeps application viewable on screen
    	}// end selection
    Note the blue code portion above. Something like that, for all the other cases, perhaps?

    Adak
    Last edited by Adak; 04-09-2007 at 10:06 AM.

  14. #14
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Thanks Adak....I will try and put it into the code once I get back from lunch and see if it works. Will post the new code as well! I do appreciate everyone's help here!!

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    Adak,

    Here are the changes I made...what am I missing? Also here is the error I am getting as well. Thank you for your help!!

    Error: line 25: Parse Error, expecting `')'' or `','' 'if (case != 4) break'

    Code:
    #include <stdio.h>//header
    #include <ctype.h>//isdigit header
    main()
    {//start
    int iResponse = 0;//town selection
    float fCost;//calculation operator
    fCost = 0;
    printf ("\n\tThank you for using the Kudler tax calculation utility.\n");
    printf ("\n\tPlease select your location to recieve your taxable total.\n");
    Printf ("\n	1\tDel Mar\n");
    Printf ("	2\tEncinitas\n");
    Printf ("	3\tLa Jolla\n");
    printf("\n	Please select your location (1-3): ");
    scanf("&#37;d", &iResponse);
    switch (iResponse)						
    {
    
    	case 1: case 4:
    		printf("\n	Welcome Del Mar Associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("\n%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .0725);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0725));//in-line calculation
    		if (case != 4) break;
    		
    	case 2: case 4:
    		printf("\n	Welcome Encinitas associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");
    		printf("	Your total tax is $%.2f\n", fCost * .075);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.075));//in-line calculation
    		if (case != 4) break;
    
    	case 3: case 4:
    		printf("\n	Welcome La Jolla associate.\n");
    		printf("	Please enter your sub-total: $");
    		scanf("%f", &fCost);//sale sub-total
    		printf("	--------------------------------\n");		
    		printf("	Your total tax is $%.2f\n", fCost * .0775);//in-line calculation
    		printf("	The total amount owed is $%.2f\n", fCost * (1+.0775));//in-line calculation
    		if (case != 4) break;
    		}// end calc						
    		getchar();//keeps application viewable on screen
    	}// end selection

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  2. Big help for big program
    By Mahesh in forum C Programming
    Replies: 1
    Last Post: 05-04-2005, 10:02 AM
  3. Help with sample tax program
    By DaMonsta in forum C Programming
    Replies: 1
    Last Post: 05-15-2003, 03:45 AM
  4. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM
  5. calculation from rows and columns
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 07:44 PM