我的显示功能有问题(An issue with my display function)
我正在开发一个雇用班级的班级项目。 我遇到问题的地方在于显示功能。 一切都正确显示,直到它输出一个最终抛出的符号列表的末尾:
抛出异常:读取访问冲突。 这是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 << "\nAME : "; 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 << "\n\n" << setw(20) << "AME" << setw(10) << "CODE"; cout << "\n------------------------------"; for (int i = 0; 1 <= 6; i++) Emp[i].display(); return 0; }I'm working on a project for class that employs a class called employee. Where I'm having an issue is with the display function. Everything displays correctly until the end where it proceeds to output a list of symbols that eventually throwing:
exception is thrown: read access violation. this was 0xFF4000.
I've narrowed to issue to either the display function or the loop that outputs the display. Any help with this issue would be much appreciated.
#include <iostream> #include <iomanip> using namespace std; class Employee { private: int empcode; char empname[10]; public: void getdata(); void display(); }; void Employee::getdata() { cout << "\nAME : "; 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 << "\n\n" << setw(20) << "AME" << setw(10) << "CODE"; cout << "\n------------------------------"; for (int i = 0; 1 <= 6; i++) Emp[i].display(); return 0; }最满意答案
你的第二个循环是错误的:
for(int i = 0; 1 <= 6 ; i ++)
应该:
for (int i = 0; i < 6; i++)1 < 6会一直循环,但是你的i++会在每次迭代中增加,当它达到6时,它会尝试访问不存在的元素Emp[6] ,只有Emp[0]...Emp[5]
Your second loop is wrong:
for (int i = 0; 1 <= 6; i++)
Should be:
for (int i = 0; i < 6; i++)1 < 6 will loop all the time, but your i++ will be increasing every iteration, when it gets 6, it tries to access to nonexistent element Emp[6], there are only Emp[0]...Emp[5]
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 10 条评论) |
本站网友 不要迷恋哥 | 2分钟前 发表 |
"; cin >> empname; cout << "\nCODE | |
本站网友 女人别喊疼 | 22分钟前 发表 |
但是你的i++会在每次迭代中增加 | |
本站网友 斑马打印机驱动 | 3分钟前 发表 |
int empcode; char empname[10]; public | |
本站网友 杭州牙齿矫正 | 26分钟前 发表 |
"; cin >> empcode; } void Employee | |
本站网友 篆体字库 | 25分钟前 发表 |
但是你的i++会在每次迭代中增加 | |
本站网友 思维导图的三招十八式 | 4分钟前 发表 |
一切都正确显示 | |
本站网友 现在房价会跌吗 | 26分钟前 发表 |
直到它输出一个最终抛出的符号列表的末尾: 抛出异常:读取访问冲突 | |
本站网友 儿童癫痫 | 25分钟前 发表 |
"; cout << "\n\n" << setw(20) << "AME" << setw(10) << "CODE"; cout << "\n------------------------------"; for (int i = 0; 1 <= 6; i++) Emp[i].display(); return 0; } I'm working on a project for class that employs a class called employee. Where I'm having an issue is with the display function. Everything displays correctly until the end where it proceeds to output a list of symbols that eventually throwing | |
本站网友 思吧软件论坛 | 6分钟前 发表 |