按下按钮,打开循环(do / while)

时间:2016-07-25 08:21:03

标签: c function loops do-while

所以,当我按n时,我希望循环停止工作。但似乎do / while循环存在问题。循环继续工作,无需等待ch(char)的输入。如果你们能帮助我,我们将不胜感激。预先感谢。

*注意:这是我的作业。我只能使用do / while循环和函数。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void input();
void output(int id,char *name,float salary);
void calculate(float salary);
int main(){

  system("cls");
   int ch;

    input();
    getch();
return 0;
 }
  void input(){
  char ch;
  do{

int id;
float salary,tax;
char name[30];
printf("Input Id :"); scanf("%d",&id);
printf("Input Name: "); fflush(stdin); gets(name);
printf("Input Salary :"); scanf("%f",&salary);
output(id,name,salary);
calculate(salary);
printf("Press n to stop : "); scanf("%c",&ch);

 }while(ch != 'n' );
 }
   void calculate(float salary)
 {

     float tax;
     float asalary;
     if(salary < 200)
     tax = salary * (0.1/100);
    if(salary > 200 && salary < 499)
    tax = salary * (1.5/100);
     if(salary > 499 && salary < 999)
    tax = salary * (2.5/100);
if(salary > 999 && salary < 1999)
    tax = salary * (3.5/100);
if(salary > 200)
    tax = salary * (4.1/100);

asalary = salary - tax;
printf("\n Tax you have to pay is %.2f",tax);
printf("\n Your salary after tax is %.2f",asalary); }

  void output(int id,char *name,float salary)
{
  printf("ID: %d\n",id);
  printf("Name: %s\n",name);
  printf("Salary: %.2f\n",salary);


}

0 个答案:

没有答案