Adding at the end of file

Hello, everyone, I should make a program that makes a structure save it in a file and then add new recordings to the structure and add them at the end of the file but somehow my second void is not working properly or it is incorrect. Also somehow the first line in the file is always empty and the recordings and written after line 2. Can someone tell me where are my mistakes?

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
  #include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
struct employee
{
    string EGN;
    string name;
    string position;
    int intership;
    float salary;
}a[20];
void create(int i);
void add(int &i);

int main()
{
    int i;
    create(i);
    add(i);
}
void create(int i)
{
    int n;
    ofstream f;
    cout<<"Enter the number of employees: ";
    cin>>n;
    f.open("Employees.txt");
    for(i=0;i<n;i++)
    {
        cout<<"Enter EGN: ";
        getline(cin,a[i].EGN,',');
        cin.get();
        cout<<"Enter name: ";
        getline(cin,a[i].name,',');
        cin.get();
        cout<<"Enter position: ";
        getline(cin,a[i].position,',');
        cout<<"Enter intership: ";
        cin>>a[i].intership;
        cout<<"Enter salary: ";
        cin>>a[i].salary;
        f<<a[i].EGN<<", "<<a[i].name<<", "<<a[i].position<<", "<<a[i].intership<<" "<<a[i].salary<<endl;
    }
    f.close();
}
void add(int &i)
{
    fstream f;
    f.open("Employees.txt",ios::app);
    while(cin)
    {

        cout<<"Enter EGN: ";f.get();
        getline(cin,a[i].EGN,',');
        cin.get();
        cout<<"Enter name: ";
        getline(cin,a[i].name,',');
        cin.get();
        cout<<"Enter position: ";
        getline(cin,a[i].position,',');
        cout<<"Enter intership: ";
        cin>>a[i].intership;
        cout<<"Enter salary: ";
        cin>>a[i].salary;
        f<<a[i].EGN<<", "<<a[i].name<<", "<<a[i].position<<", "<<a[i].intership<<" "<<a[i].salary<<endl;
    }
    f.close();
}
Last edited on
Hello Delcho,

What I see in your "add" function is:

Line 50 you define "f" as an "fstream". Then in line 51 you open the file, but never say if it is for input or output. Either change line 50 to an "ofstream" or change line 51 to:
f.open("Employees.txt", std::ios::out || std::ios::app);.

Line 52 is not a good idea because there is nothing inside that might cause "cin" to fail or any other way out. It would basically be an endless loop.

The "f.get()" at the end of line 55 should not be needed.

Line 56 would work better as getline(cin, a[i].EGN);. Remove the ",". Then the following "f.get()" would not be needed to clear the input buffer of the "\n".

The "add" function would work better more like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void add(int &i)
{
	fstream f;  // <--- You could change this to "ofstream" and leave the open statement as is.
	std::string cont;

	f.open("Employees.txt", std::ios::out || std::ios::app); // <--- As an fsream you need to say if it is for in, out or both.

	while (1)
	{

		cout << "Enter EGN: ";
		std::getline(std::cin, cont);
		if (cont == "end" || cont == "End")  // <--- Use anything you like for "end"
			break;

		getline(cin, a[i].EGN);  // <--- The input will be stored in the array when enter is presed.
		//cin.get();  // <--- This line no longeer needed.

		cout << "Enter name: ";


Hope that helps,

Andy
Thanks, but I want in the while loop to input until I press ctrl+z
Hello Delcho,

I want in the while loop to input until I press ctrl+z
That is fine. My suggestion was based on not knowing this.

You can ignore that part. The rest is still good.

Andy
Topic archived. No new replies allowed.