【问题标题】:C++ code crashes and has an infinite loopC++ 代码崩溃并出现无限循环
【发布时间】:2020-04-09 05:48:42
【问题描述】:

这段代码有几个问题;

首先,如果我运行它,然后我选择 PUSH 并输入两个名字,例如“Emily Glassberg”,它会进入 infinite循环,但如果我只输入一个名字,比如“Michael”,它会很好用。Problem picture.

其次,当我运行代码后

  1. 选择“PUSH” - 输入一个名称-
  2. 然后选择“删除”
  3. 然后选择“PUSH” - 并再次输入一个名称 - 程序崩溃
    如果我仅在输入任何名称之前选择“删除”,它甚至会崩溃
    Problem 2.1 picture
    Problem 2.2 picture
#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;

struct Queue
{
    struct node
    {
        node *next;
        string name;
    };

    node *head;
    int front=-1,rare=-1;

    void push(string x)
    {
        if (rare < 0 )
        {
            head =new node;
            head->next=NULL;
            head->name=x;
            rare ++;
        }
        else
        {
            node *temp,*temp1;
            temp=head;

            while(temp->next != NULL){temp=temp->next;}

            temp1=new node;
            temp->next=temp1;
            temp1->next=NULL;
            temp1->name=x;
        }
    }

    void display()
    {
        node *temp;
        temp=head;
        if (rare < 0)
        {
            cout <<"Queue under flow";
            return;
        }
        cout<<"\nthe queue is: \n\n";
        while(temp != NULL)
        {
            cout <<temp->name<<endl;
            temp=temp->next;
        }
    }
    void pop()
    {

        if( rare < 0)
        {
            cout <<"Queue under flow\n";
            return;
        }
        if(front == rare)
        {
            front = rare =-1;
            head=NULL;
            return;
        }
        front++;
        head=head->next;
    }

    Queue Delete(Queue q){
        node* temp = head;
        while (head != nullptr){
            head = head->next;
            delete temp;
            temp = head;
        }
        head  = nullptr;
    }

};


main()
{
    Queue q1;
    string x;
    int ch;
    while(true)
    {
        cout<<"1.PUSH\n2.POP\n3.DISPLAY\n4.DELETE\n5.EXIT\nenter Ur choice:\n";
        cin >> ch;

        switch(ch)
        {
        case 1:
            cout <<"plz,enter the name \n";
            cin >> x;
            q1.push(x);
            cout<<"\n";
            break;

        case 2:
            q1.pop();
            break;
        case 3:
            q1.display();
            cout<<"\n";
            break;
        case 4:
            q1.Delete(q1);
            break;
        case 5:
            exit(0);
        default :
            cout<<"error selection.plz,try again\n";
        }
    }
    return (0);
}

【问题讨论】:

  • 调试时尝试制作一个示例,该示例一次暴露一个错误并且不需要输入。这使您能够以尽可能少的干扰进行调试。使用minimal reproducible example 作为灵感。

标签: c++ linked-list crash queue infinite-loop


【解决方案1】:
  1. 第一次没有 PUSH 的删除不起作用,因为头指针未初始化,然后行:head = head->next 导致错误。您必须在构造函数中将 head 初始化为 null,并且在 Delete 中您必须验证 head 是否与 null 不同。

  2. 在推送中:

    while(temp->next != NULL) { temp=temp->next; }

    当 temp=NULL 时条件 temp->next 崩溃,因为 temp 为空。通过 while(temp!=null) 或通过以下方式更改条件:while(temp != null && temp->next!=null)

  3. cin >> x。 当您输入“Emily Glassberg”时,cin 读取 Emily 并返回。下一个循环 cin > ch 返回“Glassberg”然后不是有效选项并无限循环。在 PUSH 选项中使用 cin.getline() 而不是 cin >> x.

  4. 在 Delete 中,您必须将 -1 设置为 bare,因为下一次 PUSH 磁头未初始化。此外,您必须以递归形式删除。

【讨论】:

  • 嘿兄弟,感谢您的帮助,对于第 3 点,我添加了这一行 getline(cin,x);在 cin>>x 行之后。在我运行代码并选择 PUSH 之后,输入两个名称,例如 "Emily Glassberg" ,然后选择 DISPLAY 只出现第二个名称。
  • 删除行:cin >> x;
  • 我删了,死循环又回来了。
  • 替换行:cin >> ch;通过这行: while (true) { ch = _getch(); ch = ch - '0'; if (ch >= 1 && ch
【解决方案2】:

我认为您的问题是,在“Michael”这个名字中没有空格,但在“Emily Glassberg”中有空格。我相信 std::cin 很难处理这个问题,所以你应该使用 std::getline 并将 ch 更改为 std::string (你必须包括在内)。代码需要看起来像这样才能正常工作:

ch.clear();
int chInt;
while(ch.size() == 0) {
    std::getline(std::cin, ch);
}
chInt = std::stoi(ch);
//Handle the input here

您需要循环,因为无论出于何种原因,std::getline 往往只尝试从用户那里获取一次输入,然后继续前进,因此通过循环直到用户输入某些内容,我们可以确保将始终采用正确的输入。事先清除字符串也很重要,这样我们就不会在以后的任何输入收集器中自动使用最后一个输入。最后,chInt = std::stoi(ch); 行转换我们刚刚从用户那里收集的字符串,将其转换为 int,然后将其分配给 chInt(因此在您的 switch 语句中,您将使用 chInt 而不是 ch)。实施此修复可能会或可能不会帮助解决问题的后半部分。

【讨论】:

  • 感谢帮助,我添加了这一行 getline(cin,x);在 cin>>x 行之后。在我运行代码并选择 PUSH 之后,输入两个名称,例如 "Emily Glassberg" ,然后选择 DISPLAY 只出现第二个名称。
猜你喜欢
  • 2018-08-01
  • 2014-01-31
  • 2016-06-19
  • 1970-01-01
  • 2018-08-27
  • 2023-01-31
  • 2014-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多