writing data into the array

I am having trouble debugging this code so it reads two columns from the file and when the first column (Department is the same it just adds the second column to the old dept already created)This code is having trouble with looping. The output should be a partially filled array with only few departments. Any walk through, help would be much appreciated ! Thanks.

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

#include <iostream>
#include <fstream>

using namespace std;

ifstream inputFile;                   //stream object


int main()
{
	inputFile.open("text.txt");

	const int SIZE = 15;
	int candy[SIZE];
	int dept[SIZE];
	int valuecounter = 0;
	int Candy;
	int Department;
	int index = 0;

	while (inputFile >> Department >> Candy)
	{
		// Update previous values
		for (int index = 0; index < valuecounter; index++)
		{
			if (dept[index] == Department)
			{
				candy[index] += Candy;
				break;
			}
		}
		if (index == valuecounter)
		{
		dept[valuecounter] = Department;
		candy[valuecounter] = Candy;
		valuecounter++;
		}
	}

	for (int i = 0; i < valuecounter; i++)
		cout << dept[i] << "  " << candy[i] << endl;

	inputFile.close();
	return 0;

}

910  8
450  9
750  10
150  35
750  19
150  18
910  19
390  19
520  6
110  78
300  23
110  1
110  5
120  6
150  16
300  23
110  1
110  5
120  6
150  16
Last edited on
Consider what happens when you DO find the department. At line 31, you increment candy[index]. Then when you exit the loop, you still execute the code to add the department as though you DIDN'T find the department.

To fix this, (1) make index a local variable within main (right now it's local to the for loop. (2) Add a break statement after line 31 Then put lines 36-38 inside if (index == valuecounter) {

The break statement ensures that the loop will exit when it finds the department. The new "if" statement really says "if I didn't find the department...."

hello! thank you for fast response! so I did as you suggested and I actually understand the process! but the code seems to be off a little still. When I try to run it it only adds and shows dept 910 27. It looks like other departments are skipped.
Please post your current code.
the current code is posted
In future please edit your posts to modify the code, add the code to a new post. When you alter the code it makes following the topic harder.

Instead of using index and valuecounter in the if() statement after the loop I recommend using a flag variable instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
	while (inputFile >> Department >> Candy)
	{
                bool found = false;
		// Update previous values
		for (int index = 0; index < valuecounter; index++)
		{
			if (dept[index] == Department)
			{
				candy[index] += Candy;
                                found = true;
				break;
			}
		}
		if (!found)
...
yes that's what I needed!!! Thank you!
Topic archived. No new replies allowed.