簡體   English   中英

C ++程序在運行時停止工作

[英]c++ program stop working while running

請幫助解決此問題。 這是簡單的數據結構程序。在該程序中,首先用戶輸入他要輸入多少條記錄,然后再輸入記錄。 輸入記錄后,他輸入搜索和搜索數據。 現在我只輸入一個搜索第二號。 之后,我會結交他人。 當我運行它並達到搜索功能時,它將停止工作並因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();
}

在您的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()方法。 您必須在gets() fflush(stdin) 之前先進行fflush(stdin) ,否則某些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數組。

GDB可能對於獲取錯誤很有用。 啟動GDB並通過GDB控制台運行程序,在程序中執行相同的操作以獲取失敗,一旦程序失敗,引入命令“ bt”,它將報告有關最新執行的行的回溯,直到失敗為止,因此您將能夠找到錯誤

問題之一在下面的行中:

while(current->ccode!=c_code)

您將需要使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM