为什么我的程序自动假定scanf的值,导致程序自动循环并跳过下一个scanf?
我试着编写一个代码作为计数器,它可以提示用户输入他们想要购买的物品数量,然后提示用户他们想要购买的物品数量 下面是我未完成的脚本为什么我的程序自动假定scanf的值,导致程序自动循环并跳过下一个scanf?,c,C,我试着编写一个代码作为计数器,它可以提示用户输入他们想要购买的物品数量,然后提示用户他们想要购买的物品数量 下面是我未完成的脚本 #include<stdio.h> #include<string.h> #include <math.h> #include <stdlib.h> #include <conio.h> void menu(); int repeat(); f
#include<stdio.h>
#include<string.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
void menu();
int repeat();
float determine_price(char);
float calc(float,int);
void print_result(float);
int main()
{
int qty,quantity,choice=1;
char selection;
float price,pay,item_price,payment;
//char set;
while(choice!=2){
menu();
int n=0;//line24
char arry[n][2];
printf("enter Number of Order\n");
scanf("%d",&n);//input number of loop//line 27
int x=0;
while(x<n){//loop until all item defined;line30
printf("enter Set and quantity:\n");
scanf("%c %d",&selection,&qty);//input selection & quantity;line32
price=determine_price(selection);//funct for return item price
pay= calc(price,qty);//funct return calculated price
arry[x][0]=selection;//put into array, for further use later
arry[x][1]=qty;//put into array, for further use later
print_result(pay);//print price result
x++;//counter
}
choice = repeat();//prompt user either to end or re run program;line 46
}
return 0;
}
void menu()// return string for menu
{
printf("**************************\n");
printf("|AZRI & YANA'S CAKE HOUSE|\n");
printf("**************************\n");
printf("\n");
printf("***************************\n");
printf("|1 CUP-------------RM3.50 |\n");
printf("|SET A(3 CUPS)-----RM10.00|\n");
printf("|SET B(6 CUPS)-----RM20.00|\n");
printf("|SET C(12 CUPS)----RM38.00|\n");
printf("***************************\n");
printf("\n");
}
void clr(){
system("cls");
//system("clear");
//clrscr ();
}
int repeat(){
int choicex;
printf("\n\n\n\n");
printf("Press\n[1] to return to menu\n[2] to end system\n");
scanf("%d",&choicex);
if(choicex==1){
clr();
}
else{
//choicex=2;
}
return choicex;
}
float determine_price(char selection){
float setprice;
if(selection=='1'){
printf("1 cup = RM3.50\n");
setprice=3.50;
}
else if(selection=='A'){
printf("Set A = RM10.00\n");
setprice=10.00;
}
else if(selection=='B'){
printf("Set B = RM20.00\n");
setprice=20.00;
}
else if(selection=='C'){
printf("Set C = RM38.00\n");
setprice=38.00;
}
else
printf("set not available\n");
return(setprice);
}
float calc(float item_pricing,int quantity){
float answer;
answer = item_pricing*quantity;
return(answer);
}
void print_result(float payment){
printf("total cost = RM%4.2f\n",payment);
}
#包括
#包括
#包括
#包括
#包括
无效菜单();
int repeat();
浮动价格(字符);
浮动计算(浮动,整数);
无效打印结果(浮动);
int main()
{
整数数量,数量,选项=1;
字符选择;
浮动价格、付款、项目价格、付款;
//字符集;
while(选项!=2){
菜单();
int n=0;//第24行
字符arry[n][2];
printf(“输入订单编号”);
scanf(“%d”,&n);//循环的输入编号//第27行
int x=0;
while(xscanf(“%c%d”)、选择和数量;
但是,之后它将开始无限循环,从第24行到第27行循环,没有enter while循环,也没有execute行46
有人能帮我理解我的程序的问题吗?首先,我认为在用户输入n的值后,您必须声明arry[n][2]
。否则,您将获得运行时未定义的行为
你会得到一个无限循环,因为缓冲区不是空的,所以你必须刷新它
如果选中此代码,它将忽略第二次扫描,因为'\n'
仍在缓冲区中
int i;
char c;
printf("i >> ");
scanf("%d", &i);
scanf("%c", &c); // '\n' that remains in buffer is assigned to the c variable
printf("input is %d %d", i, c); // 2nd %d is to print character's ASCII Code
输出为10
第二个数字(10)表示新行元素的ASCII码
scanf("%d",&n); // Line 27
while(getchar()!='\n'); // clearing the buffer
char arry[n][2]; // arry[n][2] must be declared after input a value for n
// ...
scanf("%c %d",&selection,&qty); // Line 32 + 1
while(getchar()!='\n'); // clearing the buffer again
首先,您必须在用户输入n的值后声明arry[n][2]
。否则,您将获得运行时未定义的行为
你会得到一个无限循环,因为缓冲区不是空的,所以你必须刷新它
如果选中此代码,它将忽略第二次扫描,因为'\n'
仍在缓冲区中
int i;
char c;
printf("i >> ");
scanf("%d", &i);
scanf("%c", &c); // '\n' that remains in buffer is assigned to the c variable
printf("input is %d %d", i, c); // 2nd %d is to print character's ASCII Code
输出为10
第二个数字(10)表示新行元素的ASCII码
scanf("%d",&n); // Line 27
while(getchar()!='\n'); // clearing the buffer
char arry[n][2]; // arry[n][2] must be declared after input a value for n
// ...
scanf("%c %d",&selection,&qty); // Line 32 + 1
while(getchar()!='\n'); // clearing the buffer again
请阅读scanf
的手册页,并检查其返回值将scanf(“%c%d”
更改为scanf(“%c%d”
)。同意@Ed Heal,阅读man并检查scanf()
results.ty,我知道我的问题困扰着你们大家,对不起,但实际上我只是在C上做了几天而已..ty,我现在要做我的研究…可能重复的请阅读scanf
的手册页并检查其返回值将scanf(%C%d)
更改为scanf(%C%d)
。同意@Ed-Heal,阅读man并检查scanf()
results too.ty,我知道我的问题困扰着你们大家,对不起,但实际上我只是在C上做了几天..ty all.我现在要做我的研究…ty的可能复制品,你的解决方案对我有用..但是,我真的不太明白它是如何工作的…ty这么多。我现在会研究更多…当用户输入最后认为他点击的东西是t时他按下enter按钮,最后一个元素是'\n'
新行元素。调用scanf()
输入整数后,\n
仍在缓冲区中,因为它与it(整数)不匹配,所以我们需要刷新缓冲区。getchar()函数从缓冲区中获取一个字符并仅删除该字符,如果没有向其传递参数,则该字符不会被存储。因此,while循环继续查找最后一个元素“\n”,并清除所有缓冲区。ty,您的解决方案对我有效。但是,我真的不太明白它是如何工作的…ty这么多。现在,当用户输入他认为最后点击的是enter按钮,因此最后一个元素是'\n'
新行元素。调用scanf()
输入整数后,\n
仍在缓冲区中,因为它与it(整数)不匹配,所以我们需要刷新缓冲区。getchar()函数从缓冲区中获取一个字符,如果未向该字符传递任何参数,则只删除该字符,因此该字符不会被存储。因此while循环继续查找最后一个元素“\n”,并清除所有缓冲区。