我的CLL程序在c++中不会停止循环!为什么
my CLL program in C++ wont stop looping! why?
我使用一个CLL编写了一个程序,以便得到以下输出:
主菜单
- Create Books CLL
- 搜索(按图书名称)
<- 删除作者/gh>
输入您的选择:1
输入Book ID: 11223344
Enter Book Name: Programming c++
输入作者姓名:Jhone
节点创建
但是当我输入第一个选择时,我将输入书id,书名,然后它将开始循环不停止!我的程序有什么问题?
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int bid;
char book,author;
struct node *next;
}*last;
class circular_llist
{
public:
void create_node(int id,char bname,char aname);
void add_begin(int id,char bname,char aname);
void delete_element(char author);
void search_element(char name);
void display_list();
circular_llist()
{
last = NULL;
}
};
int main()
{
int choice, bid, position;
char name,author;
circular_llist cl;
while (1)
{
cout<<endl<<"Main Menu:"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at beginning"<<endl;
cout<<"3.Delete"<<endl;
cout<<"4.Search"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the Book ID: ";
cin>>bid;
cout<<"Enter the Book Name: ";
cin>>name;
cout<<"Enter the Book Author: ";
cin>>author;
cl.create_node(bid,name,author);
cout<<endl;
break;
case 2:
cout<<"Enter the Book ID: ";
cin>>bid;
cout<<"Enter the Book Name: ";
cin>>name;
cout<<"Enter the Book Author: ";
cin>>author;
cl.add_begin(bid,name,author);
cout<<endl;
break;
case 3:
if (last == NULL)
{
cout<<"List is empty, nothing to delete"<<endl;
break;
}
cout<<"Enter the Author name for deletion: ";
cin>>author;
cl.delete_element(author);
cout<<endl;
break;
case 4:
if (last == NULL)
{
cout<<"List Empty!! Can't search"<<endl;
break;
}
cout<<"Enter Book Name: ";
cin>>name;
cl.search_element(name);
cout<<endl;
break;
case 5:
cl.display_list();
break;
case 6:
exit(1);
}
}
return 0;
}
void circular_llist::create_node(int id,char bname,char aname)
{
struct node *temp;
temp = new(struct node);
temp->bid = id;
temp->book = bname;
temp->author = aname;
if (last == NULL)
{
last = temp;
temp->next = last;
}
else
{
temp->next = last->next;
last->next = temp;
last = temp;
}
}
void circular_llist::add_begin(int id,char bname,char aname)
{
if (last == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->bid = id;
temp->book = bname;
temp->author = aname;
temp->next = last->next;
last->next = temp;
}
void circular_llist::delete_element(char author)
{
struct node *temp, *s;
s = last->next;
if (last->next == last && last->author == author)
{
temp = last;
last = NULL;
free(temp);
return;
}
if (s->author == author) /*First Element Deletion*/
{
temp = s;
last->next = s->next;
free(temp);
return;
}
while (s->next != last)
{
/*Deletion of Element in between*/
if (s->next->author == author)
{
temp = s->next;
s->next = temp->next;
free(temp);
cout<<"Element "<<author;
cout<<" deleted from the list"<<endl;
return;
}
s = s->next;
}
if (s->next->author == author)
{
temp = s->next;
s->next = last->next;
free(temp);
last = s;
return;
}
cout<<"Element "<<author<<" not found in the list"<<endl;
}
void circular_llist::search_element(char name)
{
struct node *s;
int counter = 0;
s = last->next;
while (s != last)
{
counter++;
if (s->book == name)
{
cout<<"Element "<<name;
cout<<" found at position "<<counter<<endl;
return;
}
s = s->next;
}
if (s->book == name)
{
counter++;
cout<<"Element "<<name;
cout<<" found at position "<<counter<<endl;
return;
}
cout<<"Element "<<name<<" not found in the list"<<endl;
}
void circular_llist::display_list()
{
struct node *s;
if (last == NULL)
{
cout<<"List is empty, nothing to display"<<endl;
return;
}
s = last->next;
cout<<"Books List: "<<endl;
while (s != last)
{
cout<<s->bid<<"n"<<s->book<<"n"<<s->author<<"n"<<endl;
s = s->next;
}
cout<<s->bid<<"n"<<s->book<<"n"<<s->author<<"n"<<endl;
}
当您读取单个字符时(就像您对name
和author
所做的那样),您实际上只读取单个字符。您为这些字段输入多个字符,这意味着第一次输入(对name
)将读取名称的第一个字符,第二次输入(对author
)将读取名称的第二个字符。这将在输入缓冲区中留下相当多的字符,并且尝试读取除字符或字符串之外的任何内容都会失败,但是您不检查这些失败,因此您最终只是一遍又一遍地循环尝试读取例如数字并失败。
最简单的解决方案?首先使用std::string
读取字符串,然后继续使用std::getline
读取多词条目。
问题是如何输入作者和书名,例如,cin>>name
。这只从输入中读取第一个单词,其余的名称(如果有空格)留在输入缓冲区中。接下来,您尝试读取菜单选择数,但它将失败,因为缓冲区中的下一个字符是书名的第二个单词的第一个字符,或诸如此类。我建议你把cin >> name
换成std::getline
。
更新strong>
哦!我错过了你实际上只输入一个字符…
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 为什么在这个代码结束循环中没有得到结束
- 为什么我无法更改"set<set>"循环中的值<int>
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 为什么我不能在 while 循环中创建线程?
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么在递归中使用循环会产生意想不到的结果?
- 为什么在 while 循环中返回表达式不起作用
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 而循环:简单的除法程序输出零,不明白为什么
- 为什么我在尝试在单向链表中打印元素时会出现这个永无止境的循环
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么循环不会停止?C++
- 我的CLL程序在c++中不会停止循环!为什么
- (c++) For循环-为什么它这样做