为什么这段代码会提前退出?
#include <stdio.h>
#define MAX 5
int stk[MAX];
int top=-1;
main()
{
char ch;
void push();
void pop();
void display();
do
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
ch=getchar();
if(ch=='1')
push();
if(ch=='2')
pop();
if(ch=='3')
display();
printf("Do u want to continue y/n");
ch=getchar();
}while(ch=='y'||ch=='Y');
}
void push()
{
}
void pop()
{
}
void display()
{
}
当我完成一次推送操作时...程序打印““Do u Want to continue y/n”并退出...不等待用户输入“”y/Y”
请帮忙
#include <stdio.h>
#define MAX 5
int stk[MAX];
int top=-1;
main()
{
char ch;
void push();
void pop();
void display();
do
{
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
ch=getchar();
if(ch=='1')
push();
if(ch=='2')
pop();
if(ch=='3')
display();
printf("Do u want to continue y/n");
ch=getchar();
}while(ch=='y'||ch=='Y');
}
void push()
{
}
void pop()
{
}
void display()
{
}
The moment i finish with the push operation once...the program prints ""Do u want to continue y/n " and exits....doesnt wait for the user input "" y/Y"
Pls help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
\n
)。getchar()
一次,从缓冲区中读取该换行符,该换行符被分配给ch
并且不等于'y'
或'Y'
,因此循环退出。至于解决这个问题,那就留给你作为练习了。除了单独的
getchar()
之外,您还可以考虑使用其他读取数据的方法。请参阅此处了解一些输入函数(提示:fgets
)。您还可以尝试从缓冲区中提取该字符并将其丢弃,以便后续调用getchar()
按预期工作。如果这是为了学校(看起来是这样),您可能需要编写一个可以在整个课程中重复使用的函数。这样,您就可以调试它,并熟悉它的工作原理。
祝你好运!
\n
) in it from when the user pressed the Enter key.getchar()
once, reading in that newline from the buffer, which is assigned toch
and does not equate to'y'
or'Y'
, so your loop exits.As for fixing this problem, that is left as an exercise to you. You could look into using other methods of reading in data besides a lone
getchar()
. See here for some input functions (hint:fgets
). You could also try to extract this character from the buffer and discard it so subsequent calls togetchar()
work as expected.If this is for school, which it appears to be, you may want to write a function that you can reuse throughout your course. This way, you can debug it, and are familiar with how it works.
Good luck!
这是因为,当您输入 1 并按
RETURN
键时,两个 个字符将被放入缓冲区中(1
和一个换行符)。
然后,
newline
被第二个getchar()
拾取,因为它既不是Y
也不是y
,它退出了。快速修复(但很笨拙):在
printf
之前放置另一个getchar();
。如果您想要更可靠的用户输入,请参阅此处,< a href="https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf/1248017#1248017">此处或在附近使用 我的武器库中的防弹代码:
-
这是一个测试运行:
您可能还想充实一下您的
push
、pop
和display
函数:-) 开个玩笑。我想这就是你的下一步。顺便说一句,如果这是作业,我建议不要将上面的代码作为您自己的作业提交。你几乎肯定会被认为作弊,因为它很可能超出了你目前接受的教育水平,并且可以通过简单的网络搜索来找到它:进入
你友好的邻居谷歌搜索框即可找到答案。
That's because, when you enter 1 followed by the
RETURN
key, two characters are put in your buffer (the1
and anewline
).The
newline
is then picked up by the secondgetchar()
and, since because that's neitherY
nory
, it exits.Quick fix (but kludgy): put another
getchar();
before theprintf
.If you want more robust user input, see here, here or use this near-bulletproof code from my arsenal:
Here's a test run:
You may also want to flesh out your
push
,pop
anddisplay
functions a little :-) Just kidding. I'm assuming that's your next step.By the way, if this is homework, I'd suggest not handing in that code above as your own work. You will almost certainly be picked up as cheating since it's most likely beyond the level of education you're currently receiving, and it's available with an easy web search: enter
into your friendly neighbourhood Google search box to find out.
需要注意的小事。 getChar 返回 int 而不是 char。这可能会导致各种混乱和意外问题,因为类型可能大小不同。
Small thing to note. getChar returns int and not char. This can cause all kinds of mayhem and unexpected problems becuase the types are likely different sizes.