在添加新记录和访问记录时有一个问题
Having an issue in adding new records and accessing records
我一直在测试我的代码,发现一些我不确定如何修复的问题。看来,每当我添加新记录时,并没有真正添加记录,我都会通过每当添加记录时就对此进行了测试,该程序就是显示所有记录显示了我硬编码为程序的记录。我正在使用链接列表来进行此操作,但我不确定我是否正确实施它。
编辑:我已经进行了一些更改,其余问题之一就是添加。当我更改主要程序
时struct student student1,tempstudent; 选择添加时,该程序进入了无限循环,并且第一个条目是输入的。
.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()
如果您期望整数输入并且用户意外进入文本。
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 有没有可能有一个只有ADL才能找到的非好友功能
- 我有一个对象,它将在整个程序的持续时间内实例化,但一个类成员不会,我应该动态分配它吗?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 在学习数据结构之前对STL有一个了解是好的吗?
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 是否可以在C++中有一个"generic"模板参数,该参数可以是非类型模板参数或类型?
- 我的输出中有一个额外的 0,为什么会这样
- 节点是否为空,即使它有一个值?
- 将数组信息存储到 c++ 向量中有一个"Access violation reading location"
- 在 Stream C++ 文本之前有一个额外的换行符
- 我可以有一个 ELI5 作为参考和指针以及何时使用它们吗?
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 我有一个类,它创建了另一个类的实例.如何将变量通过第一个类传递到第二个类的实例化中?
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 是否有一个工具来记录代码执行
- 如果我有一个用户记录在一行内如何找到登录的数据