0

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;
}

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??

please assist.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
user2670535
  • 129
  • 1
  • 2
  • 6
  • 1
    If you checked the `scanf()` return code for validation, you'd probably have found the problem by now. – WhozCraig Aug 22 '13 at 05:41
  • 1
    Aaaaaand kaboom. Use `fgets()` and **stay the hell away from `scanf()`** if you don't understand how it works. It's not a function for beginners. –  Aug 22 '13 at 05:41
  • possible duplicate of [scanf won't ask for input the second time](http://stackoverflow.com/questions/13372923/scanf-wont-ask-for-input-the-second-time) and of [Program doesn't wait for user input with scanf(“%c”,&yn);](http://stackoverflow.com/questions/8464620/program-doesnt-wait-for-user-input-with-scanfc-yn) and of [why does scanf not wait for user input after it fails one time?](http://stackoverflow.com/questions/11805414/why-does-scanf-not-wait-for-user-input-after-it-fails-one-time) –  Aug 22 '13 at 05:45
  • sir,but I used the same `scanf()` in seperate program to check , whether I use it properly or not , but it works correctly. – user2670535 Aug 22 '13 at 05:47
  • You either need to pay attention to your compiler's warnings, or you need to turn the warnings on, or you need to get a better compiler. Your `main()` function returns `int` (that's good — so often we see `void main()`, which is wrong), but ends with `return;` where it should be `return 0;`. – Jonathan Leffler Aug 22 '13 at 05:49
  • I'd suggest you stop fiddling around with the problems that can occur with mixing different methods of input, and just get yourself a tried and true line input function, such as http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 – paxdiablo Aug 22 '13 at 05:51
  • @JonathanLeffler : please suggest me some compiler , because my compiler did not give me a single warning while executing the above code. – user2670535 Aug 22 '13 at 06:42
  • @user2670535 MinGW is a good one , You have to make your compiler warn you its lazy :P try to compile with `-Wall` option . One google search would have made the life easier for you..... as for your question just use `getchar()` after `scanf()` or you better don't use it :) – 0decimal0 Aug 22 '13 at 08:48

3 Answers3

8

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

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.

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...

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().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

The problem is, the \r is sent to the second scanf, remaining from the first scanf.

And since you're reading only one char with scanf (which is BTW not recomended - use getchar() instead) it accepts the carriage return (\r). If you still want to use the second scanf, make a flush of the standard input: fflush(stdin) right after the first scanf().

DaMachk
  • 643
  • 4
  • 10
0

There is nothing wrong with scanf(), learning to use it is a good exercise in reading documentation, but it DOES work. Learning to use it is a good sample of what programmers do!

As a first guess, try these statements in your code:

char &choice1[2];  // allow for %c\0, but even this can be avoided
// etc.
scanf("%1s", &choice1);
if (tolower(choic1[0]) == 'y') { // etc. 

The %1s reads and discards white space, including new lines, and the 1 limits the number of chars that qualify as a string.

If this change does NOT work, let me know, and I will test/use your code to find a fix.

JackCColeman
  • 3,777
  • 1
  • 15
  • 21