gpt4 book ai didi

c++ - 在添加新记录和访问记录时遇到问题

转载 作者:行者123 更新时间:2023-11-30 05:06:50 26 4
gpt4 key购买 nike

我一直在测试我的代码,我发现了一些我不确定如何解决的问题。似乎每当我添加一条新记录时,记录并没有真正被添加,我已经通过每当添加一条记录时测试了这一点,该程序将显示所有记录并且看起来它只是显示我硬编码到程序中的记录。我正在使用链表来执行此操作,但我不确定我是否正确实现了它。

编辑:我做了一些更改,剩下的问题之一是添加。当我对主程序进行更改时

Struct Student student1, tempStudent; The program went into an infinite loop when add was selected and the first entry was input.

.h文件:

#ifndef SLIST_H
#define SLIST_H
#include <string>
#include <iostream>
using namespace std;

class Student
{
public:
int ID;
string lastName;
string firstName;
string phoneNumber;
string major;
float GPA;
int year;
int month;
int date;
string address;
};

class Node
{
public:
struct Student data;
Node *next;
Node();
struct Student GetData();
void SetData(struct Student);
friend class LinkedList;
};

class LinkedList
{
public:
int length;
Node *currentPos;
Node *head;
Node *tail;
LinkedList();
~LinkedList();
int LengthIs();
void MakeEmpty();
void AddToTail(struct Student);
void AddToHead(struct Student);
int SearchByID(struct Student);
void DeleteFromHead();
void DeleteFromTail();
void Delete(int);
Node GetNext();
bool IsLast();
void Reset();
void PrintAll(int, string);
};


void LinkedList::AddToTail(struct Student item)
{
Node *ptr = new Node();
ptr->SetData(item);
if (length == 0)
{
tail = ptr;
head = ptr;
length++;
return;
}
tail->next = ptr;
tail = ptr;
length++;

}

void LinkedList::AddToHead(struct Student item)
{
Node *ptr = new Node;
ptr->SetData(item);
ptr->next = head;
head = ptr;
if (length == 0) tail = ptr;
length++;
}


#endif

主程序:

#include "slist.h"
#include <iostream>

using namespace std;

int main()
{
LinkedList students;
int choice = 1;
Student student1, tempStudent;
Node student1Node;
int ret;
student1.ID = 12345678;
student1.firstName = "Daenerys";
student1.lastName = "Targaryen";
student1.phoneNumber = "111-123-1234";
student1.major = "PScience";
student1.GPA = 3.9;
student1.year = 2000;
student1.month = 11;
student1.date = 30;
student1.address = "1234 Harpy Way Mereen, OK 74701";

students.AddToHead(student1);

while (choice != 6)
{
cout << "What would you like to do?" << endl;
cout << "1: Add a student record." << endl;
cout << "2: Remove a student record." << endl;
cout << "3: List all students." << endl;
cout << "4: List the student(s) by major or by ID." << endl;
cout << "5: Order the list." << endl;
cout << "6: Exit!" << endl << endl;
cout << "Make your choice: ";
cin >> choice;

switch (choice)
{
case 1:
cout << "Warning: there's a lot of data entry. Try to keep up with what you're entering."
<< endl;
cout << "Please enter in the student's ID, first name, last name, phone number and major ON SEPARATE LINES";
cout << endl;
cin >> tempStudent.ID;
cin >> tempStudent.firstName;
cin >> tempStudent.lastName;
cin >> tempStudent.phoneNumber;
cin.ignore();
getline(cin, tempStudent.major);
cout << "Almost done! Enter the student's gpa, birth year, birth month (IN DIGITS!!!), birth date, and address ON SEPARATE LINES.";
cout << endl;
cin >> tempStudent.GPA;
cin >> tempStudent.year;
cin >> tempStudent.month;
cin >> tempStudent.date;
cin.ignore();
getline(cin, tempStudent.address);

students.AddToHead(tempStudent);
cout << "Student entry added!" << endl;
students.PrintAll(-1, "");
break;

最佳答案

你输入错误,不是因为你去掉了错误的struct关键字

cin >> tempStudent.ID; 如果输入非整数会失败并导致无限循环。必须检查输入是否成功,如果失败则清除输入。

//cin >> tempStudent.ID; <- remove this line

while(true) //<- replace with this
{
cout << "id: ";
if( (cin >> tempStudent.ID) )
break;
cin.clear();
cin.ignore(0x1000, '\n');
}

您可能想要添加一个函数来对其他整数输入执行相同的输入错误检查。

cin.ignore()的其他情况改成

cin.clear();
cin.ignore(0x1000, '\n');

cin.clear() 如果您希望输入整数但用户不小心输入了文本,则推荐使用。

关于c++ - 在添加新记录和访问记录时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47747365/

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