-
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();
}
-
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
-
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?
-
-
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!
- You're missing a closing double quote here:
Code:
printf ("Your tax rate is : 20%);
- Consider printing newlines after each line.
- You do have #include <stdio.h>, right?
- Your indentation could use improvement.
-
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!!.
-
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 ("%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)*/
-
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?
-
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 ("%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.
-
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.
-
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