在do while循环中忽略scanf

在do while循环中忽略scanf,c,arrays,string,scanf,do-while,C,Arrays,String,Scanf,Do While,我有这个程序,在第一个循环中它按预期工作,但从第二个循环开始,它不执行第一个scanf。我们还没有被教过指针,所以我不知道他们是否能解决这个问题。我应该用另一种循环语法(如for)来解决这个问题吗?感谢您的帮助 #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) { char type[20]; int amou

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

#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;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
炭型[20];
整数金额;
双倍价格;
双倍价格;
双倍价格;
双倍价格a;
双倍价格;
双倍价格c;
价格=0;
char frouro[4]=“添加”;
字符支付[5];
双倍退款;
做{
printf(“您今天希望回收哪种材料?必须是纸、玻璃、铝、塑料或布”);
scanf(“%s”,类型(&T);
printf(“今天要回收多少材料?必须超过1,最多10个”);
scanf(“%d”和金额);
if(strcmp(类型,“纸张”)==0)
{ 
价格P=价格P+金额*0.10;
}   
if(strcmp(玻璃型)==0)
{ 
价格G=价格P+金额*0.20;
}
if(strcmp(铝)型)==0)
{ 
价格A=价格A+金额*0.15;
}
if(strcmp(类型,“塑料”)==0)
{ 
价格损益=价格损益+金额*0.05;
}  
if(strcmp(类型,“布料”)==0)
{
价格C=价格C+金额*0.50;
}  
printf(“您还想做其他事情吗?\n”);
scanf(“%c”和“frouro”);
}while(strcmp(frouro,“add”)=0和strcmp(frouro,“end”)!=0);
返回0;
}
Change
scanf(“%c”&frouro)
scanf(“%s”和&frouro)

当您存储1个字符时,在
期间,而
条件检查期间,它将始终为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;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
炭型[20];
整数金额;
双倍价格;
双倍价格;
双倍价格;
双倍价格a;
双倍价格;
双倍价格c;
价格=0;
char frouro[4]=“添加”;
字符支付[5];
双倍退款;
做{
printf(“您今天希望回收哪种材料?必须是纸、玻璃、铝、塑料或布”);
scanf(“%s”,类型(&T);
printf(“今天要回收多少材料?必须超过1,最多10个”);
scanf(“%d”和金额);
if(strcmp(类型,“纸张”)==0)
{ 
价格P=价格P+金额*0.10;
}   
if(strcmp(玻璃型)==0)
{ 
价格G=价格P+金额*0.20;
}
if(strcmp(铝)型)==0)
{ 
价格A=价格A+金额*0.15;
}
if(strcmp(类型,“塑料”)==0)
{ 
价格损益=价格损益+金额*0.05;
}  
if(strcmp(类型,“布料”)==0)
{
价格C=价格C+金额*0.50;
}  
printf(“您还想做其他事情吗?\n”);
scanf(“%s”和“frouro”);
}while(strcmp(frouro,“add”)=0和strcmp(frouro,“end”)!=0);
返回0;
}

如果第二次扫描未能转换为十进制整数,则可能会发生这种情况:%d转换说明符要求将输入文本格式化为十进制整数。否则,转换将失败,导致转换失败的字符将保留在输入流中。使用%d转换说明符对scanf()的进一步调用将在同一个字符上阻塞
scanf(“%s”和&frouro)。然后用户输入
qowitjglanvoefmaafnmqvmaoijorpijfvlaksdmcvopaheakfmoaijfomlakjewoifja;VkaoijerFoamogjaomewrvopawnvm,FNZKJH
,导致缓冲区溢出。不会,因为程序员自己永远无法分配足够大的缓冲区大小。您还需要限制scanf读取的输入,最好是:。