Hi, I am trying to write a program for my mum who is a teacher, the program needs to be able to store students information like their name address SAT scores etc. I have a basic background in c, but i decided to try and implement this program using c++. what i have done so far compiles but the program keeps crashing whenever i try to enter the section of the program where i have to add students into my vectors.

Here is my code

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <vector>
#include <string>
#include <fstream>

#define MAXSTUDENTS 200

using namespace std;


class Options{
public:

	void welcome(void);
	int main_menu(void);
	Options add(void);
	void search(void);
	void remove(void);
	void display(void);

private:

	vector<Options> info;

	string firstname, lastname, middlename;

	int birthyear, birthmonth, birthdate;

	int contacticenumber;
	string contacticename;

	int housenumber;
	string streetname, city, postcode;

	string allergies;

	string SAT1, SAT2, SAT3, SAT4, SAT5, SAT6, SAT7, SAT8, SAT9;

	};

void Options::welcome()
{
	cout << "Welcome to the Datamaster 1.0" << endl << endl << "Please keep checking for regular software updates" << endl;
	cout << "Loading ";
	for (int a = 0; a < 6; a++)
	{
		cout << ". ";
	for (int x =  0; x < 500; x ++)
	{

		for (int y = 0; y < 200000; y++)
		{}
	}
	}
	system("cls");
}

int Options::main_menu()
{
	int selection;
	char answer;
	

	cout << "Please select from the following: " << endl << endl;
	cout << "1. Search Students" << endl << "2. Add Student" << endl << "3. Remove Student" << endl << "4. Exit Program" << endl;
	cin >> selection;

	if (selection < 1 || selection > 4)
	{
		system("cls");
		cout << "Sorry that was not an option, please try again\nPress any key to return to the main menu";
		cin.get();
		system("cls");
		return Options::main_menu();
	}
	
	if (selection != 4)
	{
	cout << endl << "You have chosen " << selection << ", is this correct (y/n)?" << endl;
	cin >> answer;
	}
	else
	{
	cout << endl << "Do you want to exit the program (y/n)?" << endl;
	cin >> answer;
	}
	if (answer == 'y')
	{

		switch (selection){
		case 1:
			Options::search();
			break;
		case 2:
			Options::add();
			break;
		case 3:
			Options::remove();
			break;
		case 4: 
			return 0;
			break;
		}
				
	}
	else
		return Options::main_menu();

	return selection;
}

void Options::search()
{
	/*cout<< "Searching ";
	for (int a = 0; a < 6; a++)
	{
		cout << ". ";
	for (int x =  0; x < 500; x ++)
	{

		for (int y = 0; y < 200000; y++)
		{}
	}
	}*/
	string lastname;

	cout << "Please enter the last name of the student you are looking for: ";
	cin >> lastname;

	int size = Options::info.size();

    cout << "The Students with that Last name are: ";

	for(int i = 0; i < size; i ++)
	{	
	if (lastname == info[i].lastname)
	{
		cout << info[i].firstname << info[i].lastname;
	}
	}

	system("cls");

	Options::main_menu();
}

void Options::remove()
{
	cout<< "Removing ";
	for (int a = 0; a < 6; a++)
	{
		cout << ". ";
	for (int x =  0; x < 500; x ++)
	{

		for (int y = 0; y < 200000; y++)
		{}
	}
	}
	system("cls");

	Options::main_menu();
}

Options Options::add()
{
	system("cls");
	int choice;
	Options adding;

	int size = Options::info.size();

	for (int i = size; i < MAXSTUDENTS; i++)
	{
	cout << "Please enter the following information about the new student" << endl;

	cin.get();

	cout << "First name: ";
	getline (cin, info[i].firstname);
	
	cout << "Last name: ";
	getline (cin, info[i].lastname);
	
	cout << "Any middle names or middle initials: ";
	getline (cin, info[i].middlename);
	cout << endl;

	cout << "Now please enter the students address" << endl;

	cout << "House number: ";
	cin >> info[i].housenumber;
	
	cin.get();

	cout << "Streetname: ";
	getline (cin, info[i].streetname);

	cout << "City: ";
	getline (cin, info[i].city);
	
	cout << "Postcode: ";
	getline (cin, info[i].postcode);
	cout << endl;

	cout << "Now enter the D.O.B for the student" << endl;

	cout << "Year: ";
	cin >> info[i].birthyear;


	cout << "Month (01-12): ";
	cin >> info[i].birthmonth;


	cout << "Date (01-31): ";
	cin >> info[i].birthdate;


	cout << "Please detail any allergies the student has separated by commas (,)" << endl;

	cin.get();

	cout << "Allergies: ";
	getline (cin, info[i].allergies);
	cout << endl;

	cout << "Now enter the students emergency contact details" << endl;

	cout << "I.C.E Name: ";
	getline (cin, info[i].contacticename);

	cout << "I.C.E Contact Number: ";
	cin >> info[i].contacticenumber;
	cout << endl;

	break;
	}

	cin.get();

	cout << "Student successfully added\nYou can add SAT results to this student now\nor in the edit screen" << endl;
	cout << "Choose either:\n(1) to add another student\n(2) to add SAT results for this student\n(3) to return to main menu" << endl;
	cin >> choice;
	cout << endl;

	switch (choice){
	case 1:
		system("cls");
		Options::add();
		break;
	case 2:
		//Add upto 9 years of SATs results
		system("cls");
		Options::main_menu();
		break;
	case 3:
		system("cls");
		Options::main_menu();
		break;
					}

return adding;
}

void Options::display ()
{
	//function to display all the first and last names of all the students in the database
}

int main()
{	
	Options options;

	options.welcome();
	options.main_menu();

	return 0;
}

Any help would be greatly appreciated

Thanks

Tom

just thought i should mention, the error i keep getting says the vector subscript is out of range...

Tom

I suppose that is a debug assertion you're getting? You should also mention on which line (and don't put spaces in the code tag).

Usually it means that you're accessing some_vector, but since the vector is indexed from [0...size-1] this is out of range. Without proper code tags your code is a little hard to read.

sorry about that, im still kind of new to this. yeah it says its a debug assertion!
The error says: Debug assertion failed!
The error is line 932 of the include file "vector"
And the error occurs when entering the function "add"
I've tried debugging it step by step but everytime i try to enter that function it crashes.

Right, that makes sense. Lets have a look at the function add:

int size = Options::info.size();
for (int i = size; i < MAXSTUDENTS; i++)
{
   // here you access info[i]
}

What if MAXSTUDENTS is bigger than the current size of 'info' ? Then you're going to index elements in info that do not exist.

Does that make sense to you?

I think so...
Well i have tried using just a random large integer in place of MAXSTUDENTS and it still has the same error

Tom

Ok then you do not understand yet.
Take a look at this code:

vector<int> vInt;
for( int i = 0; i < 200; ++i ) {
   vInt[i] = 4;
}

This will set the first 200 elements of vInt to 4, but just like your code it will crash because there is no memory allocated for the 200 elements.
vector has a member function called 'push_back' that will allocate the memory, and add the element:

vector<int> vInt;
for( int i = 0; i < 200; ++i ) {
    vInt.push_back( 4 );
}

This will work just fine.

You can also specify when you define vInt, how many elements should be pre-allocated:

vector<int> vInt(200);
for( int i = 0; i < vInt.size(); ++i ) {
   vInt[i] = 4;
}

As you have a vector of some structure, you may need to do a little more work but I'll leave that to you as you still did not fix the code tags in your original post, it is impossible to read.

Here i have worked out how to sort my code:

#include "stdafx.h"
#include <iostream>
#include <string.h>
#include <vector>
#include <string>
#include <fstream>

#define MAXSTUDENTS 200
using namespace std;
class Options{
public:

	void welcome(void);
	int main_menu(void);
	Options add(void);
	void search(void);
	void remove(void);
	void display(void);

private:

	vector<Options> info;

	string firstname, lastname, middlename;

	int birthyear, birthmonth, birthdate;

	int contacticenumber;
	string contacticename;

	int housenumber;
	string streetname, city, postcode;

	string allergies;

	string SAT1, SAT2, SAT3, SAT4, SAT5, SAT6, SAT7, SAT8, SAT9;

	};

void Options::welcome()
{
	cout << "Welcome to the Datamaster 1.0" << endl << endl << "Please keep checking for regular software updates" << endl;
	cout << "Loading ";
	for (int a = 0; a < 6; a++)
	{
		cout << ". ";
	for (int x =  0; x < 500; x ++)
	{

		for (int y = 0; y < 200000; y++)
		{}
	}
	}
	system("cls");
}

int Options::main_menu()
{
	int selection;
	char answer;
	

	cout << "Please select from the following: " << endl << endl;
	cout << "1. Search Students" << endl << "2. Add Student" << endl << "3. Remove Student" << endl << "4. Exit Program" << endl;
	cin >> selection;

	if (selection < 1 || selection > 4)
	{
		system("cls");
		cout << "Sorry that was not an option, please try again\nPress any key to return to the main menu";
		cin.get();
		system("cls");
		return Options::main_menu();
	}
	
	if (selection != 4)
	{
	cout << endl << "You have chosen " << selection << ", is this correct (y/n)?" << endl;
	cin >> answer;
	}
	else
	{
	cout << endl << "Do you want to exit the program (y/n)?" << endl;
	cin >> answer;
	}
	if (answer == 'y')
	{

		switch (selection){
		case 1:
			Options::search();
			break;
		case 2:
			Options::add();
			break;
		case 3:
			Options::remove();
			break;
		case 4: 
			return 0;
			break;
		}
				
	}
	else
		return Options::main_menu();

	return selection;
}

void Options::search()
{
	/*cout<< "Searching ";
	for (int a = 0; a < 6; a++)
	{
		cout << ". ";
	for (int x =  0; x < 500; x ++)
	{

		for (int y = 0; y < 200000; y++)
		{}
	}
	}*/
	string lastname;

	cout << "Please enter the last name of the student you are looking for: ";
	cin >> lastname;

	int size = Options::info.size();

    cout << "The Students with that Last name are: ";

	for(int i = 0; i < size; i ++)
	{	
	if (lastname == info[i].lastname)
	{
		cout << info[i].firstname << info[i].lastname;
	}
	}

	system("cls");

	Options::main_menu();
}

void Options::remove()
{
	cout<< "Removing ";
	for (int a = 0; a < 6; a++)
	{
		cout << ". ";
	for (int x =  0; x < 500; x ++)
	{

		for (int y = 0; y < 200000; y++)
		{}
	}
	}
	system("cls");

	Options::main_menu();
}

Options Options::add()
{
	system("cls");
	int choice;
	Options adding;

	int size = Options::info.size();

	for (int i = size; i < MAXSTUDENTS; i++)
	{
	cout << "Please enter the following information about the new student" << endl;

	cin.get();

	cout << "First name: ";
	getline (cin, info[i].firstname);
	
	cout << "Last name: ";
	getline (cin, info[i].lastname);
	
	cout << "Any middle names or middle initials: ";
	getline (cin, info[i].middlename);
	cout << endl;

	cout << "Now please enter the students address" << endl;

	cout << "House number: ";
	cin >> info[i].housenumber;
	
	cin.get();

	cout << "Streetname: ";
	getline (cin, info[i].streetname);

	cout << "City: ";
	getline (cin, info[i].city);
	
	cout << "Postcode: ";
	getline (cin, info[i].postcode);
	cout << endl;

	cout << "Now enter the D.O.B for the student" << endl;

	cout << "Year: ";
	cin >> info[i].birthyear;


	cout << "Month (01-12): ";
	cin >> info[i].birthmonth;


	cout << "Date (01-31): ";
	cin >> info[i].birthdate;


	cout << "Please detail any allergies the student has separated by commas (,)" << endl;

	cin.get();

	cout << "Allergies: ";
	getline (cin, info[i].allergies);
	cout << endl;

	cout << "Now enter the students emergency contact details" << endl;

	cout << "I.C.E Name: ";
	getline (cin, info[i].contacticename);

	cout << "I.C.E Contact Number: ";
	cin >> info[i].contacticenumber;
	cout << endl;

	break;
	}

	cin.get();

	cout << "Student successfully added\nYou can add SAT results to this student now\nor in the edit screen" << endl;
	cout << "Choose either:\n(1) to add another student\n(2) to add SAT results for this student\n(3) to return to main menu" << endl;
	cin >> choice;
	cout << endl;

	switch (choice){
	case 1:
		system("cls");
		Options::add();
		break;
	case 2:
		//Add upto 9 years of SATs results
		system("cls");
		Options::main_menu();
		break;
	case 3:
		system("cls");
		Options::main_menu();
		break;
					}

return adding;
}

void Options::display ()
{
	//function to display all the first and last names of all the students in the database
}

int main()
{	
	Options options;

	options.welcome();
	options.main_menu();

	return 0;
}

Ok, that is much more readable.
Do you understand my last post and how you can apply it to your code?

i have managed to get it working following your advice by adding this:

Options adding;


	vector<Options> info(200);//<<<<<<<<<<<<<<<<<<<

	int size = Options::info.size();

	for (int i = size; i < MAXSTUDENTS; i++)

Now i just need to work out how to get it working defining it in a class

Thanks very much!

Tom

Ok, this works but is not very dynamic. What if you later decide to change MAXSTUDENTS to 300? Then you will have the same problem as before.
Also.. your loop does not really make sense, you do:

function add 
{
   info( 200 );
   for( i = 0; i < MAXSTUDENTS; i++ ) {
      // add new student
      break;
   }
   // ask user what to do
   case 1: 
     add();
}

So your for loop is only ever executed one time.

This article has been dead for over six months. Start a new discussion instead.