Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Run Time Error "Stack around the variable 'Student' was corrupted."

C++ Run Time Error "Stack around the variable 'Student' was corrupted."

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresc++helptutorial
2 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Umad Ahmad
    wrote on last edited by
    #1

    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");
    

    }

    L 1 Reply Last reply
    0
    • U Umad Ahmad

      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");
      

      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 memory

      if ((Student\[i\].marks\[k\]) < 40)
      {
          failSubjects++;
      }
      

      }

      It should be:

      for (int k = 0; k < 2; k++) { // k must be only 0 or 1

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups