I'm trying to add the values stored in an array.

its printing a big negative number.

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
79
80
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>


using namespace std;


struct ventasType
{
	string idArr[50];
	double quarterSales[50], totalSales[50];
};

void readingID(int& counter) {

	ventasType sales;

	ifstream inFileID;
	counter = 0;

	inFileID.open("id.txt");

	while (!inFileID.eof())
	{
		inFileID >> sales.idArr[counter];
		cout << sales.idArr[counter] << endl;
		counter++;

	}
	cout << counter << endl;
}

void readingSales(int& counter, int& quarterSales, int& totalSales)
{
	ventasType sales;

	ifstream inFileSale;
	string salesID[50];
	int numsID = 0;
	quarterSales = 0, totalSales = 0;
	counter = 0;
	inFileSale.open("ventas.txt");

	while (!inFileSale.eof())
	{
		inFileSale >> salesID[counter] >> sales.quarterSales[counter] >> sales.totalSales[counter];
	//	cout << salesID[counter] << " " << sales.quarterSales[counter] << " " << sales.totalSales[counter] << endl;
		counter++;	
	}
	numsID = counter;
	for (counter = 0; counter <= numsID; counter++)
	{
		if (sales.idArr[counter] == salesID[counter])
		{
			quarterSales = quarterSales + sales.quarterSales[counter];
			totalSales = totalSales + sales.totalSales[counter];

		}

		
	}

	cout << quarterSales << totalSales;


}


int main()
{
	ventasType sales;
	int count, quartSales, totSales;

	readingID(count);
	readingSales(count, quartSales, totSales);

	return 0;
}
Last edited on
Hard to see exactly the issue without knowing what the input file is.

One thing is that you are looping on counter <= numsID. You're probably gone out of range of your values here. Try looping on just counter < numsID.

Second thing is that you're looping on eof(). This is very error prone.
Consider looping on the success of the data extraction itself:
1
2
3
4
while (inFileSale >> salesID[counter] >> sales.quarterSales[counter] >> sales.totalSales[counter])
{
    counter++;
}


1
2
3
4
while (inFileID >> sales.idArr[counter]) {
		cout << sales.idArr[counter] << endl;
		counter++;
}


Also, in modern programming, it's considered a "code smell" to be defining variables before they're actually used. It makes the code harder to read and reason about. It's best to get in a good habit now: in readingSales, you don't use numID until it's assigned in line 52.
So, remove line 41, and on line 52, just do
int numsID = counter;

Second, you are using counter for two different purposes.
First, you're using it to counter the number of sales.
Then, you use it to iterate over the existing data.
This could also be cleaned up.
Last edited on
His biggest problem is that he's defining a new sales object in each function instead of passing in the one defined in main!
*facepalm* yep I missed that...
Topic archived. No new replies allowed.