C++ Run Time Error "Stack around the variable 'Student' was corrupted."
C / C++ / MFC
2
Posts
2
Posters
2
Views
1
Watching
-
AnyOne Please Guide Me on my This Error <
"Stack around the variable 'Student' was corrupted."I Receives it when I copy Members of Structure "StudentInfo" into Structure "FailStudents" using list\[i\].FullName = Student\[i\].FullName ; list\[i\].RollNo = Student\[i\].RollNo; Here list is Structure Variable of Structure "FailStudents" and Student is Structure Variable of Structure "StudentInfo" /\* An array stores details of 5 students (rollno, name, marks in three subject). Write a program to create such an array and print out a list of students who have failed in more than one subject. \*/ #include #include using namespace std; const int No\_Of\_Students = 5; void main() { struct StudentInfo { int RollNo; string FullName; int marks\[2\]; }; StudentInfo Student\[No\_Of\_Students\]; struct FailStudents { int RollNo; string FullName; int marks\[2\]; }; FailStudents list\[No\_Of\_Students\]; int NoOfFailStudents = 0; for (int i = 0; i < No\_Of\_Students; i++) { cout << "Enter Student NO " << i + 1 << \\" FullName : \\t"; cin >> Student\[i\].FullName; cout << "Enter Student NO " << i + 1 << " Roll Number : \\t"; cin >> Student\[i\].RollNo; int failSubjects = 0; // Subjects Marks Input for (int k = 0; k <= 2; k++) { cout << "Enter Student NO " << i + 1 << " Subject " << k + 1 << " Marks : \\t"; cin >> Student\[i\].marks\[k\]; if ((Student\[i\].marks\[k\]) < 40) { failSubjects++; } } if (failSubjects > 1) { NoOfFailStudents++; list\[i\].FullName = Student\[i\].FullName; list\[i\].RollNo = Student\[i\].RollNo; cout << "Student Name "<< list\[i\].FullName << "is fail"<< endl; cout << "Having Rol Number " << list\[i\].RollNo << endl; // list\[i\].marks = Student\[i\].marks; } cout << "\\n \\n "; } system("pause");
}
-
AnyOne Please Guide Me on my This Error <
"Stack around the variable 'Student' was corrupted."I Receives it when I copy Members of Structure "StudentInfo" into Structure "FailStudents" using list\[i\].FullName = Student\[i\].FullName ; list\[i\].RollNo = Student\[i\].RollNo; Here list is Structure Variable of Structure "FailStudents" and Student is Structure Variable of Structure "StudentInfo" /\* An array stores details of 5 students (rollno, name, marks in three subject). Write a program to create such an array and print out a list of students who have failed in more than one subject. \*/ #include #include using namespace std; const int No\_Of\_Students = 5; void main() { struct StudentInfo { int RollNo; string FullName; int marks\[2\]; }; StudentInfo Student\[No\_Of\_Students\]; struct FailStudents { int RollNo; string FullName; int marks\[2\]; }; FailStudents list\[No\_Of\_Students\]; int NoOfFailStudents = 0; for (int i = 0; i < No\_Of\_Students; i++) { cout << "Enter Student NO " << i + 1 << \\" FullName : \\t"; cin >> Student\[i\].FullName; cout << "Enter Student NO " << i + 1 << " Roll Number : \\t"; cin >> Student\[i\].RollNo; int failSubjects = 0; // Subjects Marks Input for (int k = 0; k <= 2; k++) { cout << "Enter Student NO " << i + 1 << " Subject " << k + 1 << " Marks : \\t"; cin >> Student\[i\].marks\[k\]; if ((Student\[i\].marks\[k\]) < 40) { failSubjects++; } } if (failSubjects > 1) { NoOfFailStudents++; list\[i\].FullName = Student\[i\].FullName; list\[i\].RollNo = Student\[i\].RollNo; cout << "Student Name "<< list\[i\].FullName << "is fail"<< endl; cout << "Having Rol Number " << list\[i\].RollNo << endl; // list\[i\].marks = Student\[i\].marks; } cout << "\\n \\n "; } system("pause");
}
You have declared
marks
as a 2 element array, but your loop runs 3 times.for (int k = 0; k <= 2; k++) { // <-- this will run three loops
cout << "Enter Student NO " << i + 1 << " Subject " << k + 1 << " Marks : \t";
cin >> Student[i].marks[k]; // when k == 2 you are overwriting unallocated memoryif ((Student\[i\].marks\[k\]) < 40) { failSubjects++; }
}
It should be:
for (int k = 0; k < 2; k++) { // k must be only 0 or 1