can compile but crash when run.


The code can be compiled just fine, but the problem is when running, after I give student ID, the program crash if the ID integer is more than 1. Not the value, but the digit. The ID is supposed to be 6 digit, but less than 10.
I hope someone can help me solve the problem, or at least give me some hints on how to solve it, where did i do wrong, etc.
Here's the code:

#include<iostream>
#include <windows.h> // call the colour from library
#include<string>
#include<fstream>
#include<stdio.h> //for fflush(stdin)
using namespace std;

struct CourseRegistrationInformation //system that enables student to register
//course for KMK1093 Basic Programming with quota
{
long int stud_ID; //to define the student matric number
string stud_Name; //to define the student name
string prog_Code; //to define the program that student taken

};

const int QUOTA = 100; //quota left
CourseRegistrationInformation course[QUOTA];
int kmk1093basicprogramming = 0;

//function for this program

void CourseRegistration();
void UpdateStudentInformation();
void DeleteStudentRegister();
void DisplayStudentList();
void Save();

int main()
{
course[QUOTA].stud_ID=0;
char choice;
// This is the main menu

Option:
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 11);
//colour from library that we choose.
cout<<"\n Group member : AHMAD QKHUAIS ALQARNI BIN HAJI BAHARI 39693 \n";

cout<<" Course Name : Basic Programming \n";
cout<<" Course Code : KMK 1093 \n";
cout<<" Assignment Name : STUDENT COURSES REGISTRATION 2012 \n\n";
cout<<"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* \n";
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 14);
//colour from library that we choose.
cout<<"\n @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"<<endl;
cout<<" @@ STUDENT COURSE REGISTRATION @@ \n"<<endl;
cout<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n"<<endl;
cout<<" @@ 0. EXIT @@\n"<<endl;
cout<<" @@ 1. COURSE REGISTRATION @@\n"<<endl;
cout<<" @@ 2. UPDATE STUDENT INFORMATION @@\n"<<endl;
cout<<" @@ 3. DELETE REGISTERED STUDENT @@\n"<<endl;
cout<<" @@ 4. DISPLAY STUDENT LIST @@\n"<<endl;
cout<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"<<endl;
cout<<" \n"<<endl;
cout << " Enter choice 1-4 to continue or 0 to Exit: " << endl;
cin >> choice;
//our group member name for this project include course name, course code, and
//the title of assignment name and the main menu for register course

switch(choice)
{
case '0':;
goto end;

case '1':;
CourseRegistration();
goto Option;

case '2':;
UpdateStudentInformation();
goto Option;

case '3':;
DeleteStudentRegister();
goto Option;

case '4':;
DisplayStudentList();
goto Option;

case 'Y'://yes to save
case 'y'://yes to save
break;
case 'N'://don't save
case 'n'://don't save
Save();//save the data
goto Option;
}
end:;

SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 15);
//colour from library that we choose.
cout<<"\n Thank you for REGISTER THE COURSE!\n";
cout<<" Have a nice day!.\n\n";
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 95);
//colour from library that we choose.
system("pause");
return 0;
}

void CourseRegistration()
{
if(kmk1093basicprogramming <=9)
{
CourseRegistration:
int number;
cout<<" \n Please enter student ID : ";
cin>>number;

//if(kmk1093basicprogramming > 10)

/*{
cout << "\n Error!!! : Your ID input is invalid!" <<endl
<< "Please Enter your ID again" << endl;

goto CourseRegistration;
}*/
course[number].stud_Name = number;
cout<< "\a Please enter your name : " <<endl;
cin>>course[number].stud_Name;
fflush(stdin);

course[number].prog_Code;
cout<< "\a Please enter your program code : " <<endl;
cin>>course[number].prog_Code;
fflush(stdin);
kmk1093basicprogramming++;
}
else //if you choose the WRONG CODE PROGRAM
cout<<"\n\a Error!! This course only for WS24 program "<<endl;
//NEW ENTER
cout<<"\n\a Please re-enter your program code "<<endl;
CourseRegistration();

}

void UpdateStudentInformation()
{
long int stud_ID;
cout<< "\a \n Please enter ID number if you want update : ";
cin>>stud_ID;
stud_ID--;

cout<<"\n\a Sorry!! The student ID cannot be found! "<<endl;
cin>>stud_ID; //NEW ENTER
cout<<"\n\a Do you want to continue press 'Y' to main menu or 'N' to exit "<<endl;
cin>>stud_ID;

cout<<"\a \n Please enter your ID : "<<endl;
cin>> course[kmk1093basicprogramming].stud_ID;
cout<< "\a Please enter your name : " <<endl;
cin>>course[kmk1093basicprogramming].stud_Name;
fflush(stdin);
cout<< "\a Please enter your program code : " <<endl;
cin>>course[kmk1093basicprogramming].prog_Code;
fflush(stdin);
cout<<"\a Press 'Y' if you want save your data or 'N' to exit : ";
cin>>stud_ID;
cout<< "\a Succesfully Save and Update Course Registrationr"<<++stud_ID<<endl;
}

void DeleteStudentRegister()
{
// CourseRegistration
//do
//{
long int stud_ID;
cout<< "\a \n Please enter ID number if you want delete the register course : ";
cin>>stud_ID;
//}
//while(kmk1093basicprogramming <=9);
cout<< "\a Succesfully Deleted Course Account Number " <<stud_ID<<endl;
// default:
//if you choose the WRONG CODE PROGRAM
//cout<<"\n\a Sorry!! The student ID cannot be found! "<<endl;
//cout<<"\n\a Do you want to continue press 'Y' to main menu or 'N' to exit "<<endl;
//}
//long int stud_ID;
//if(QUOTA<=10)
//if(stud_ID <=10)
course[QUOTA -1].stud_ID = 0;
course[QUOTA -1].stud_Name = "";
course[QUOTA -1].prog_Code ="";
}

void DisplayStudentList()
{
cout << " #######################################################################" << endl;
cout << " ## student ID\t student name\t program code\t ## \n";
cout << " #######################################################################" << endl;
for(int i=0; i< QUOTA; i++)
{

}
cout << "\n \n \n \n";
}
void Save()
{
ofstream file;
file.open("C:/CourseEnrollment.txt");
file << " #######################################################################" << endl;
file << " ## student ID\t student name\t program code\t ## \n";
file << " #######################################################################" << endl;

for (int i = 0; i< QUOTA; i++)
{


}
file.close();

cout << "Successfully saved to file";
cout << "\n \n \n \n" ;
}


This is the type of situation where you want to make a new program to isolate your issue.

It will help your cause on this site if you put your code in code tags, as well as use some type of indenting.

The main issue I think is that you use the studentID as the index to the course array. I would keep a separate variable to maintain the idx of the first "null" student in the array.
Like LowestOne says, please use code tags to make it easier to read,
I can see the first line of main writes to memory that is not reserverd

course[QUOTA].stud_ID=0;

course has indexes from 0 to 99 and this is assigning to index 100, bad start to things, change it to QUOTA-1 or just 99.
1
2
3
4
5
6
7
8
9
const int QUOTA = 100; //quota left
CourseRegistrationInformation course[QUOTA];
//...
// In CourseRegistration()
int number;
cout<<" \n Please enter student ID : ";
cin>>number;
// ...
course[number].stud_Name = number;


Entering any value larger than 99 or smaller than 0 will cause you to go out of bounds and will typically crash your program. As LowestOne mentioned, you should have a separate variable for keeping track of the number of students entered so far (I believe that this might be the variable kmk1093basicprogramming).

And you definitely need to use the code tags. -.-

Let's also not forget the use of goto/labels, and numerous other logical errors throughout your code. Also, what is this?
1
2
3
4
5
6
case 'Y'://yes to save
case 'y'://yes to save
break;
case 'N'://don't save
case 'n'://don't save
Save();//save the data 


That doesn't make sense, nor do I see anywhere that you ask a Y or N question in your menu.
Topic archived. No new replies allowed.