Thread: Programming trouble.

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    18

    Programming trouble.

    Im trying to write a program for school, and its supposed to :

    Using loop , write a program to read as many data as the user wishes to enter. The program will read the number of dependants and of income for each entry. Test every entry and print their tax rate based on the following:
    If the number of dependants is >= 5 and the income <= $25000 tax rate = 10%
    If the number is between 2 and <= 5 and the income is <=$45000 tax rate=20%
    If the number is <= 1 and the income is >= $45000 the taxe rate =30%
    Everything else is 40% tax rate.
    (supposed to use While statement)
    This is where i got but im afraid im kinda stuck (Im a noob ^^)
    Code:
    main()
    {
    int dependant;
    long income;
    
    printf ("Thank you for using my Tax calculator");
    printf ("Please enter your annual income:  )"
    scanf ("%ld", &income);
    printf ("Please enter the number of dependants in your house hold")
    scanf ("%d", &dependant);
    
     while (true)
     {
                    if(dependant >= 5 && income <= 25000)
                    {printf ("Your tax rate is : 10%");}
    	else if(dependant >= 2 && dpendant <= 5 && income <= 45000)
    	{printf ("Your tax rate is : 20%);}	
    	else if(dependant <= 1 && income >= 45000)
    	{printf ("Your tax rate is : 30%");}
                    else
    	{printf ("Your tax rate is : 40%");}
     }
    getch();
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You need 1 change and 1 addition.

    First, you'll need to decide what signifies a reason to break out of your infinite loop. This should be a prompt for the user to exit.

    Also, since this loop should ask the user over and over again for new dependants and new incomes, those should be inside your loop too.

    Todd

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    hmmm.... could you give me some more inout please... i'm trying to get ahead of my class, and we havent touched the while statement yet.

    so would i do that by creating more if statements inside of the while loop, and then putting a ( break; ) at the en of the loop before closing brackets?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    A few things to note about your code.
    • main() should return int. The implicit-int rule where you don't have to specify a return value is deprecated. http://faq.cprogramming.com/cgi-bin/...&id=1043284376
    • getch() is non-standard. Consider avoiding it if at all possible. http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    • true isn't a keyword in C (unless you're using C99 and you include iso464.h) -- consider using 1 instead. Or my favourite!
      Code:
      for(;;)
    • You're missing a closing double quote here:
      Code:
      printf ("Your tax rate is : 20&#37;);
    • Consider printing newlines after each line.
    • You do have #include <stdio.h>, right?
    • Your indentation could use improvement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    18

    ups

    (yes i do have #include<stdio.h> and #include<conio.h>)

    Just decided not to show the whole code that i wrote.... mainly my problemm is that i have to use an old compiler (tclite) and that i dont really know how to do while looping.
    Indentation indeed could use some help but i was looking to getting the code down as quickly as posible as i was leaving the school library for a 2 hour drive back home.... hehehe.
    and thx 4 correceting the syntax, i really need to get better at not making silly mistakes like that.
    Regarding the getch() i had no idead i should avoid it.... thx 4 the tip!!.
    Last edited by francusmag; 11-28-2007 at 10:52 PM.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    ok i re-did the code hopefully better... with a "do, while" statement. but i need some tips on the las part.
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    
    int dependant;
    long income;
    
    do {
    
         printf ("Thank you for using my tax calculator program!");
         printf ("Please enter your income :   ");
         scanf ("&#37;ld", &income);
         printf ("Please enter the number of dependants:  ");
         scanf ("%d", %dependant);
    	
         if(dependants >= 5 && income <= 25000)
    	{
    		printf ("Your tax rate is : 10%");
    	}
         else if(dependants >= 2 && dependants <= 5 && income <= 45000)
    	{
    		printf ("Your tax rate is : 20%");
    	}
         else if(dependants <= 1 && income >= 45000)
    	{
    		printf ("Your tax rate is : 30%");
    	}
         else
    	{
    		printf ("Your tax rate is : 40%");
    	}
    }
    while /*Here i will like to add the loop for the person to say they
    	would like to run the program again with either a "y" (yes)
            or a "n" (no)*/
    Last edited by francusmag; 11-28-2007 at 11:23 PM.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    i would have to add another variable userEntry. and it may look like this:
    while (userEntry == "Y" || userEntry =="y") ----- not very sure.
    would i have to declare variable (userEntry) as a char?
    and where do i add the break; statement to quit the loop?

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    ok this is the last variation i got for the program:
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    
    int dependant;
    long income;
    char userEntry[1]; 
    
    do {
    
         printf ("\t\t\tThank you for using my tax calculator program!\n\n");
         printf ("Please enter your income :   ");
         scanf ("&#37;ld", &income);
         printf ("Please enter the number of dependants:  ");
         scanf ("%d", &dependant);
    	
         if(dependant >= 5 && income <= 25000)
    	{
    		printf ("\nYour tax rate is : 10%\n\n");
    	}
         else if(dependant >= 2 && income <= 45000)
    	{
    		printf ("\nYour tax rate is : 20%\n\n");
    	}
         else if(dependant <= 1 && income >= 45000)
    	{
    		printf ("\nYour tax rate is : 30%\n\n");
    	}
         else
    	{
    		printf ("\nYour tax rate is : 40%\n\n");
    	}
    }
    
    printf ("Wish to continue? Enter [Y]es or [N]o:  ");
    while (userEntry = getchar())
        {
    	if(userEntry == "Y" || userEntry == "y")
    	{
    		break;
    	}
    	else(userEntry == "N" || userEntry == "n")
    	{
    		continue;
    	}
        }
     	
    printf ("Thank you for using my program please come back again");	
    
    getchar();
    }
    Im getting an error mistake on the 1st else if statement it says.... Warning: Constant is long in function main.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, that's wrong. Ask for the user input inside the do...while loop.
    Your first loop is now infinite again. It should be do...while(condition), so move your while up again.

    The while statement controls when the program will break the loop, so you don't need break or continue or anything like that since the condition will break the loop for you. I don't recommend using break in loops where you have a condition to break it, because it can be confusing. It's better used in loops where you don't use a condition, like an infinite for loop, to manually, within the code break the function when criterias are met.

    You can re-check the tutorial if you need a little help: http://www.cprogramming.com/tutorial/c/lesson3.html

    As for indentation, time is not an excuse. You don't write sloppy code and no indentation and then indent it. You indent the code as you write it, not the other way around.
    Last edited by Elysia; 11-29-2007 at 06:42 AM.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Also, you define an array of one byte in length for userEntry, and then getch() into it. You don't need an array - a simple char will do it.

    Todd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Is it so trouble?
    By Yumin in forum Tech Board
    Replies: 4
    Last Post: 01-30-2006, 04:10 PM
  4. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  5. C++ program trouble
    By senrab in forum C++ Programming
    Replies: 7
    Last Post: 04-29-2003, 11:55 PM