【问题标题】:c++ program stop working while runningc++程序在运行时停止工作
【发布时间】:2018-10-18 11:30:35
【问题描述】:

请帮助解决这个问题。 这是一个简单的数据结构程序 在这个程序中,用户首先输入他想输入多少条记录,然后输入记录。输入记录后,他输入搜索和搜索数据。现在我只为搜索 No2 输入一个。在那之后,我会做其他人。 当我运行它并到达搜索功能时,它停止工作并因 Windows 错误而关闭。

其次,第一次输入数据时,循环第一次运行时不输入名称,而不是输入名称。

请帮助提前谢谢。

#include<conio.h>
#include<iostream>
#include<fstream>
#include<Windows.h>
#include<dos.h>
#include<cctype>
#include<sstream>
#include<string>

using namespace std;

bool check = true;
struct node    //structure of node //
{
    char name[20];
    char ccode[20];
    int marks;
    float cgpa;
    node *next;
}*head,*lastptr;

void add()    //Adds record of student//
{
    node *p;
    p=new node;
    cout<<"Enter name of student:"<<endl;
    gets(p->name);
    fflush(stdin);
    cout<<"Enter cource code:"<<endl;
    gets(p->ccode);
    fflush(stdin);
    cout<<"Enter Marks of student:"<<endl;
    cin>>p->marks;
    fflush(stdin);
    cout<<"Enter CGPA of student:"<<endl;
    cin>>p->cgpa;
    fflush(stdin);
    p->next=NULL;

    if(check)
    {
        head = p;
        lastptr = p;
        check = false;
    }
    else
    {
        lastptr->next=p;
        lastptr=p;
    }
    cout<<endl<<"Student's information saved Successfully";
    getch();
}

void search()   //searches record of student//
{
    node *prev=NULL;
    node *current=NULL;
    char c_code[20];
    cout<<"Enter Roll Number to search:"<<endl;
    //c_code=getch();
    gets(c_code);
    fflush(stdin);
    cout<<"hkjhk"<<c_code;
    prev=head;
    current=head;
    while(current->ccode!=c_code)
    {
        prev=current;
        current=current->next;
    }
    cout<<"\nname: ";
    puts(current->name);
    cout<<"\n Cource Code:";
    cout<<current->ccode;
    cout<<"\nMarks:";
    cout<<current->marks;
    cout<<"\nCGPA:";
    cout<<current->cgpa;
    getch();
}


int main()
{
    int x;
    system("cls");
    cout<<"How many students you want to enter"<<endl;
    cin>>x;
    while(x>0){
        add();
        x--;
    }

    cout<<"\nwhat type of search you want to search select choice 1 ,2 3 \n";
    int choice;
    cout<<"1 search all student by cource code \n";
    cout<<"2 search all student by marks \n";
    cout<<"3 search all student by cgpa \n"; 
    cin>>choice; 
    if(choice==1)
    {
        system("cls");
        add();
    }
    else if(choice==2)
    {
        cout<<"fhghgf";
        system("cls");
        search();
    }
    else
    {
    }
    getch();
}

【问题讨论】:

  • 在调试器中运行,找出崩溃的地方。
  • 可能是未捕获的异常或空指针。您可能可以从应用程序数据文件夹中挖掘崩溃报告,但我同意最好的办法是在调试器下运行。 (我猜你没有安装它,因为它没有在“停止工作”对话框中为你提供调试选项?你可以例如免费获得 Visual Studio Community。)
  • 如何在调试器上运行
  • 我正在使用 dev c++ 编辑器,但不了解调试器,请帮助我
  • 您为什么要混合使用cingetchfflush 之类的输入法?你在学习 C++ 吗?这是别人你的吗?

标签: c++ c data-structures linked-list


【解决方案1】:

在您的 search() 方法中有循环:

while(current->ccode!=c_code)
{
   prev=current;
   current=current->next;
}

找不到代码时发生了什么?您在链表结束后继续。 另外,你需要比较内容,而不是地址,所以你必须使用strcmp

解决问题应该是:

while(current && strcmp (current->ccode, c_code))
{
   prev=current;
   current=current->next;
}

另一个问题是gets() 方法。你必须 fflush(stdin) BEFORE gets() 否则一些 gets() 将只读取前一个输入的未读 CR(行尾)。

如果你想代替fflush(stdin),你可以使用一些eatwhites 方法,像这样:

istream& eatwhites(istream& stream)
{
    // to define white spaces manually:
    //const string skip=" \t\r\n";
    //while(string::npos != skip.find(stream.peek())){
    //   stream.ignore();
    //}

    //or just use isspace:
    while(isspace(stream.peek())){
        stream.ignore();
    }
    return stream;
}

gets()之前调用它:eatwhites(stdin); 此方法跳过白色字符并将读取放在数据的开头,这样您就不会读取前一个输入留下的空行...

另一件事:您最好使用 std::getline(); 并使用 std::string 而不是 char 数组。

【讨论】:

  • 它现在可以工作,但是如果两个学生具有相同的课程代码,那么它只会显示一个记录,然后它会在显示 1stttt 后停止工作
【解决方案2】:

GDB 可能有助于获取错误。启动 GDB 并通过 GDB 控制台运行您的程序,在您的程序中执行相同的操作以获取失败,一旦程序失败,引入命令“bt”,它将报告有关最新执行行的回溯,直到失败,所以您将能够找到错误

【讨论】:

    【解决方案3】:

    其中一个问题在下面一行:

    while(current->ccode!=c_code)
    

    你需要使用:

    while (strcmp (current->ccode, c_code))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多