Home > Back-end >  Excuse me, I know the reason for the error, this return is read into the input work time, but why th
Excuse me, I know the reason for the error, this return is read into the input work time, but why th

Time:09-24





# include & lt; stdio.h>
# include & lt; Ctype. H>
# define TIME 40
# define M/1.5/1.5 times in
# define MONEY_1 300
# define MONEY_2 450
# define TAX_1 0.15
# define TAX_2 0.20
# define TAX_3 0.25
# define B1 8.75
# define B2 9.33
# define B3 10.00
# define B4 11.20

Char get_first (void);
Char get_option (void);
Void money (double B);

Int main (void)
{
Char option;

Printf (" please select your salary level: \ n ");
Printf (" a) $8.75/hr, TB) $9.33/hr, nc) $10.00/hr, td) is $11.20/hr, nq) quit \ n ");
While ((option=get_option ())! )
='q'{
The switch (option)
{
Case 'a' :
Money (B1);
break;
Case 'b' :
Money (B2);
break;
Case 'c' :
Money (B3);
break;
Case 'd' :
Money (B4);
break;
}
}
}

Char get_first (void)
{
Char ch;

ch=getchar();
While (isblank (ch))
{
ch=getchar();//if it's blank can continue to read, hit the first non-white-space characters does not enter circulation
}
While (getchar ()! )
='\ n'{
continue;
}

Return ch;
}

Char get_option (void)
{
Char ch;

Ch=get_first ();

While ((ch<'a' | | ch> 'd') & amp; & Ch! )
='q'{
Printf (" Srooy, I can not understand it, both please respond with a, b, c, d, or q \ n ");
Ch=get_first ();
}

Return ch;
}

Void money (double) B
{
Float hour;
Float income;
Float r_income;
Float tax;
Float BASE;

The BASE=B;
Printf (" please input a week working time: ");
The scanf (" % f ", & amp; Hour);
//fflush (stdin);//here to clear the cache, or input working hours return will be put into the cache, in the next cycle, enter to have read, since there is only one character at a time, the value of the ch is enter, therefore, repeated in get_option sorry
If (hour<=TIME)
{
Income=hour * BASE;
If (income<=MONEY_1)
{
Tax=income * TAX_1;
}
Else if (income<=MONEY_2)
{
Tax=MONEY_1 + (income - MONEY_1) * * TAX_1 TAX_2;
}
The else
{
Tax=MONEY_1 * TAX_1 + (MONEY_2 - MONEY_1) + (income - MONEY_2) * * TAX_2 TAX_3;
}
}
The else
{
Income=M * hour * BASE;
If (income<=MONEY_1)
{
Tax=income * TAX_1;
}
Else if (income<=MONEY_2)
{
Tax=MONEY_1 + (income - MONEY_1) * * TAX_1 TAX_2;
}
The else
{
Tax=MONEY_1 * TAX_1 + (MONEY_2 - MONEY_1) + (income - MONEY_2) * * TAX_2 TAX_3;
}
}
R_income=income tax;
Printf (" % f total wages, taxes as % f, net income for the % f \ n ", income, tax, r_income);
Printf (" \ n ");
Printf (" please select your salary level: \ n ");
Printf (" a) $8.75/hr, TB) $9.33/hr, nc) $10.00/hr, td) is $11.20/hr, nq) quit \ n ");
}

CodePudding user response:

According to the characters of the flow, entered a Enter first, and then because get_first, return is no problem, but in the input time at work, if you don't clear the cache, Enter will be sent to get_option ch, output so sorry, but the back type b, should be in line according to the truth, then it should be to b, you shouldn't still in Shanghai, why still here in Shanghai?

CodePudding user response:

 # include & lt; stdio.h> 
# include & lt; Ctype. H>
# define TIME 40
# define M/1.5/1.5 times in
# define MONEY_1 300
# define MONEY_2 450
# define TAX_1 0.15
# define TAX_2 0.20
# define TAX_3 0.25
# define B1 8.75
# define B2 9.33
# define B3 10.00
# define B4 11.20

Char get_first (void);
Char get_option (void);
Void money (double B);

Int main (void)
{
Char option;

Printf (" please select your salary level: \ n ");
Printf (" a) $8.75/hr, TB) $9.33/hr, nc) $10.00/hr, td) is $11.20/hr, nq) quit \ n ");
While ((option=get_option ())! )
='q'{
The switch (option)
{
Case 'a' :
Money (B1);
break;
Case 'b' :
Money (B2);
break;
Case 'c' :
Money (B3);
break;
Case 'd' :
Money (B4);
break;
}
getchar();//for '\ n'
}
}

Char get_first (void)
{
Char ch;

ch=getchar();
While (isblank (ch))
{
ch=getchar();//if it's blank can continue to read, hit the first non-white-space characters does not enter circulation
}
While (getchar ()! )
='\ n'{
continue;
}

Return ch;
}

Char get_option (void)
{
Char ch;

Ch=get_first ();

While ((ch<'a' | | ch> 'd') & amp; & Ch! )
='q'{
//printf (" Srooy, I can not understand it, both please respond with a, b, c, d, or q \ n ");
Printf (" Sorry, I can not understand it, both please respond with a, b, c, d, or q \ n ");
Ch=get_first ();
}

Return ch;
}

Void money (double) B
{
Float hour;
Float income;
Float r_income;
Float tax;
Float BASE;

The BASE=B;
Printf (" please input a week working time: ");
The scanf (" % f ", & amp; Hour);
//fflush (stdin);//here to clear the cache, or input working hours return will be put into the cache, in the next cycle, enter to have read, since there is only one character at a time, the value of the ch is enter, therefore, repeated in get_option sorry
If (hour<=TIME)
{
Income=hour * BASE;
If (income<=MONEY_1)
{
Tax=income * TAX_1;
}
Else if (income<=MONEY_2)
{
Tax=MONEY_1 + (income - MONEY_1) * * TAX_1 TAX_2;
}
The else
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related