我的CLL程序在c++中不会停止循环!为什么

my CLL program in C++ wont stop looping! why?

本文关键字:循环 为什么 程序 CLL c++ 我的      更新时间:2023-10-16

我使用一个CLL编写了一个程序,以便得到以下输出:

主菜单

  1. Create Books CLL
  2. 搜索(按图书名称)
  3. <
  4. 删除作者/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;
}

当您读取单个字符时(就像您对nameauthor所做的那样),您实际上只读取单个字符。您为这些字段输入多个字符,这意味着第一次输入(对name)将读取名称的第一个字符,第二次输入(对author)将读取名称的第二个字符。这将在输入缓冲区中留下相当多的字符,并且尝试读取除字符或字符串之外的任何内容都会失败,但是您不检查这些失败,因此您最终只是一遍又一遍地循环尝试读取例如数字并失败。

最简单的解决方案?首先使用std::string读取字符串,然后继续使用std::getline读取多词条目。

问题是如何输入作者和书名,例如,cin>>name。这只从输入中读取第一个单词,其余的名称(如果有空格)留在输入缓冲区中。接下来,您尝试读取菜单选择数,但它将失败,因为缓冲区中的下一个字符是书名的第二个单词的第一个字符,或诸如此类。我建议你把cin >> name换成std::getline

哦!我错过了你实际上只输入一个字符…