![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
Y for yes and N for no problem
hi
I am writing a program which prompts to enter y to continue or n to exit. if you enter anything else than y or n it asks you to enter y or n. now so far i have solve the problem for y or n entry.its a nested while loop which sends zz=0 or zz=1 which terminates the parent while loop. I want to know why it goes through the loop twice when enter j or k.( it works fine when i enter y or n.) How do i solve this problem.(please try the code yourself before you answer as ive been given many answers but not a single one was correct.) here is the patch of the code which has the problem.(i included the whole code at the bottom and this patch starts at the line 200. to run the code enter 3067 for first prompt and 1 for the rest of them) inykeypress = 0; while(inykeypress != 'y' && inykeypress != 'n') { printf("\nPress Y for Yes or N for No"); inykeypress = fgetc(stdin); if (inykeypress==121) zz=0; else zz=1; } THE FULL CODE/*it will calculate the net income for ontario residents and made according to tax laws of ontario*/ /* Payroll Deductions - Compiling * sakbar.c */ #include <stdio.h> #define MIN 0 #define MAX 400000 double maxmin(double p,double c,double ei); double calohp(double ANN); double lesserof(double tempa,double tempb); double getdouble(double min, double max); int getint(int min, int max); void clear(void); double roundup(double); void main() { double i ; /*Gross Income */ double f ; /*Registered Pension Plan Contributions */ double uu ; /*Union Dues */ double p ; /*Number of Pay Periods */ double tc ; /*Federal Personal Claim */ double tcp ; /*Provincial Personal Claim */ double xx ; /*Number of Dependants below age of 18 */ double yy ; /*Labour-sponsored share purchases */ double ff ; /*Authourized deductions */ double d ; /*CPP contributions year-to-date */ double c ; /*CPP contribution */ double d_d ; /*EI premiums year-to-date */ double ei ; /*EI premiums */ double A ; /*Annual taxable income */ double r ; /*rate */ double k ; /*Overcharge constant */ double f_f ; /*line 4-5*/ double k1f ; /* Personal Tax credit */ double k2f ; /*cpp, EI credit */ double t3t ; /* Basic fedral tax */ double lcf ; /*Labour-sponsored tax credit */ double t1t ; /*Annual Federal Tax */ double v ; /* provincial rate */ double kp ; /* provincial overcharge constant */ double pp ; double k1p ; /*prov. personal tax credit */ double k2p ; /*prov. cpp EI credit */ double t4t ; /*basic prov. tax */ double v1p ; /*total surtax */ double s ; /* prov. tax reduction */ double lcp ; /* laboured-sponsered tax credit */ double v2p ; /* lesser of line 18 and 19 */ double t2o ; /* annual ontario tax */ double tp1f ; /* annual federal tax per pay period */ double tp2p ; /* annual ontario taz per pay period */ double T ; /* total tax deduction */ double TD ; /*total deduction */ double NI ; /*net income */ double bp ; /*base preimium*/ double pl ; /*premium limit*/ double ba ; /*base amount*/ int zz,inykeypress ; char jj,kkk,shit ; double tempa,tempb,tempc,tempd,tempe,tempf; zz=0 ; while (zz<1) { printf("Income for the current pay period :"); i=getdouble(MIN, MAX); printf("\nRegistered Pension Plan contributions :"); f=getdouble(MIN, i); printf("\nUnion dues for the current pay period :"); uu=getdouble(MIN, i-f); printf("\nNumber of pay periods this year :"); p=getint(0,52); printf("\nFederal personal claim :"); tc=getdouble(MIN, MAX); printf("\nprovincial personal claim :"); tcp=getdouble(MIN, MAX); printf("\nNumber of Dependants < 18 years old :"); xx=getint(MIN, 50); printf("\nLabour-sponsored share purchases :"); yy=getdouble(MIN, MAX); printf("\nAuthorized deductions :"); ff=getdouble(MIN, i); printf("\nCPP contributions year-to-date :"); d=getdouble(MIN, i-f-uu); printf("\nEI premiums paid year-to-date :"); d_d=getdouble(MIN, i-f-uu-d); tempa=0.0495*(i-(3500/p)); tempb=1861.20-d ; c=lesserof(tempa,tempb); tempa=.0195*i; tempb=760.50-d_d ; ei=lesserof(tempa,tempb); A=(p*(i-f-uu))-ff; if ((0<A)&&(A<=35595)) {r=0.16; k=0;} if ((35595<A)&&(A<71191)) {r=0.22; k=2136;} if ((71190<A)&&(A<115740)) {r=0.26; k=4983;} if (115739<A) {r=0.29; k=8455;} f_f=(A*r)-k ; k1f=0.16*tc; k2f=0.16*(maxmin(p,c,ei)); t3t=f_f-k1f-k2f ; if (t3t<0) t3t=0; tempa=0.15*yy ; lcf=lesserof(tempa,750); t1t=t3t-lcf ; if ((0<A)&&(A<=34010)) {v=0.0605; kp=0;} if ((34010<A)&&(A<68021)) {v=0.0915; kp=1054;} if (68020<A) {v=0.1116; kp=2422;} pp=(A*v)-kp ; k1p=0.0605*tcp ; k2p=0.0605*(maxmin(p,c,ei)); t4t=pp-k1p-k2p; if (t4t<0) t4t=0; if (0<((t4t-3929)*0.2)) tempa=(t4t-3929)*0.2; else tempa=0; if (0<((t4t-4957)*0.36)) tempb=(t4t-4957)*0.36; else tempb=0; v1p=tempa+tempb ; lcp=lcf; tempa=t4t+v1p; tempb=(2*(190+(xx*350)))-(t4t+v1p); s=lesserof(tempa,tempb); v2p=calohp(A); t2o=t4t+v1p+v2p-s-lcp ; tp1f=t1t/p; tp2p=t2o/p; T=tp1f+tp2p; TD=T+c+ei+uu+f; TD=roundup(TD); NI=i-TD; NI= roundup(NI); if (i>NI) printf("\n ***WARNING***\n ***YOUR TOTAL DEDUCTIONS ARE MORE THAN GROSS INCOME.***\n"); printf("\nGross income %10.2lf\n", i); printf("Deuctions:\n "); printf("Fedral Tax %10.2lf\n", tp1f); printf(" Provincial Tax %10.2lf\n", tp2p); printf(" Canada pension plan %10.2lf\n", c); printf(" Employment Insurance %10.2lf\n", ei); printf(" RRSP Contribution %10.2lf\n", f); printf(" Union Dues %10.2lf\n", uu); printf(" Total Deductions %10.2lf\n", TD); printf("\n\nNet Income %10.2lf\n", NI); inykeypress = 0; while(inykeypress != 'y' && inykeypress != 'n') { printf("\nPress Y for Yes or N for No"); inykeypress = fgetc(stdin); if (inykeypress==121) zz=0; else zz=1; }} getch(); return 0; } /*this function rounds up the number*/ double roundup(double value){ int trunc,trunca,truncb; double b,c,d,e,f,g,rounded; trunc=value; b=value-trunc; c=b*10000; trunca=c; d=c-trunca; if (d>0.44) e=trunca+1; else e=trunca; e=e*.01; truncb=e; f=e-truncb; if (f>0.44) g=(truncb+1)*0.01; else g=truncb*0.01; rounded=trunc+g; return rounded; } /* getdouble accepts from the standard input * stream an double between min and max inclusive, * returns the value of the double accepted */ double getdouble(double min, double max) { double value, keeptrying = 1, rc; char after; do { rc = scanf("%lf%c", &value, &after); if (rc == 0) { printf("**No input accepted!\n**Try again. :"); clear(); } else if (after != '\n') { printf("**Trailing characters\n**Try again. :"); clear(); } else if (value < min) { printf("**Value must be greater than 0.0\n**Try again. :"); } else if (value > max) { printf("**Out of max range of %.2lf\n**Try again. :",max); } else keeptrying = 0; } while (keeptrying == 1); return value; } /* getInt accepts from the standard input * stream an int between min and max inclusive, * returns the value of the int accepted */ int getint(int min, int max) { int value, keeptrying = 1, rc; char after; do { rc = scanf("%d%c", &value, &after); if (rc == 0) { printf("**No input accepted!\n**Try again. :"); clear(); } else if (after != '\n') { printf("**Trailing characters!\n**Try again. :"); clear(); } else if (value < min) { printf("**Value must be greater than 0\n**Try again. :"); } else if (value > max) { printf("**Out of max range of %d\n**Try again. :",max); } else keeptrying = 0; } while (keeptrying == 1); return value; } /* clear empties input buffer of characters */ void clear (void) { while ( getchar() != '\n' ) ; /* null statement intentional */ } /*lesser of the two values*/ double lesserof(double tempa,double tempb){ double c; if (tempa<=tempb) {c=tempa;} if (tempb<=tempa) {c=tempb;} if(tempa<=0||tempb<=0) {c=0 ;} return c; } /* function to calculate ontario health premium*/ double calohp(double ANN){ double ohr,bp,ba,pl,v2p,tempa,pli,bpi; /* there is a pattern in increasing premium limit so i am using that pattern as a base assuming the change will follow the same pattern*/ /* In 'pli' and 'bpi' the letter 'i' stands for increment*/ pli=150; bpi=150; if (ANN<20001) {ohr=0; bp=0; ba=0; pl=pli-pli;} if ((20000<ANN)&&(ANN<=36000)) {ohr=0.06; bp=0; pl=pli+pli; ba=20000;} if ((36000<ANN)&&(ANN<48001)) {ohr=0.06; bp=bpi*2; pl=3*pli; ba=36000; } if ((48000<ANN)&&(ANN<72001)) {ohr=0.25; bp=bpi*3; pl=pli*4; ba=48000; } if ((72000<ANN)&&(ANN<200001)) {ohr=0.25; bp=bpi*4; pl=pli*5; ba=72000; } if (200000<ANN) {ohr=0.25; bp=bpi*5; pl=pli*6; ba=200000; } tempa=(ohr*(ANN-ba))+bp ; v2p=lesserof(tempa,pl); return v2p; } /*helps in calculation of fedral and provincial CPP,EI credit*/ double maxmin(double p,double c,double ei) { int maxa,maxb,tempc,tempd; maxa=1861.20; maxb=760.50; tempc=p*c; tempd=p*ei; if (maxa<tempc) {tempc=maxa;} if (maxb<tempd) {tempd=maxb;} return (tempc+tempd); } |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 64
Rep Power: 4
![]() |
looks like you were close... try this
inykeypress = 0;
while(inykeypress != 'y' && inykeypress != 'n')
{
printf("\nPress Y for Yes or N for No");
inykeypress = fgetc(stdin);
getchar(); /* remove carriage return from stdin buffer */
if (inykeypress==121)
zz=0;
else
zz=1;
}
}
return 0;Note: if the user types more than one character, then <enter> you will still get multipe print statments. You could get the entire stream and just check the first character ???? or use fflush(stdin); instead of getchar(); Last edited by spydoor; Mar 1st, 2005 at 3:34 PM. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
Dude i can kiss you and i dont even care if you are a guy. (lol)
anyway thanks a lot guy. What does carriage return means (you can tell im a pure noob in c ) |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
|
Quote:
[] == Current cursor position BEFORE CARRAGE RETURN: asdfasdf[] AFTER CARRAGE RETURN: []asdfasdf |
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
and, for future programming projects, can i recommend that you use ENGLISH variable names? how in the world did you remember that v was the provincial rate while you were writing that?! I find it quite helpful to name variables somthing that acutally gives you a clue as to what the variable MEANS
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|