获取字符串输入崩溃程序

时间:2017-05-21 13:19:57

标签: c++ string dictionary nodes getline

我这里有很长的代码。我正在尝试为dnode获取字符串输入' word'和'意思'。 问题是,在getline语句中,当提示用户接收输入时,程序终止。只需崩溃并显示错误消息:

'file'.exe has stopped working. And, process exited after 'some' seconds with a return value 'somevalue'

以下是代码:

#include <iostream>
#include<stdlib.h>
#include <stdio.h>
#include <string.h>
#include<string>


using namespace std;

void create();
void add();
//void display();
void search();



struct dnode
{
    string word= " " ;
    string meaning = " ";
    dnode *right;
    dnode *left;
};

dnode *root =NULL;

string searchword= " " ;
string addedword= " " ;
string str;
int x;


int main()
{
    if (root==NULL)
{
    create();
    main();
}

else
{
    cout << "enter:";
    cin >> x;
    if (x==1)
    {
        add();
        main();
    }

    if (x==2)
    {
        search();
        main();
    }
 }
}

void create()
{
struct dnode *newnode = (dnode*) malloc (sizeof(struct dnode));

cout << "Enter the first word: " << endl;
cin >> newnode->word;
getline (cin, newnode->word);
cout << "Added";
cout << "Enter the meaning of " << newnode->word << endl;
getline (cin, newnode->meaning);
newnode->left=NULL;
newnode->right=NULL;
root=newnode;   
}

void add()
{
if (root==NULL)
{
    struct dnode *location=NULL;
    struct dnode *parent=NULL;
    struct dnode *temp=NULL;
    struct dnode *save=NULL;
    create();
}

else
{
    struct dnode *location=NULL;
    struct dnode *parent=NULL;
    struct dnode *temp=NULL;
    struct dnode *save=NULL;


    struct dnode *newnode = (dnode*) malloc (sizeof(struct dnode));
    cout << "Enter the word to add: " << endl;
    getline (cin, newnode->word);
    cout<< "Meaning: " << endl;
    getline (cin, newnode->meaning);
    newnode->left=NULL;
    newnode->right=NULL;

if ((root->word.compare(newnode->word)==0))
{
    location=root;
    parent=NULL;
    cout << addedword << "Word already exists" << endl;
    cout<< "Meaning: " << root->meaning << endl;
    main();
}

else if ((root->word.compare(newnode->word)< 0))
{
    temp=root->left;
    save=root;
}

else if ((root->word.compare(newnode->word)> 0))
{
    temp= root->right;
    save=root;
}

while (temp!=NULL)
{
    if ((newnode->word.compare(temp->word)==0))
    {
        location= temp;
        parent=save;

    }

    else if (newnode->word.compare(temp->word)<0)
    {
        save=temp;
        temp=temp->left;
    }

    else if (newnode->word.compare(temp->word)>0)
    {
        save=temp;
        temp=temp->right;
    }
}

location=NULL;
parent=save;



if ((newnode->word.compare(parent->word)<0))
{
    parent->left=newnode;
}

else if (newnode->word.compare(parent->word)>0)
{
    parent->right=newnode;
}

main(); 


}


}

void search()
{
int f=0; 
struct dnode *location=NULL;
struct dnode *parent=NULL;
struct dnode *temp=root;
struct dnode *save=NULL;

cout << "Enter the word to find: " << endl;
cin >> searchword;

if (root==NULL)
{
    cout << "Dictionary does not exist" << endl;

    main();
}

if ((root->word.compare(searchword)==0))
{
    location=root;
    parent=NULL;
    cout << searchword << " found" << endl;
    cout << "Meaning: " << root->meaning << endl;
    f=1;

    main();
}

else if ((searchword.compare(root->word)< 0))
{
    temp=root->left;
    save=root;
}

else if ((searchword.compare(root->word)> 0))
{
    temp= root->right;
    save=root;
}

while (temp!=NULL)
{
    if ((searchword.compare(temp->word)==0))
    {
        location= temp;
        parent=save;
        cout << searchword<< " found" << endl;
        cout << "Meaning: " << temp->meaning << endl;
        f=1;
        main();

    }

    else if ((searchword.compare(temp->word)<0))
    {
        save=temp;
        temp=temp->left;
    }

    else if ((searchword.compare(temp->word)>0))
    {
        save=temp;
        temp=temp->right;
    }
}

if (f==0)
{
    cout << "Word not in dictionary" << endl;
    main();
}


}

1 个答案:

答案 0 :(得分:1)

不要在C ++代码中使用mallocmalloc分配未初始化的内存,因此,例如,string永远不会在您的结构中构造。在C ++中,使用new

此外,您通过递归调用main来遍历整个地方。如果你这样做,你最终会耗尽堆栈空间。