Loading and printing function

I can´t see whats wrong this code, after cout << "Sort order by :" << endl;
it stops and doesn´t print out anything. Can someboy help me.

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
  void load(vector<Konto>&konton)
{ 

	ifstream fin("Bank.txt");

	int kontonummer;
	string innehavare;
	double saldo;
	double rantesats;

	konton.clear(); // Clear the contents of konton

	while(fin >> kontonummer >> innehavare >> saldo >> rantesats) 
	{
		konton.push_back(Konto(kontonummer, innehavare, saldo, rantesats));
	}

	BubbleSort(konton);
	cout << "Sort order by :" << endl; // Here it stops and refuses to print
	for (unsigned int i = 0; i < konton.size(); i++)
	{
			cout << "Kontonummer: " << konton[i].Kontonummer() << endl;
			cout << "Innehavare: " << konton[i].Innehavare() << endl;
			cout << "Slutligt saldo: " << konton[i].Saldo() << endl;
			cout << "R\x84ntesats: " << konton[i].Rantesats() << endl << endl;
	}

	cout << "\n" << endl;
}
The obvious guess is that the for loop is skipped over. How do you know, absolutely for sure, that the for loop is being entered?

Add some more debug output. On line 17, output the size of the vector konton. On line 27, output some text so you know it's past the for loop.
Last edited on
Yes, the for-loop is skipped but I don´t know why. The konto.size() returns a zero. Test1, Test2, and Test3 is printed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	cout << "Test1" << endl;
	BubbleSort(konton);
	cout << "Test2" << endl;
	for(unsigned int i = 0; i < konton.size(); i++)
	{
		cout << "Kontonummer: " << konton[i].Kontonummer() << endl;
		cout << "Innehavare: " << konton[i].Innehavare() << endl;
		cout << "Saldo: " << konton[i].Saldo() << endl;
		cout << "R\x84ntesats: " << konton[i].Rantesats() << endl << endl;
	}

	cout << "\n" << endl;
	cout << "Test3" << endl;
	cout << konton.size() << endl;
Last edited on
Your for loop will keep looping as long as i is less than the size of konton.

i begins with a value of zero. konton has a size of zero. So the first time, is i less than zero? No, it is not. So how many times will we go around the for loop? Zero times.
Yes, the for-loop is skipped but I don´t know why. The konto.size() returns a zero

Your answer is in your question no?

If it returns 0, why would the for-loop run? Look at the condition.

for(unsigned int i = 0; i < konton.size(); i++)

0 is not bigger than 0, therefor this loop is obviously being skipped.

You need to find the source of the problem, which is why the size of konto is 0.
konto's size is zero because it is explicitly emptied
konton.clear(); // Clear the contents of konton
and nothing gets put into it after that.

If I had to guess, the file bank.txt is not in the working directory of the program.
Last edited on
It seems like it is the while-loop

1
2
3
4
	while(fin >> kontonummer >> innehavare >> saldo >> rantesats) 
	{
		konton.push_back(Konto(kontonummer, innehavare, saldo, rantesats));
	}


It doesnt start and I don´t know why.
Last edited on
I expect because there's a problem with fin.

I expect because Bank.txt is not in the working directory of the program.
Last edited on
Topic archived. No new replies allowed.