我一直在做一个大学项目。我希望用户输入Y / N,如果他想继续或不继续,所以我写了下面的代码
repeat:
pr=0;
q=0;
res=0;
printf("\nEnter the serial number of product you wish to purchase: ");
scanf("%d",&pr);
printf("Quantity: ");
scanf("%d",&q);
billing(pr,q);
printf("Continue Shopping? (y/n) ");
scanf("%d",&res);
if(res=='y')
goto repeat;
else
return 0;
}
问题是输入y执行else语句。我尝试用1,2,3替换y ....它可以工作,但我想让它与Y或Y一起工作或是。
答案 0 :(得分:1)
此行中存在错误
scanf("%d",&res);
应该是
scanf(" %c",&res);
char的格式占位符为%c
而不是%d
答案 1 :(得分:-1)
scanf("%d",&res);
应该是
scanf(" %c",&res);
正如多个用户所建议的,同时应该使用goto,因此更好的代码将是
do{
pr=0;
q=0;
printf("\nEnter the serial number of product you wish to purchase: ");
scanf("%d",&pr);
printf("Quantity: ");
scanf("%d",&q);
billing(pr,q);
printf("Continue Shopping? (y/n) ");
scanf(" %c",&res);
}
while(*res == 'Y' || *res == 'y');
return 0;
}
也应该声明res char res;