0

I want to make a simple spell check program. This program return Correct if the word is found in text file or Incorrect if the word is not present in text file. For this I am following this appproach:-

1) Create a text file with 1 word per line in alphabetical order.( just like dictionary words without meanings but only 1 word per line)

**2)**Insert the words from text file into Binary Search tree and check whether the word is present or not upon Input from user.

But, I am not sure how do I get the words from text file into BST.

#include <iostream>
#include <fstream>
#include <string>



using namespace std;



struct node
{
    struct node *left;
    struct node *right;
    char word[100];
}

struct node *root=NULL;


void create()
{
    struct node *newnode;
    newnode=(struct node *)malloc(sizeof(struct node));
    char str[100];
    for (i=0;i<100;i++){
    cout<< a[i]<<endl;
    }
    strcpy(newnode->word,str);
    newnode->left=NULL;
    newnode->right=NULL;
    root=newnode;
}   


void insert()
{
    struct node *newnode,*temp,*parent;
    newnode=(struct node *)malloc(sizeof(struct node));
    char str[100];
    cout<<"enter word to be inserted \n";
    cin>>a[i+1];
    strcpy(newnode->word,str);
    newnode->left=NULL;
    newnode->right=NULL;
    temp=root;
    while(temp!=NULL)
    {
        parent=temp;
        if(strcmp(temp->word,str)<0)
            temp=temp->left;
        else
            temp=temp->right;
    }
    if(strcmp(parent->word,str)<0)
        parent->left=newnode;
    else
        parent->right=newnode;
}



int main() {

    ifstream inFile;
    inFile.open("test.txt");


    if(inFile.fail()) {

    cerr << "Error opening file"<< endl ;

    exit(1);
    }

    string x;
    string a[100];
    int count=0,i=0;
    string str;

    while( !inFile.eof()) {
        inFile >> x;
            a[i]=x;
            count++;
            i++;        
            }

    for (i=0;i<100;i++){
    cout<< a[i]<<endl;
    }

    create();
    insert();

    return 0;


}

Please help me. Thanks in advance!


***

I am able to successfully import the text file into program but not into BST.

I created two classes for Root node and inserting the elements from text file to BST but couldnot do it. 

I dont have idea of how to do it. So please help me.
***
mohinish
  • 19
  • 5
  • Please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then edit your question to tell us what problems you have with the code you show. How does it work? How should it work? What attempts of debugging have you done? Etc. – Some programmer dude Oct 19 '19 at 07:54
  • On an unrelated note, please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Oct 19 '19 at 07:55
  • 1. Variable `a` is nowhere to be found (declared) in your code. 2. You really ought to do `char str[100] = {0};`. 3. You really ought to run `for (i=0;i<99;i++)`. – goodvibration Oct 19 '19 at 07:58
  • As a hint about what I ***guess*** your problem might be, think about *scope* and where your variables are defined and how to pass variables to functions. You probably should take a couple of steps back and go back to your text-book, tutorial or class-notes to figure those things out first. – Some programmer dude Oct 19 '19 at 07:59
  • Warning: even when it works, this isn't going to work well. Inserting words in alphabetical order without rebalancing the tree at all is going to give a completely imbalanced tree. Might I advise a binary search in an array instead? – Jerry Coffin Oct 19 '19 at 21:04

0 Answers0