c - do while 循环中省略了 scanf

标签 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 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;
}

关于c - do while 循环中省略了 scanf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59020035/

相关文章:

带有语言环境的 Javascript string.prototype.contains()

c++ - 如何使用 C++ 从网页中获取文本?

c - 使用 fork 和 dup 的简单 http 通用服务器

c++ - char temp[3] =""; 是什么意思?

c - 当我接受矩阵的第一个值时出现段错误

c - 尝试在 C 中分配结构数组的段错误

c - c 中的 typedef 和类型等价

java - 表达式的类型必须是数组类型,但它解析为 ArrayList(试图比较两个数组中的字符串

c - 如何解决 MISRA C :2012 Rule 11. 6?

无法以编程方式减小 gtk 窗口的大小