当 getline 用于获取整个字符串时,程序崩溃



制作一个C ++程序以使用哈希实现字典,但是在尝试将含义的输入作为整个句子时,程序崩溃了

#include <iostream>
using namespace std;
const int MAX=10;
class dictionary;
class node
{
string key,value;
node *next;
public:
friend class dictionary;
node()
{
next=NULL;
}
node(string key,string value)
{
this->key=key;
this->value=value;
next=NULL;
}
};
class dictionary
{
node *head[MAX];
public:
dictionary()
{
for(int i=0;i<MAX;i++)
head[i]=NULL;
}
int hashf(string word);
void insert(string,string);
void find(string word);
bool deleteWord(string word);
void display();
};

哈希函数

int dictionary::hashf(string word)
{
int asciiSum=0;
for(int i=0;i<word.length();i++)
{
asciiSum=asciiSum+word[i];
}
return (asciiSum%10);
}

在字典函数中查找单词

void dictionary::find(string word)
{
int index=hashf(word);
int flag=0;
node *start=head[index];
while(start!=NULL)
{
if(start->key==word)
{
flag=1;
break;
}
start=start->next;
}
if(flag==1)
cout<<"Word Is  present.";
else
cout<<"Word Is not present.";
}

插入字典功能

void dictionary::insert(string word,string meaning)
{
int index=hashf(word);
node *p=new node(word,meaning);
if(head[index]==NULL)
{
head[index]=p;
}
else
{
node *start=head[index];
while(start->next!=NULL)
start=start->next;
start->next=p;
}
cout<<endl<<word<<" inserted into dictionary at index"<<index;
}

从字典功能中删除

bool dictionary::deleteWord(string word)
{
int index=hashf(word);
node *tmp=head[index];
node *par=head[index];
if(tmp==NULL) //if no word is present at that index
{
return false;
}
if(tmp->key==word && tmp->next==NULL)//only one word is present
{
head[index]=NULL;
delete tmp;
return true;
}
//tmp=tmp->next;
while(tmp->key!=word && tmp->next!=NULL)
{
par=tmp;
tmp=tmp->next;
}
if(tmp->key==word&&tmp->next!=NULL)
{
if(par->key==tmp->key)
{
head[index]=tmp->next;
}
else
{
par->next=tmp->next;
tmp->next=NULL;
}
delete tmp;
return true;
}
else //delete at end
{
par->next=NULL;
tmp->next=NULL;
delete tmp;
return true;
}
return false;
}

显示整个字典功能

void dictionary:: display()
{
cout<<"nIndext Keyt Value";
for(int i=0;i<10;i++)
{
node *start=head[i];
if(start==NULL)
cout<<"n";
while(start!=NULL)
{
cout<<"n:"<<i<<"t"<<start->key <<"t "<<start->value;
start=start->next;
}
}
}

主呼叫

int main() {
dictionary oxford;
int choice;
string word;
string meaning;
char ch='y';
while(ch=='y')
{
cout<<"n**** OXFORD DICTIONARY ****n"
<<"1.Insert Wordn"
<<"2.Find Wordn"
<<"3.Delete Wordn"
<<"4.Displayn"
<<"Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter Word: ";
cin>>word;
cout<<"Enter Meaning: ";
getline(cin,meaning);
oxford.insert(word,meaning);
break;
case 2:
cout<<"Enter Word to Search: ";
cin>>word;
oxford.find(word);
break;
case 3:
cout<<"Enter Word to Delete: ";
cin>>word;
if(oxford.deleteWord(word))
cout<<" Word is deleted.";
else
{
cout<<"nFailed to delete "<<word;
}
break;
case 4:
cout<<"***Oxford Dictionary***";
oxford.display();
break;
default:
cout<<"nWrong Choice.";
}
cout<<"nDo you want to continue(y/n)";
cin>>ch;
if(ch=='y')
{
continue;
}
else if(ch=='n')
{
cout<<"nThank you for using our dictionary";
break;
}
}
return 0;
}

输出

**** OXFORD DICTIONARY ****
1.Insert Word
2.Find Word
3.Delete Word
4.Display
Enter Your Choice :1
Enter Word: john
Enter Meaning: 
john inserted into dictionary at index1
Do you want to continue(y/n)

好消息:

它不会崩溃。它完全按照您编程的方式执行;)

溶液

std::cin >> word之后插入一个std::cin.ignore()

问题的原因

读取word后,您不会忽略输入。std::cin >> word将读取字符"J"、"o"、"h"和"n",然后在遇到输入流中的第一个空格时停止,在您的情况下是新行(这是什么取决于操作系统,让我们假设它只是(。

但是""仍在输入流中,并且是下一个要读取的字符!

因此,std::getline开始读取,立即输入""并停止,返回零个字符。

最新更新