繁体   English   中英

在 do while 循环中省略了 scanf

[英]scanf is omitted in do while loop

我有这个程序,在它的第一个循环中它按预期工作,但是从第二个循环开始,它不执行第一个 scanf。 我们没有教过指针,所以我不知道他们是否能解决问题。 我应该用另一种循环语法来解决这个问题吗? 任何帮助表示赞赏。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char type[20];
    int amount;
    double price;
    double priceP;
    double priceG;
    double priceA;
    double pricePL;
    double priceC;
    price=0;
    char frouro[4]= "add";
    char payment[5];
    double refund;

    do{

       printf("What kind of material do you want to recycle today?. It has to be either paper , glass, aluminium, plastic or cloth\n");

       scanf(" %s" , &type);

       printf("how many materials do you want to recycle today? It has to be more than 1 and a maximum of 10\n");

       scanf(" %d" , &amount);

       if (strcmp(type, "paper") == 0)
          { 

           priceP = priceP + amount * 0.10;

          }   
       if (strcmp(type, "glass") == 0)
          { 

           priceG =priceP + amount * 0.20;

          }
       if (strcmp(type, "aluminium") == 0)
          { 

          priceA =  priceA + amount * 0.15;

          }
       if (strcmp(type, "plastic") == 0)
          { 

          pricePL =  pricePL +amount * 0.05;

          }  
       if (strcmp(type, "cloth") == 0)
          {

           priceC =  priceC + amount * 0.50;

          }  
      printf("do you want to do anything else?\n");

      scanf(" %c", &frouro);
      }while(strcmp(frouro,"add")==0 && strcmp(frouro,"end")!=0);

      return 0;
}

更改scanf(" %c", &frouro); scanf("%s", &frouro); .

当你在过程中存储1个字符, while条件检查这将是始终为false。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char type[20];
    int amount;
    double price;
    double priceP;
    double priceG;
    double priceA;
    double pricePL;
    double priceC;
    price=0;
    char frouro[4]= "add";
    char payment[5];
    double refund;

    do{

       printf("What kind of material do you want to recycle today?. It has to be either paper , glass, aluminium, plastic or cloth\n");

       scanf(" %s" , &type);

       printf("how many materials do you want to recycle today? It has to be more than 1 and a maximum of 10\n");

       scanf(" %d" , &amount);

       if (strcmp(type, "paper") == 0)
          { 

           priceP = priceP + amount * 0.10;

          }   
       if (strcmp(type, "glass") == 0)
          { 

           priceG =priceP + amount * 0.20;

          }
       if (strcmp(type, "aluminium") == 0)
          { 

          priceA =  priceA + amount * 0.15;

          }
       if (strcmp(type, "plastic") == 0)
          { 

          pricePL =  pricePL +amount * 0.05;

          }  
       if (strcmp(type, "cloth") == 0)
          {

           priceC =  priceC + amount * 0.50;

          }  
      printf("do you want to do anything else?\n");

      scanf("%s", &frouro);
      }while(strcmp(frouro,"add")==0 && strcmp(frouro,"end")!=0);

      return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM