System Generated ID Number

I'm trying to make a program that generates an ID number automatically and save it in a text file however, when I close the program and reopen in it again it does not automatically generate a new ID number it just resets to 1 instead of the next ID number for the last generated ID number in the text file. This is the snippets of the code that I think relevant to answering the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
struct employeeinfo {
	string name;
	string address;
	string department;
	string position;
	string contactnum;
	string date;
	float dayrate;
	float hourrate;
	float overtimerate;
	int employeenum;
};
struct employeenumber{
	employeenumber(): employid(1){
		ifstream infile("Employee.txt");
		infile >> employid;
	}
	~employeenumber(){
		ofstream outfile("Employee.txt", ios_base::out | ios_base::trunc);
		outfile << employid;
	}
	int operator()() { return employid++; }
	int employid;
};
void addemployee() {
	employeeinfo ok;
	employeenumber employeenum;
	char answer;
	ofstream outfile;
	ifstream infile;
	outfile.open("Employee.txt", fstream::app);
	if (outfile.fail())
	{
		cout << "File Does not Exist" << endl;
		system("pause>0");
	}
	do {
		cin.ignore();
		system("cls");
		cout << "ADD EMPLOYEE\n";
		ok.employeenum = employeenum();
		cout << "Employee Number: E" << ok.employeenum << endl;
		cout << "Name: ";
			getline(cin, ok.name);
			namereq(ok.name);
		cout << "Address: ";
		getline(cin, ok.address);
		cout << "Contact Number: ";
		getline(cin, ok.contactnum);
		cout << "Department: ";
		getline(cin, ok.department);
		cout << "Position: ";
		getline(cin, ok.position);
		cout << "Date Started: ";
		cin >> ok.date;
		cout << "Rate per Day: ";
		cin >> ok.dayrate;
		ok.hourrate = ok.dayrate / 8;
		cout << "Rate per Hour: " << ok.hourrate << endl;
		ok.overtimerate = ok.hourrate * 1.5;
		cout << "Overtime Rate: " << ok.overtimerate << endl;
		cout << fixed;
		outfile << "Employee Number: E" << ok.employeenum << endl;
		outfile << "Name: " << ok.name << endl;
		outfile << "Address: " << ok.address << endl;
		outfile << "Contact Number: " << ok.contactnum << endl;
		outfile << "Department: " << ok.department << endl;
		outfile << "Position: " << ok.position << endl;
		outfile << "Date Started: " << ok.date << endl;
		outfile << "Rate per Day: " << setprecision(2) << ok.dayrate << endl;
		outfile << "Rate per Hour: " << setprecision(2) << ok.hourrate << endl;
		outfile << "Overtime Rate: " << setprecision(2) << ok.overtimerate << endl << endl;
		cout << "Would you like to ADD another EMPLOYEE? [Y/N]";
		cin >> answer;
	} while (toupper(answer) == 'Y');
	outfile.close();
	menu();
}
Last edited on
1
2
3
4
void addemployee() {
//...
	menu();
}
let me guess, in `menu()' you present the user with several choices and according to his input you call the correspondent function, like `addemployee()'


apart from that, the "Employee.txt" file it's being used for two things:
- as storage for `employid' of the ¿singleton? class `employeenumber'
- as a database for all `employeeinfo'
notice that you wipe out the file in `~employeenumber()' so those two things are not compatible.
1
2
3
4
void addemployee() {
//...
	menu();
}

let me guess, in `menu()' you present the user with several choices and according to his input you call the correspondent function, like `addemployee()'


apart from that, the "Employee.txt" file it's being used for two things:
- as storage for `employid' of the ¿singleton? class `employeenumber'
- as a database for all `employeeinfo'
notice that you wipe out the file in `~employeenumber()' so those two things are not compatible.


Yes, the menu() have the options like the addemployee().

Yes it does store the class mentioned and a database for the whole 'employeeinfo'.
So the code that I made that includes `~employeenumber()' wipes out the file? I thought what I'm doing in my struct is scanning the file and displays a value that iterated the latest ID number? if not, what are the options that I have to have the desired output?
Last edited on
you were doing something like this
1
2
3
4
5
6
7
menu()
   addemployee()  //1
      menu()
         addemployee()  //2
            menu()
               addemployee()  //3
                  ...
the thing is that when a function ends, the program returns to its caller.
so going bottom-up, menu() ends, and goes back to addemployee(), which ends, writes a 3 to the file and goes back to menu().
that menu() ends and goes to addemployee(), which ends, writes a 2 to the file and goes back to menu().
the last `addemployee()' to end will write a 1 to your file, and thats what your program sees the next time you execute it.

instead, you may do something like
1
2
3
4
5
main()
   while(true){
      menu()
         addemployee()
   }
`menu()' calls `addemployee()', after both end the control goes back to main() which calls menu() agains because it's inside a loop.


> So the code that I made that includes `~employeenumber()' wipes out the file?
1
2
~employeenumber(){
   ofstream outfile("Employee.txt", ios_base::out | ios_base::trunc);
yes, that's the meaning of the `trunc' option


> what are the options that I have to have the desired output?
I'll suggest you to get rid of `employeenumber' class. (you may use a statict member variable to keep track of the increasing id)
work with a container that will store all the employees and write it to a file just at the end of your program.
Last edited on
Thanks for the help I have the codes to use and it works fine thank you for helping I reakky appreciate it just took a while to understand but still thank you.
Topic archived. No new replies allowed.