scanf() 不等待用户输入 [英] scanf() is not waiting for user input

查看:85
本文介绍了scanf() 不等待用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 c 中的双向链表制作树.我在该函数中使用递归调用,但不知何故它不起作用.我的代码是:

I am working on making tree using doubly linked list in c. I use recursive call in that function , but somehow it do not works. my code is :

struct node
{
    int data;
    struct node *right;
    struct node *left;
};

struct node* getNode()
{
    struct node *temp;
    temp= (struct node *)malloc(sizeof(struct node));
    temp->right=NULL;
    temp->left=NULL;
    return temp; 
}

在下面的函数中,我遇到了问题.

here in the below function I am getting the problem.

struct node* maketree()
{
    struct node *t;
    t=getNode(); 
    int value;
    char choice1='n',choice2='n';
    printf("\nenter the value to the node");
    scanf("%d",&value);
    t->data=value;
    printf("\nis there any left child??\n");
    scanf("%c",&choice1);               // I think here my problem is .
    if (choice1 == 'y')
    {
        t->left=maketree();   
    }

    printf("\nis there any right child??\n");
    scanf("%c",&choice2);
    if (choice2 == 'y' || choice2 == 'Y')
    {
        t->right=maketree();   

    }
    return t;
}

int main (void)
{
    struct node *t;
    t=maketree();
    return;
}

代码编译正确,但问题是,代码不等待我的选择(我使用 scanf() ,C 应该等到我输入终端的输入.)但输出是:

the code compiles properly , but the problem is , the code do not wait for my choice (I use scanf() , C should wait untill I enter the input to the terminal.) but the output is :

enter the value to the node4

is there any left child??

is there any right child??

请帮忙.

推荐答案

scanf("%d", &value) 在后面留下了一个换行符;scanf("%c", &choice1) 读取该换行符.

The scanf("%d", &value) left a newline behind; the scanf("%c", &choice1) reads that newline.

每次检查 scanf() 的返回值.并打印您阅读的内容以帮助您调试代码.确保您的程序达到了您认为的效果.

Check the return values from scanf() every time. And print what you read to help you debug your code. Make sure your program got what you think it got.

一个简单的解决方法是将第二个 scanf() 替换为 scanf(" %c", &choice1).格式字符串中的空白会占用空白,包括换行符,并读取第一个非空白字符.当然,它也会留下换行符.

A simple fix is to replace the second scanf() with scanf(" %c", &choice1). The blank in the format string eats up white space, including newlines, and reads the first non-blank character. Of course, it too leaves a newline behind.

正如评论中所暗示的那样,通常更容易控制:

As intimated in the comments, it is usually easier to control things with:

char line[4096];

if (fgets(line, sizeof(line), stdin) == 0)
    ...deal with EOF...

然后您可以使用 sscanf() 来解析该行.这种通用技术比直接使用 scanf() 更不容易出错;当您将整行包含在错误报告中时,连贯地报告错误也容易得多.当您每次调用 scanf() 时读取多次转换时,这一点更重要.

And then you can use sscanf() to parse the line. This general technique is quite a bit less error prone than using scanf() directly; it is also a lot easier to report errors coherently when you have the whole line to include in the error report. That matters more when you're reading multiple conversions per call to scanf().

这篇关于scanf() 不等待用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆