gpt4 book ai didi

c++ - 无法使 strstr() 工作

转载 作者:行者123 更新时间:2023-11-28 00:09:00 25 4
gpt4 key购买 nike

我有一个家庭作业,我在一个区域根本无法正确工作,特别是在我尝试比较字符串的地方。这是作业:

You will write a program that prompts the user for student names, ages, gpas, and graduation dates. Your program will then read in all the student information and store them into a linked list. The program will then print the names of the students. Next, the program will prompt the user for a string. The program will print the complete information for each student containing the string in the name.

这是我所拥有的:

#include <iostream>
#include <cstring>
using namespace std;
const char NAME_SIZE = 50;
struct StudentInfo
{
char studentName[NAME_SIZE];
int age;
double gpa;
char graduationSemester[3];
StudentInfo *next;
};
void displayStudentNames(StudentInfo *top);
void displayStudentInfo(StudentInfo *top);

int main(){
StudentInfo *top = 0;
cout << "Please enter the students. Enter the name, age, gpa, and semester of graduation (e.g. F13)." << endl;
cout << "Enter an empty name to stop." << endl << endl;
bool done = false;
while(!done){
char nameBuffer[NAME_SIZE];
char graduationBuffer[3];
cin.getline(nameBuffer, NAME_SIZE);
if(nameBuffer[0] != 0){
StudentInfo *temp = new StudentInfo;
strcpy(temp->studentName, nameBuffer);
cin >> temp->age;
cin >> temp->gpa;
cin.getline(graduationBuffer, 3);
strcpy(temp->graduationSemester, graduationBuffer);
cin.ignore(80, '\n');
temp->next = top;
top = temp;
}else{
displayStudentNames(top);
displayStudentInfo(top);
done = true;
}
}
}

void displayStudentNames(StudentInfo *top){
cout << "Here are the students that you entered: " << endl << endl;
while(top){
cout << top->studentName << endl;
top = top->next;
}
cout << endl;
}

void displayStudentInfo(StudentInfo *top){
char name[NAME_SIZE];
do{
cout << "Which students do you want? ";
cin.getline(name, NAME_SIZE);
const char *str = top->studentName;
const char *substr = name;
const char *index = str;
while((index = strstr(index,substr)) != NULL){
cout << "Name: " << top->studentName << ", Age: " << top->age << ", GPA: " << top->gpa << ", Graduations Date: " << top->graduationSemester;
index++;
}
}while(name[0] != 0);
}

我的问题出在 displayStudentInfo 函数中,我无法使其正常工作。我尝试过很多不同的东西,这只是我尝试过的最新的东西。但是在程序前面创建链表后,我们应该输入一个从字母到全名的字符串,并在列表中的任何位置找到它,然后打印出该特定名称的信息。

eta:我的链表向后存储结构也有问题?它也没有存储我的毕业日期,或者当我尝试打印它们时出现问题,因为它们打印为空白。

最佳答案

您的代码问题很明显。您只是检查列表的头部,而不是遍历列表。

这是有问题的部分(假设每个函数调用应该返回 1 名学生的信息):

void displayStudentInfo(StudentInfo *top){
char name[NAME_SIZE];

//Addes 'node' variable for simplicity, you can use 'top' itself
StudentInfo *node = top;

cout << "Which students do you want? ";
cin.getline(name, NAME_SIZE);
do{
// Check if name is correct
// "Entered name should be at start of field"
// You should use == not =
if(node->studentName == strstr(node->studentName, name){
cout << "Name: " << top->studentName << ", Age: " << top->age << ", GPA: " << top->gpa << ", Graduations Date: " << top->graduationSemester;
}
// Traverse in list
node = node->next;
// Until reach end of list
}while(node != NULL);
}

关于c++ - 无法使 strstr() 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34028754/

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