我的程序没有响应。当我运行该程序时,它停留在空白屏幕上,没有错误也没有输出。
我不知道是什么错误。如何从结构内部读取枚举器。以下是错误的。请帮忙。
infile >> gRecs[i].Subjects[j].Status;
Error:
C:\Users\PC\Desktop\a1\ass1.cpp [Error] no match for 'operator>>'
(operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and
'StatusType')
这是“.txt”文件。
7453842
Gregory Harrison
4
CSCI104 0 56
IACT111 0 44
INFO112 0 75
CSCI321 0 89
输出必须是这样的:
13 records read
Commands Available:
d - Display Record
u - Update Record
q - Quit the program
Command: d
Enter student number: 4734455
Student No. 4734455
First Name Kieren
Last Name Legrande
Subjects:
CSCI104 Provisional 65
IACT123 Enrolled 67
CSCI121 Enrolled 98
我想读取此数据并保存并更新记录。但是,该程序不会运行或停留在空白屏幕上,同时没有显示任何错误。它运行但不输出任何内容。
.h 和 main.cpp 都很好。 但我对这个编码文件有疑问。 这是我的编码:
#include <iostream>
#include <fstream>
using namespace std;
// ============== Constants ==========================================
const char cTextFileName[] = "ass1.txt";
const char cBinaryFileName[] = "ass1.dat";
const int cMaxRecs = 100;
const int cMaxChars = 30;
const int cMaxSubjects = 8;
// ============= User Defined types ==================================
enum StatusType{eEnrolled,eProvisional,eWithdrawn};
struct SubjectType{ char Code[8]; StatusType Status; int Mark;};
struct StudentRecord{
long StudentNo;
char FirstName[cMaxChars];
char LastName[cMaxChars];
int NumSubjects;
SubjectType Subjects[cMaxSubjects];
};
// ============= Global Data =========================================
StudentRecord gRecs[cMaxRecs];
int gNumRecs=0;
// ============= Private Function Prototypes =========================
void PrintRecord(int i);
int FindRecord(long StudentNum);
// ============= Public Function Definitions =========================
void ReadFile()
{ // Reads data file into array
int i =0;
StatusType items;
ifstream infile;
infile.open("ass1.txt");
if (!infile.good())
{
cerr << "There is an error opening a file.";
exit(1);
}
infile >> gRecs[i].StudentNo;
while(!infile.eof())
{
infile >> gRecs[ i].FirstName;
infile >> gRecs[i].LastName;
infile >> gRecs[i].NumSubjects;
for(int j=0; j<=gRecs[i].NumSubjects;)
{
infile >> gRecs[i].Subjects[j].Code;
infile >> gRecs[i].Subjects[j].Status;
infile >> gRecs[i].Subjects[j].Mark;
}
i++;
infile >> gRecs[i].StudentNo;
gNumRecs = i;
infile.close();
cout << gNumRecs << "read records " << gNumRecs;
}
}
void DisplayRecord()
{ // Displays specified record on screen
// int stuNo;
int i = 0;
// int StudentNo;
// cout << PrintRecord(i);
cout << "Enter Student Number :";
cin >> gRecs[i].StudentNo;
i=FindRecord(gRecs[i].StudentNo);
if(!FindRecord(gRecs[i].StudentNo));
{
cout << "Sorry, Record not found .";
}
PrintRecord(i);
// cout << PrintRecord(i);
}
void SaveFile()
{// Writes array to text data file
StatusType Status;
fstream myfile;
myfile.open("ass1.txt");
if (!myfile.good())
{
cerr << " Problem opening the data file.";
exit(1);
}
for(int i=0; i<gNumRecs;)
{
cout << "Student No :" << gRecs[i].StudentNo;
cout << "First Name :" << gRecs[i].FirstName;
cout << "Last Name :" << gRecs[i].LastName;
cout << "Subjects :" << gRecs[i].NumSubjects;
for(int j=0; j<=gRecs[i].NumSubjects;)
{
cout << gRecs[i].Subjects[j].Code << " ";
switch(Status)
{
case eEnrolled : cout << "Enrolled" << " " << endl; break;
case eProvisional : cout << "Provisional" << " " << endl; break;
case eWithdrawn : cout << "Withdrawn" << " " << endl; break;
}
cout << gRecs[i].Subjects[j].Mark;
}
}
myfile.close();
cout << "Records have been saved.";
}
void UpdateRecord()
{// updates status or mark of specified subject of specified student number
int i=0;
int j=0;
// int sub_cod;
int studentNo;
// int stuNo;
char optn;
char s;
int m;
fstream infile;
infile.open("ass1.txt");
cout << "Enter Student Number :";
cin >> gRecs[i].StudentNo;
infile >> gRecs[i].StudentNo;
i=FindRecord(gRecs[i].StudentNo);
if(!FindRecord(gRecs[i].StudentNo))
{
cout << "Sorry, Record not found .";
}
PrintRecord(i);
cout << "Please Enter the subject code you want to update :";
cin >> gRecs[i].Subjects[j].Code;
if(!gRecs[i].Subjects[j].Code)
{
cout << "Subject code not found!";
}
cout << "Select status or mark (s/m) :";
cin >> optn;
switch(optn)
{
case 's':
cout << "Select new status :" ;
cout << " " << "e: Enrolled";
cout << " " << "p: Provisional ";
cout << " " << "w: Withdrawn ";
cin >> s;
// need to be entered
cout << "Status :" << s << endl;
cout << "Record " << i << "updated ." << endl;
break;
case 'm':
cout << "Enter the marks";
cin >> m;
for(int j=0; j<=gRecs[i].NumSubjects;)
{
infile >> gRecs[i].Subjects[j].Mark;
}
break;
}
}
// ============= Private Function Definitions =========================
void PrintRecord(int i)
{ // Prints record i on screen
StatusType emembers;
cout << gRecs[i].StudentNo;
cout << gRecs[i].FirstName;
cout << gRecs[i].LastName;
cout << gRecs[i].NumSubjects;
for(int j=0; j<=gRecs[i].NumSubjects;)
{
cout << gRecs[i].Subjects[j].Code;
/* switch(emembers)
{
case '0' : cout << "Enrolled" ; break;
case '1' : cout << "Provisional" << endl; break;
case '2' : cout << "Withdrawn" << endl; break;
}
*/
cout << gRecs[i].Subjects[j].Mark;
}
}
int FindRecord(long StudentNo)
{// returns index of matching record or -1 if not found
int i=0;
for(int i=0; i<13;)
{
if (!gRecs[i].StudentNo && StudentNo == StudentNo)
{
return i;
}
}
return -1;
}
另外,我对 UpdateRecord() 和 FindRecord() 有疑问。如果我犯了任何错误,请检查错误。请大伙帮忙。谢谢
最佳答案
您应该为您的自定义类型覆盖 operator >>
。
template<class _CharT,class _Traits>
std::basic_istream<_CharT,_Traits>&
operator >>(std::basic_istream<_CharT, _Traits>& _is, StatusType& _my_enum)
{
int _temp;
_is >> _temp;
_my_enum = static_cast<StatusType>(_temp);
return _is;
}
关于c++ - 当其标识符已经是结构的成员时如何使用枚举器列表项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49419479/