Why is my programming running forever?

Got it
Last edited on
What happens if you change to an "if" statement?
Last edited on
Delete
Last edited on
I don't have access to a compiler atm, but I would try to cout the variables.
I would cout the variables at the beginning of the for-loop as such:

1
2
3
4

cout << "subjectCode  = " << subjectCode << endl;
cout << "subjectCodes[i].name = " << subjectCodes[i].name << endl;
//...etc...for all the variables that might change in the loop. 



another thing I would try is to store subjectCodes.size() into a variable. Then the for-loop would look like this :

1
2
3
for (int i = 0; i < size; i++){
//code goes here
}


....because the size of subjectCodes increase. So maybe if you put subjectCodes.size() as the ending parameter, the size increases every time, so the ending parameter is never met.

Idk those are just my guesses.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool found_it = false ;

for (int i = 0; i < subjectCodes.size(); i++)
{
    if (subjectCode == subjectCodes[i].name)
    {
          subjectCodes[i].count++;
          found_it = true ;
          break ; // found it, nothing more needs to be done
    }
}

if( !found_it ) // did not find it; add a new item to the vector
                // and do this just once, not each time through the loop 
{
    sc.name = subjectCode;
    sc.count++;
    subjectCodes.push_back(sc);
}
Topic archived. No new replies allowed.