[英]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.