Loding list from text file, adding new data to it erases the list and creates a new

I have a list of accounts (konton) where i save the values in a text file and use in a vector. And have to add a new customer to the existing/loaded list with this function, instead it erases the old list and I have to start a new one.
I don´t know why that is in that way, but when I starting from scratch with a new list there is no problem adding new holders to the bank and the new list started.

Wordlist:
konto = account
nummer = number
fnamn = firstname
enamn = lastname
saldo = debt
rantesats = interest

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
// The save function looks like this:

{
	cout << "Spara konton" << endl;
	ofstream outFile;
	outFile.open("Bank.txt");
	for (unsigned int i = 0; i < konton.size(); i++)
	{
		outFile << konton[i].Nummer() << " " << konton[i].Fnamn() << " " << konton[i].Enamn() << " " << konton[i].Saldo() <<  " " << konton[i].Rantesats() << " " << endl;
	}
	outFile.close();				
}

// Then, when I have to load the information in the vector that has been saved with this function:

void load(vector<Konto>&konton)
{ 
	ifstream fin("Bank.txt");
	
	int nummer;
	string fnamn;
	string enamn;
	double saldo;
	double rantesats;

	konton.clear(); // Clear the contents of myPhonebook
	
	while(fin >> nummer >> fnamn >> enamn >> saldo >> rantesats) 
	{
		konton.push_back(Konto(nummer, fnamn, enamn, saldo, rantesats));
	}

	BubbleSort(konton);
	
	for (unsigned int i = 0; i < konton.size(); i++)
	{
		cout << "Kontonummer: " << konton[i].Nummer() << endl;
		cout << "Namn: " << konton[i].Fnamn() << " " << konton[i].Enamn() << endl;
		cout << "Saldo: -" << konton[i].Saldo() << "%" << endl;
		cout << "R\x84ntesats: " << konton[i].Rantesats() << endl << endl;
	}
	cout << "\n" << endl;
}


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
// And have to add a new customer to the existing/loaded list with this function, instead it erases the loaded list and I have to start a new one.
// I don´t know why that is in that way, but when I starting from scratch with a new list there is no problem adding new holders to the bank and
// the new list started:

void add_account(vector<Konto>&konton)
{
	string fnamn;
	string enamn;
	int nummer;
	double saldo;
	double rantesats;

	cout << "Skriv in kontonummer: " << flush << endl;
		
	// Enter values into vector
	cin >> nummer;

	for(unsigned int i=0; i < konton.size(); i++)
	{
		if (nummer == konton[i].Nummer())
		{
			cout << "Konto existerar redan." << endl << endl;
			return;
		}
	}

	// Flush makes sure that the buffer is cleared and the characters are written to their destination
	cout << endl << "Skriv namn p\x86 innehavaren av kontot: " << flush << endl << endl;
		
	// Enter values into vector
	cout << "Skriv in f\x94rnamn: " << endl;

	cin >> fnamn;

	cout << "Skriv in efternamn: " << endl;

	cin >> enamn;

	cout << endl << "Skriv in saldot: " << flush << endl;
		
	// Enter values into vector
	cout << "-";
	cin >> saldo;

	// Flush makes sure that the buffer is cleared and the characters are written to their destination
	cout << endl << "Skriv in r\x84ntesatsen per \x86r i %: " << flush << endl;
		
	// Enter values into vector
	cin >> rantesats;

	cout << endl;
	cout << "Nytt konto skapat:" << endl;
	cout << "Kontonummer: " << nummer << endl;
	cout << "Namn: " << fnamn << " " << enamn <<  endl;
	cout << "Saldo: -" << saldo << endl;
	cout << "R\x84ntesats per \x86r i %: " << rantesats << "%" << endl << endl;

	konton.push_back(Konto(nummer, fnamn, enamn, saldo, rantesats));
}


Last edited on
The code that you've shown all looks fine.

I suggest that you move the code that prints the konton array from load() to a separate print() function. Then call it at the end of add_account(). You should find that the new record shows up just fine. Sprinkle the rest of your code with calls to print() to find out where things are really going wrong.
Yes and it adds a new aoccount. But there is one problem with it. If a list is loaded from the text-file the void add_account(vector<Konto>&konton) erases the list and adds the new account over the deleted old ones. I need it to preserve the old list and keep the old list intact and add the new account to it. I can´t see why this code wouldn´t work for it? What am I doing wrong?

Found an error in the code. Yea! Alrigth and you gusy a couple of working functions. Thanks for the help! / P

Last edited on
1
2
3
    ofstream outFile;
    outFile.open("Bank.txt");
    outFile.open("Bank.txt", std::ios::app); 


By default, opening a file for output truncates it.
Topic archived. No new replies allowed.