我不明白为什么我无法在 c 中获得三个输入

发布于 2024-12-10 20:23:44 字数 643 浏览 0 评论 0原文

我的一个朋友正在尝试学习c(自学,通过一本书),有时她会寻求帮助。

她只是向我展示了一些我无法回答的东西;我很惭愧,但我在大学学习了 C,然后转向了 php。我真的很困惑,所以我想知道为什么我们无法获得三个输入。这是部分代码:

#include <stdio.h>

int main()
{
    int num1;
    int num2;
    char x;

    printf("Enter a number:\n");
    scanf("%d\n",&num1);
    printf("Enter another number:\n");
    scanf("%d\n",&num2);
    printf("Choose an operation sign:\n");
    scanf("%c\n",&x);

...

像这样,它要求第一个输入两次,像这样:

Enter a number:
1
2
Enter another number:
3
Choose an operation sign:
-

如果我删除 \n 它会跳过最后一个 scanf

你能帮我理解为什么吗?

A friend of mine is trying to learn c (on her own, with a book) and sometimes she asks for help.

She just showed me something I can't answer; I'm ashamed but I studied C in college and then moved to php. I'm really stuck so I would like to know why we can't get three inputs. Here's partial code:

#include <stdio.h>

int main()
{
    int num1;
    int num2;
    char x;

    printf("Enter a number:\n");
    scanf("%d\n",&num1);
    printf("Enter another number:\n");
    scanf("%d\n",&num2);
    printf("Choose an operation sign:\n");
    scanf("%c\n",&x);

...

Like this it asks for the first input two times, like this:

Enter a number:
1
2
Enter another number:
3
Choose an operation sign:
-

If I remove the \n it skips the last scanf.

Can you help me understand why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-12-17 20:23:45

您还可以尝试使用fflush,但这取决于库实现(stdio)。
可以在此处找到它的 C 参考。

稍后我会对此进行测试并更新我的帖子并说明它是否有效。

You can also try to use fflush, but it depends on the library implementation (stdio).
The C reference for it can be found here.

I'll test this a bit later on and update my post and say whether it worked.

久隐师 2024-12-17 20:23:44

阅读此处: scanf() 将新行字符保留在buffer?

解决方案:

int main()
{
    int num1;
    int num2;
    char x;

    printf("Enter a number:\n");
    scanf("%d",&num1);
    printf("Enter another number:\n");
    scanf("%d",&num2);
    printf("Choose an operation sign:\n");
    scanf("\n%c",&x); /* See the \n <---------------- */
}

替代方案:

char buf[2]; /* We need 2 characters for the null */
scanf("%1s", buf); /* We ask max 1 character (plus null given by scanf) */
char x = buf[0]; /* We take the first character */

作为一个小注释,由于 scanf 的工作原理,使用这两种解决方案,您都可以直接在第一个“输入”中插入所有数据各种 scanf 将参与其中。因此,您可以插入 123 234 +,它将正确拆分为三个变量。

Read here: scanf() leaves the new line char in buffer?

Solution:

int main()
{
    int num1;
    int num2;
    char x;

    printf("Enter a number:\n");
    scanf("%d",&num1);
    printf("Enter another number:\n");
    scanf("%d",&num2);
    printf("Choose an operation sign:\n");
    scanf("\n%c",&x); /* See the \n <---------------- */
}

An alternative:

char buf[2]; /* We need 2 characters for the null */
scanf("%1s", buf); /* We ask max 1 character (plus null given by scanf) */
char x = buf[0]; /* We take the first character */

As a small note, thanks to how scanf works, with both the solutions you can insert directly in the first "input" all the data and the various scanf will take their part. So you could insert 123 234 + and it would be split in the three variables correctly.

开始看清了 2024-12-17 20:23:44

是的,scanf 不会删除换行符,并且您无法刷新 stdin,那么这样怎么样:

int num1;
char nleater;
printf("Enter a number:\n");
scanf("%d%c", &num1, &nleater);

或者实际上是这样:

printf("Enter number sign number: ");
scanf("%d %c %d",&num1,&x,&num2);
printf("%d %c %d", num1, x, num2);

Yes, scanf does not remove the newline, and you can't flush stdin, so how about this:

int num1;
char nleater;
printf("Enter a number:\n");
scanf("%d%c", &num1, &nleater);

or indeed this:

printf("Enter number sign number: ");
scanf("%d %c %d",&num1,&x,&num2);
printf("%d %c %d", num1, x, num2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文