Using For loop to loop thru struct arrays

Hi, I cant figure out how to get the years 298,299,300 each, one time. everytime i do i get 298,299,299. i am passing arrays and struct arrays. Let me me know if you need anymore info.

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
 
// header file
void determineBattPerYear(BattlesPerYear numbBat[], int year[]);Paste

string Write( BattlesPerYear numbBat[]);

//struct 
struct BattlesPerYear {

int battles;

int year;

};


int main(){

char int SIZE= 9;

int year[SIZE] = {298,298,298,298,299,299,300,300,300};

battlesPerYear numbBat[SIZE];

determineBattPerYear(numbBat, year)

Write( numbBat);

}

// Here I am trying to loop thru each size of each year at i and if the year doesint = its self than the year at h is = to the year at i.

determineBattPerYear(numbBat, int year[]){

numbBat[0].year = year[0];

for (int i = 1; i < SIZE; i++) {

for (int h = 1; h < 3; h++) {

if (year[i] != year[i-1]) {

numbBat[h].year = year[i - 1];

}

if (year[i] != year[i]) {

numbBat[h].year = year[i];

}

}
// here I am storing the info in a string stream

string Write(Battles battles[], BattlesPerYear numbBat[]) {
	stringstream ss;
	string output;
	for (int i = 0; i < 3; i++) {

        ss << "Year, " << numbBat[i].year << endl;
	}
	
	output = ss.str();

	return output;
}
Last edited on
What are you trying to do on line 47? Comparing something with itself can impossibly be not equal.

What is determineBattPerYear(...) supposed to do?
I can't make much sense of your code, but one of these ideas might help you...

for(year = 298; year <= 300; year++)
do something
or
for(i = 0; i < size; i++)
dostuffwith(year[i]);

i see you looping from 1 to < size, but its 0 in c++ not 1
but you are also doing some sort of -1 backtracking. I can't tell if you know what you are doing or not. It could be correct to start with 1.

battlesPerYear numbBat[SIZE]; //your struct is not spelled the same. c++ cares about case.
Last edited on
Topic archived. No new replies allowed.