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

时间:2015-10-26 07:54:46

标签: c++ input iostream

我使用单个CLL编写了一个程序,以便输出以下内容:

  

主菜单

     
      
  1. 创建图书CLL
  2.   
  3. 添加图书
  4.   
  5. 搜索(按书名)
  6.   
  7. 删除作者
  8.         

    输入您的选择:1

         

    输入图书ID:11223344

         

    输入书名:编程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;
}

2 个答案:

答案 0 :(得分:2)

当您阅读单个字符时(就像使用nameauthor一样),您实际上只读取单个字符。并为这些字段输入多个字符,这意味着第一个输入(到name)将读取名称的第一个字符,第二个输入(到author)将读取第二个字符名称的。这将在输入缓冲区中留下相当多的字符,并且尝试读取除字符或字符串之外的任何字符都将失败,但是您不会检查这些故障,因此您最终只需要反复循环尝试读取,例如一个数字而且失败了。

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

答案 1 :(得分:0)

问题在于您输入作者和书名的方式,例如cin>>name。这只读取输入中的第一个单词,名称的其余部分(如果有空格)保留在输入缓冲区中。接下来,您尝试读取菜单选项编号,它将失败,因为缓冲区中的下一个字符是书名称的第二个单词的第一个字符,或者类似。我建议您将cin >> name替换为std::getline

<强>更新

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