有人知道此错误是否与win64有关,或者我的代码有问题吗? 在窗口10-64位上,我正在运行gcc版本egcs-2.91.57 19980901(egcs-1.1版本)-我认为它适用于32bit
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_STUDENT = 3;
typedef struct student{
char name[50];
char classID[15];
int age;
};
student student_list [MAX_STUDENT];
int student_position = 0;
void add_new(){
if (student_position == MAX_STUDENT) {
student_position = 1;
}
else {
student_position++;
}
cout << "student name: " << endl;
cin >> student_list[student_position].name;
cout << "student classID: " << endl;
cin >> student_list[student_position].classID;
cout << "student age: " << endl;
cin >> student_list[student_position].age ;
}
void list(){
for(int i = 1; i < student_position+1; i ++){
cout << i << "/name: " << student_list[i].name << " - classID: " << student_list[i].classID << " - age: " << student_list[i].age << endl;
}
}
int main()
{
char command = 'a';
cout << "please input command: N (New), L (List), E (Exit), W(Write to file)." << endl;
while(cin >> command){
switch (command) {
case 'N':
add_new();
break;
case 'L':
list();
break;
case 'E':
return 0;
default:
cout << "wrong command" << endl;
break;
}
};
return 0;
}
然后第三次输入学生信息时发生错误:
[main] D:\1.books\0.C++_Primer\student.exe 1000 (0) handle_exceptions: Exception: STATUS_ACCESS_VIOLATION [main] student 1000 (0) handle_exceptions: Dumping stack trace to student.exe.core
p / s:我添加了堆栈跟踪:
[main] student 1000 (0) exception: trapped!
[main] student 1000 (0) exception: code 0xC0000005 at 0x407E23
[main] student 1000 (0) exception: ax 0x330000 bx 0x330000 cx 0x418348 dx 0xA
[main] student 1000 (0) exception: si 0x0 di 0x401000 bp 0x246FEA8 sp 0x246FEA4
[main] student 1000 (0) exception: exception is: STATUS_ACCESS_VIOLATION
[main] student 1000 (0) stack: Stack trace:
[main] student 1000 (0) stack: frame 0: sp = 0x246F830, pc = 0x6100A2C3
[main] student 1000 (0) stack: frame 1: sp = 0x246F86C, pc = 0x7783EAC2
[main] student 1000 (0) stack: frame 2: sp = 0x246F890, pc = 0x7783EA94
[main] student 1000 (0) stack: frame 3: sp = 0x246F95C, pc = 0x7782C6B6
[main] student 1000 (0) stack: frame 4: sp = 0x246FEA8, pc = 0x407AB1
[main] student 1000 (1) stack: frame 5: sp = 0x246FED0, pc = 0x4011A2
[main] student 1000 (0) stack: frame 6: sp = 0x246FEE4, pc = 0x401637
[main] student 1000 (0) stack: frame 7: sp = 0x246FF00, pc = 0x61004402
[main] student 1000 (0) stack: frame 8: sp = 0x246FF48, pc = 0x61004420
[main] student 1000 (0) stack: frame 9: sp = 0x246FF54, pc = 0x41772E
[main] student 1000 (0) stack: frame 10: sp = 0x246FF64, pc = 0x40103A
[main] student 1000 (0) stack: frame 11: sp = 0x246FF80, pc = 0x77318484
[main] student 1000 (0) stack: frame 12: sp = 0x246FF94, pc = 0x77822FEA
[main] student 1000 (0) stack: frame 13: sp = 0x246FFDC, pc = 0x77822FBA
[main] student 1000 (0) stack: frame 14: sp = 0x246FFEC, pc = 0x0
[main] student 1000 (0) stack: End of stack trace
答案 0 :(得分:2)
您的索引错误。
C和C ++都使用基于零的索引。这意味着可以使用索引N
访问维度为0..(N-1)
的本机数组。您student_position
应该将视在翻转达到零(如果已达到最大数组维数时,在遇到插入请求时似乎正在寻找)。并且显示的循环应该从零到尺寸,在顶端严格小于(因此符合0..(N-1)
索引。
参见下文:
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_STUDENT = 3;
struct student
{
char name[50];
char classID[15];
int age;
};
student student_list [MAX_STUDENT];
int student_position = 0;
void add_new(){
if (student_position == MAX_STUDENT) {
student_position = 0; // HERE
}
cout << "student name: " << endl;
cin >> student_list[student_position].name;
cout << "student classID: " << endl;
cin >> student_list[student_position].classID;
cout << "student age: " << endl;
cin >> student_list[student_position].age ;
++student_position; // HERE
}
void list(){
for(int i = 0; i < student_position; ++i){
cout << i << "/name: " << student_list[i].name << " - classID: " << student_list[i].classID << " - age: " << student_list[i].age << endl;
}
}
int main()
{
char command = 'a';
cout << "please input command: N (New), L (List), E (Exit), W(Write to file)." << endl;
while(cin >> command){
switch (command) {
case 'N':
add_new();
break;
case 'L':
list();
break;
case 'E':
return 0;
default:
cout << "wrong command" << endl;
break;
}
}
return 0;
}
这将解决错误的问题,但要让您完成确定包装逻辑是否正确的任务。我怀疑不是,但这不是这个问题的意思。