gpt4 book ai didi

c++程序在运行时停止工作

转载 作者:太空宇宙 更新时间:2023-11-04 06:13:53 27 4
gpt4 key购买 nike

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

enter image description here其次,当第一次输入数据时,当循环第一次运行时它不接受名称输入,而不是输入名称。

enter image description here

请提前致谢。

#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() 方法。你必须 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 数组。

关于c++程序在运行时停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50234868/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com