Can someone help me? after exiting the program.. the student record.txt becomes empty after i restart the program..
so far Im only after the name, last name, and student id because i dont want to confuse myself more but if someone could teach me here i might implement other datas..
here is the codes:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


struct studentRecordType
{
	string fName;
	string lName;
	int studID;

};

int main()
{
	studentRecordType students[3000];
	ifstream infeil;
	ofstream outfeil;
	char yNnAnswer,transactionSelector;
	int index=2000;
	
	int primaryChoice;
	outfeil.open("studentrecord.txt");
	
	cout<<"******************************************************\n"
		<<"******************* STUDENT RECORD *******************\n"
		<<"******************************************************\n"
		<<"\t\t 1.Add a student\n"
		<<"\t\t 2.Search a student\n"
		<<"\t\t 3.Delete a record\n"
		<<"\t\t 4.quit\n"
		<<"\t\t your choice: ";
	cin>>primaryChoice;
	cout<<endl;
	switch(primaryChoice)
	{
	case 1:
		{
			system("CLS");
			cout<<"****************************************\n"
				<<"********Registering a student***********\n"
				<<"****************************************\n";
			cout<<endl<<endl;
			cout<<"Please enter the students first name:";
			cin>>students[index].fName;
			outfeil<<students[index].fName<<" ";
			cout<<endl;
			cout<<"Please enter the students last name:";
			cin>>students[index].lName;
			outfeil<<students[index].lName<<" ";
			cout<<endl;
			students[index].studID = index; 
			outfeil<<students[index].studID<<endl;
			cout<<"Your student number would be: "<<students[index].studID<<endl;
			cout<<"do you want to register another student?(y/n):";
			cin>>yNnAnswer;
			if(yNnAnswer == 'y')
			{
			do
			{ 
			index++;
			system("CLS");
			cout<<"****************************************\n"
				<<"********Registering a student***********\n"
				<<"****************************************\n";
			cout<<endl<<endl;
			cout<<"Please enter the students first name:";
				cin>>students[index].fName;
				outfeil<<students[index].fName<<" ";
			cout<<endl;
			cout<<"Please enter the students last name:";
				cin>>students[index].lName;
				outfeil<<students[index].lName<<" ";
			cout<<endl;
				students[index].studID = index; 
				outfeil<<students[index].studID<<endl;
			cout<<"Your student number would be: "<<students[index].studID<<endl;
			cout<<"do you want to register another student?(y/n):";
				cin>>yNnAnswer;
			}
			while(yNnAnswer == 'y' );
			}
			outfeil.close();
			break;
		};
	case 2:
		{
			cout<<students[index].fName<<endl;
			break;
		};
	case 3:
		{
			break;
		};
	case 4:
		{
			exit(0);
		}
	}
	return main();
	system("pause");
}

Recommended Answers

All 6 Replies

Lines 19 and 20 should read infile and outfile

Lines 19 and 20 should read infile and outfile

thank you for the reply... i'll fix this up and post the new one as soon as possible and i'll try to revise it

Here is a very simple way to enter records in your array:

void InputStudents(studType Students[], int NrE)
{
	int x = NrE;
	long studNr;

	cout << "Enter a student number( or 0 to stop):";
	cin >> studNr;
	while ( studNr != 0 && x < 10)
	{
		Students[x].studNr = studNr;
		cout << "Enter name:";
		cin >> Students[x].name;
		cout << "Enter surname:";
		cin >> Students[x].surname;
		cout << "Enter age:";
		cin >> Students[x].age;
		x++;
		cout << "Enter a student number( or 0 to stop):";
		cin >> studNr;
	}

	NrE = x;
}

Here is a very simple way to enter records in your array:

void InputStudents(studType Students[], int NrE)
{
	int x = NrE;
	long studNr;

	cout << "Enter a student number( or 0 to stop):";
	cin >> studNr;
	while ( studNr != 0 && x < 10)
	{
		Students[x].studNr = studNr;
		cout << "Enter name:";
		cin >> Students[x].name;
		cout << "Enter surname:";
		cin >> Students[x].surname;
		cout << "Enter age:";
		cin >> Students[x].age;
		x++;
		cout << "Enter a student number( or 0 to stop):";
		cin >> studNr;
	}

	NrE = x;
}

why does x have to be equal to NrE? im a little puzzled

NrE is the number of elements

And if you want the number of elements in the array to be returned, then

void InputStudents(studType Students[], int maxStudents, int& NrE)
...

Note the addition of the '&' after int, meaning that a reference to NrE is what's passed. Also, I've included the size of the array, rather than hard-coding a value of 10 inside the function ... then call it as

studType the_students[100];
int num_students = 0;  // the number of valid students in the array so far
InputStudents(the_students, 100, num_students);
// on return, num_students will contain the number of valid students in the array

Also, why are you writing new registered students to outfeil/outfile (regardless of how you spell it) as you read them in from the user? This will make deleting them much harder!

As far as why it's empty when you re-run the program, the default usage of ofstream::open() is to delete any existing content. If you want to append new content onto the end, use outfeil.open(student_records.txt", ios_base::app); . Or possibly ios_base::out|ios_base:app if just app doesn't work. See http://www.cplusplus.com/reference/iostream/ofstream/open/ for more details.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.