This is just part of my code.
For some unknown reason, the value of staff is correct after using copy constructor, however, once it goes out of the loop, 2 of s value's turn into -858993460, other values stay fine, look at the red lines part of the code

Staff s;
		int currentLineNum;
		for(int nLineNum = 1;;nLineNum++)
		{

						// read a buffer
			char buffer[256];
			pFileStream2->getline(buffer, 256);
			if (pFileStream2->fail())
			{
				delete pFileStream2;
				break;
			}

			// parse the individual fields
			char userName[80];
			char password[80];
			int accessLevel;
			int staffNumber;
			char name[80];
			char ICNumber[80];
			char gender;
			char designation[80];
			char department[80];
			char dateJoined[80];
			char nationality[80];
			char religion[80];
			char dob[80];
			char maritalStatus[80];
			bool result = parseString(	buffer,
										userName,
										password,
										accessLevel,
										staffNumber,
										name, 80,
										ICNumber,
										gender,
										designation,
										department,
										dateJoined,
										nationality,
										religion,
										dob,
										maritalStatus);
			if (result == false)
			{
				cerr << "Error parsing string\n" << endl;
			}

			if(idArray[selection] == staffNumber)
			{
				currentLineNum = nLineNum;
				Staff tempStaff (	userName,
									password,
									staffNumber,
									name, 
									ICNumber, 
									gender, 
									designation,
									department,
									dateJoined,
									nationality,
									religion,
									dob,
									maritalStatus);
				Staff s(tempStaff);

				cout << "  s number = " << s.staffNumber << "\n" //the value is correct
					 << "  s access level = " << s.accessLevel << endl;

				// output the fields we parsed out
				cout << "  User Name = " << userName << "\n"
					 << "  Password = " << password << "\n"
					 << "  Staff Number = " << staffNumber << "\n"
					 << "  Name = " << name << "\n"
					 << "  IC Number = " << ICNumber << "\n"
					 << "  Gender = " << gender << "\n"
					 << "  Designation = " << designation << "\n"
					 << "  Department = " << department << "\n"
					 << "  Date Joined = " << dateJoined << "\n"
					 << "  Nationality = " << nationality << "\n"
					 << "  Religion = " << religion << "\n"
					 << "  Date of Birth = " << dob << "\n"
					 << "  Marital Status = " << maritalStatus << endl;
			}
		}
	
				cout << "  s number = " << s.staffNumber << "\n" //the values turns into -858993460
					 << "  s access level = " << s.accessLevel << endl;

When the value of a variable changes without any kind of modifying statement on it, you should look for memory corruption like buffer overflows and bogus pointers. I can help you look for it if you can pare your code down to a small but complete program that still has the problem. Looking for that kind of thing in a snippet is probably not going to be productive.

Edited 7 Years Ago by Tom Gunn: n/a

i'm having difficulty paring it down...
i've removed several unrelated functions
below are the codes, not sure if i removed something crucial.

#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
using namespace std;

class Staff
{
public:
	Staff(){};
	Staff(	string wUserName,
			string wPassword,
			string wName, 
			string wICNumber, 
			char wGender, 
			string wDesignation, 
			string wDepartment, 
			string wDateJoined, 
			string wNationality, 
			string wReligion, 
			string wDob, 
			string wMaritalStatus )
		:	userName(wUserName),
			password(wPassword),
			accessLevel(0),
			staffNumber(nextStaffNum++),
			name(wName),
			ICNumber(wICNumber),
			gender(wGender),
			designation(wDesignation),
			department(wDepartment),
			dateJoined(wDateJoined),
			nationality(wNationality),
			religion(wReligion),
			dob(wDob),
			maritalStatus(wMaritalStatus)
			{updateNextStaffNum();};
	Staff(	string wUserName,
			string wPassword,
			int wStaffNumber,
			string wName, 
			string wICNumber, 
			char wGender, 
			string wDesignation, 
			string wDepartment, 
			string wDateJoined, 
			string wNationality, 
			string wReligion, 
			string wDob, 
			string wMaritalStatus )
		:	userName(wUserName),
			password(wPassword),
			accessLevel(0),
			staffNumber(wStaffNumber),
			name(wName),
			ICNumber(wICNumber),
			gender(wGender),
			designation(wDesignation),
			department(wDepartment),
			dateJoined(wDateJoined),
			nationality(wNationality),
			religion(wReligion),
			dob(wDob),
			maritalStatus(wMaritalStatus)
			{};

	void view(){};

//protected:
	string userName;
	string password;
	int accessLevel;
	int staffNumber;
	string name;
	string ICNumber;
	char gender;
	string designation;
	string department;
	string dateJoined;
	string nationality;
	string religion;
	string dob;
	string maritalStatus;

};




bool parseString(const char* pString,
				 char* pUserName,
				 char* pPassword,
				 int& accessLevel,
				 int& staffNumber,
                 char* pName, int arraySize,
				 char* pICNumber,
				 char& gender,
				 char* pDesignation,
				 char* pDepartment,
				 char* pDateJoined,
				 char* pNationality,
				 char* pReligion,
				 char* pDob,
				 char* pMaritalStatus)
{
    // associate an istrstream object with the input
    // character string
    istringstream inp(pString);

	inp.getline(pUserName, arraySize, ',');
    inp.getline(pPassword, arraySize, ',');
    inp.getline(pName, arraySize, ',');
    inp.getline(pICNumber, arraySize, ',');
	inp.getline(pDesignation, arraySize, ',');
	inp.getline(pDepartment, arraySize, ',');
	inp.getline(pDateJoined, arraySize, ',');
	inp.getline(pNationality, arraySize, ',');
	inp.getline(pReligion, arraySize, ',');
	inp.getline(pDob, arraySize, ',');
	inp.getline(pMaritalStatus, arraySize, ',');
	inp >> staffNumber;
	inp >> gender;
	inp >> accessLevel;
    // return the error status
    return !inp.fail();
}
int main()
{
		
		static int searchcount = 0;
		static int idArray[100];
		ifstream* pFileStream = new ifstream("StaffInfo.txt");
		if (!pFileStream->good())
		{
			cerr << "Can't open " << endl;
		}
		int aFromStaffId;
		int aToStaffId;
		cout << "From StaffID: ";
		cin >> aFromStaffId;
		cout << "To StaffID: ";
		cin >> aToStaffId;
		fflush(stdin);
		// read a line out of file, parse it and display
		// results
		for(int nLineNum = 1;;nLineNum++)
		{

						// read a buffer
			char buffer[256];
			pFileStream->getline(buffer, 256);
			if (pFileStream->fail())
			{
				searchcount = 0;
				delete pFileStream;
				break;
			}

			// parse the individual fields
			char userName[80];
			char password[80];
			int accessLevel;
			int staffNumber;
			char name[80];
			char ICNumber[80];
			char gender;
			char designation[80];
			char department[80];
			char dateJoined[80];
			char nationality[80];
			char religion[80];
			char dob[80];
			char maritalStatus[80];
			bool result = parseString(	buffer,
										userName,
										password,
										accessLevel,
										staffNumber,
										name, 80,
										ICNumber,
										gender,
										designation,
										department,
										dateJoined,
										nationality,
										religion,
										dob,
										maritalStatus);
			if (result == false)
			{
				cerr << "Error parsing string\n" << endl;
			}

			if((staffNumber >= aFromStaffId ) && ( staffNumber <= aToStaffId ))
			{
				searchcount++;
				idArray[searchcount] = staffNumber;
				// output the fields we parsed out
				cout << searchcount << ": " << staffNumber << " - " << name << endl;
			}
		}
		int selection;
		cout << "Selection : " << endl;
		cin >> selection;

		ifstream* pFileStream2 = new ifstream("StaffInfo.txt");
		if (!pFileStream2->good())
		{
			cerr << "Can't open " << endl;
		}
		
		Staff s;
		int currentLineNum;
		for(int nLineNum = 1;;nLineNum++)
		{

						// read a buffer
			char buffer[256];
			pFileStream2->getline(buffer, 256);
			if (pFileStream2->fail())
			{
				delete pFileStream2;
				break;
			}

			// parse the individual fields
			char userName[80];
			char password[80];
			int accessLevel;
			int staffNumber;
			char name[80];
			char ICNumber[80];
			char gender;
			char designation[80];
			char department[80];
			char dateJoined[80];
			char nationality[80];
			char religion[80];
			char dob[80];
			char maritalStatus[80];
			bool result = parseString(	buffer,
										userName,
										password,
										accessLevel,
										staffNumber,
										name, 80,
										ICNumber,
										gender,
										designation,
										department,
										dateJoined,
										nationality,
										religion,
										dob,
										maritalStatus);
			if (result == false)
			{
				cerr << "Error parsing string\n" << endl;
			}

			if(idArray[selection] == staffNumber)
			{
				currentLineNum = nLineNum;
				Staff tempStaff (	userName,
									password,
									staffNumber,
									name, 
									ICNumber, 
									gender, 
									designation,
									department,
									dateJoined,
									nationality,
									religion,
									dob,
									maritalStatus);
				Staff s(tempStaff);

				cout << "  s number = " << s.staffNumber << "\n" //the value is correct
					 << "  s access level = " << s.accessLevel << endl;

				// output the fields we parsed out
				cout << "  User Name = " << userName << "\n"
					 << "  Password = " << password << "\n"
					 << "  Staff Number = " << staffNumber << "\n"
					 << "  Name = " << name << "\n"
					 << "  IC Number = " << ICNumber << "\n"
					 << "  Gender = " << gender << "\n"
					 << "  Designation = " << designation << "\n"
					 << "  Department = " << department << "\n"
					 << "  Date Joined = " << dateJoined << "\n"
					 << "  Nationality = " << nationality << "\n"
					 << "  Religion = " << religion << "\n"
					 << "  Date of Birth = " << dob << "\n"
					 << "  Marital Status = " << maritalStatus << endl;
			}
		}
	
				cout << "  s number = " << s.staffNumber << "\n" //the values turns into -858993460
					 << "  s access level = " << s.accessLevel << endl;

    system("PAUSE");

}

and this is the StaffInfo.txt file i used to parse

username,password,Peter,890318-04-5127,CEO,ALL,18-9-2001,Canada,Buddist,18-3-1989,Single,1000 M 0 
username1,password1,James,890318-04-5127,CEO,ALL,18-9-2001,Africa,Buddist,18-3-1989,Single,1001 M 1 
username2,password2,Garrett,890318-04-5127,CEO,ALL,18-9-2001,Australia,Buddist,18-3-1989,Single,1002 M 2 
username3,password3,May,890318-04-5127,CEO,ALL,18-9-2001,USA,Buddist,18-3-1989,Single,1003 M 2 
username4,password4,June,890318-04-5127,CEO,ALL,18-9-2001,USA,Buddist,18-3-1989,Single,1004 s 2
This article has been dead for over six months. Start a new discussion instead.