Unable to read input from keyboard, in below C code, for a BST.

Joined
Feb 12, 2023
Messages
1
Reaction score
0
The below C-code for implementation of a BST, is unable to read input integers, in the main()'s first case (#1), and exits before reading any.

This is shown in the output, shown after the below program.
The same output is obtained on onlinegdb.com, and on Cygwin (installed over Windows):
C:
// C program to implement a Binary-search tree
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
 
int nodecount=0;
// BST node
struct Node {
    int key;
    struct Node* left;
    struct Node* right;
};
 
struct Node *root=0;
 
// Function to create a new node
struct Node* createNode(int key)
{
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->key = key;
    node->left = NULL;
    node->right = NULL;
    return node;
}
 
// Function to insert a key into BST
struct Node* insert(struct Node* node, int key, int call_no)
{
    printf("\n<I> call_no:%d <> key:%d", call_no, key);
    if (root==NULL)
       printf("\t <> ROOT is NULL <> Creating root");
    // 1. Perform standard BST insertion
    if (node == NULL)
    {
        return createNode(key);
    }
 
    if (key < node->key)
    {
        node->left = insert(node->left, key, call_no+1);
     }
    else if (key > node->key)
    {
        node->right = insert(node->right, key, call_no+1);
     }
    else // Equal keys are not allowed in BST
    {
        return node;
    }
   
    // Return the (unchanged) node pointer
    return node;
}
 
 
int main()
{
   int option, val;
   int i=0, n, c;
   int countval=0, *count = &countval;
   
   struct Node *node;
   do
   {
      printf("\n\n ******MAIN MENU******* \n");
      printf("\n 1. Insert Element");
      printf("\n 2. Exit");
      printf("\n\n Enter your option : ");
     
      scanf("%d", &option);
      switch(option)
      {
         case 1:
            char line[1024];        
            i = 0;        
            nodecount = 0;
            printf("Enter space-separated integers to insert: ");
           
            if (fgets(line, sizeof(line), stdin) != NULL) {
               char *ptr = strtok(line, " ");
               while (ptr != NULL)
               {
                  if (sscanf(ptr, "%d", &val) == 1){
                     root = insert(root, val, 0);
                     i++;
                  }
                  else {
                     printf("Error reading input.");
                     break;
                  }
                  printf("You entered: %s", line);
               }
 
               n = i;
               printf("\n\nNumber of new key-values = %d, number of new nodes = %d\n", n, nodecount);
            }
            else
            {
               printf("Input error.\n");
            }
           
            break;
         case 2:
            exit(0);
      }
    }while(option!=0);
    getchar();
    return 0;
}

Output:
Code:
HP@DESKTOP-G158GS6 ~/ists_
$ gcc sample.c -o sample
 
HP@DESKTOP-G158GS6 ~/ists_
$ ./sample
 
 
 ******MAIN MENU*******
 
 1. Insert Element
 2. Exit
 
 Enter your option : 1
Enter space-separated integers to insert: Error reading input.
 
Number of new key-values = 0, number of new nodes = 0
 
 
 ******MAIN MENU*******
 
 1. Insert Element
 2. Exit
 
 Enter your option :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,347
Messages
2,571,447
Members
48,794
Latest member
Engareh

Latest Threads

Top