我的显示功能有问题

An issue with my display function

本文关键字:有问题 功能 显示 我的      更新时间:2023-10-16

我正在为类做一个项目,该项目雇用了一个叫做 employee 的类。我遇到问题的地方是显示功能。所有内容都正确显示,直到最后它继续输出最终抛出的符号列表:

引发异常:读取访问冲突。 这很0xFF4000。

我已经缩小到显示功能或输出显示的循环。对此问题的任何帮助将不胜感激。

#include <iostream>
#include <iomanip>
using namespace std;
class Employee {
private:
int empcode;
char empname[10];
public:
void getdata();
void display();
};
void Employee::getdata() {
cout << "nNAME : ";
cin >> empname;
cout << "nCODE : ";
cin >> empcode;
} 
void Employee::display() {
cout << endl << setw(20) << empname << setw(10) << empcode << endl;
}
int main()
{
Employee Emp[6];
cout << "Enter employee details:n ";
for (int i = 0; i<6; i++)
{
cout << "nemployee " << i + 1 << endl; 
Emp[i].getdata();
}
cout << "nEmployee details are as follows :";
cout << "nn" << setw(20) << "NAME" << setw(10) << "CODE";
cout << "n------------------------------";
for (int i = 0; 1 <= 6; i++)
Emp[i].display();
return 0;
}

你的第二个循环是错误的:

对于 (整数 i = 0;1 <= 6;i++(

应该是:

for (int i = 0; i < 6; i++)

1 < 6会一直循环,但是你的i++每次迭代都会增加,当它得到6时,它会尝试访问不存在的元素Emp[6],只有Emp[0]...Emp[5]