[RESOLVED] My For Loop Will Not Start

Sorry to dump ~20 lines of code on you guys, but this is a function from a bigger program I'm working on that counts votes from a text file.

The file goes like so: it starts with an ID, ten Y/N votes on bills, and then a vote on a new captain (it's a pirate themed problem, I guess).
Example:
2YNYNYNYNYNThe New Guy
4YYYYYYNNYYThe Captain
29YYYYYYNYYNThe Captain
etc.

I've stored the votes in an array of strings called lineNoDupes. The array of chars called filecontents searches each string character by character in order to increment votes, and this particular function counts the votes for "The New Guy" or "The Captain".

My problem is that the for loop in the beginning of the function does not start. The cout in the beginning is to make sure the function is being called successfully, which it is.

I am using a lot of global variables just for convenience's sake.

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
void countElections()
{
    cout << "countElections();\n";
    char filecontents[20];
    for( int i = 0; i < numElements; i++ )
    {
        strcpy( filecontents, lineNoDupes[i].c_str() );
        if( i < 8 ) //the first 8 elements have single digit IDs, so they're tested differently
        {
            cout << filecontents[15] << endl; //filecontents[15] is where the N or C will be in the candidate name
            if( filecontents[15] == 'N' ) //no need to test more than the 'N' in new guy or 'C' in captain
                newGuyVotes++;
            else if( filecontents[15] == 'C' )
                captainVotes++;
        }
        if( i > 8 ) //these are all double digit IDs
        {
            if( filecontents[16] == 'N' ) //no need to test more than the 'N' in new guy or 'C' in captain
                newGuyVotes++;
            else if( filecontents[16] == 'C' )
                captainVotes++;
        }
    }
    cout << "CAPTAIN VOTES: " << captainVotes << "\n";
    cout << "NEW GUY VOTES: " << newGuyVotes << "\n";
}
Last edited on
Where is numElements coming from? If that's 0, then the code in the for loop won't execute.
And this is probably the biggest thing wrong with your program:
I am using a lot of global variables just for convenience's sake.
numElements is a const global variable. It's initialized to 55 and could not have changed.
Last edited on
Do you verify that the file is successfully opened and the lineNoDupes array contains the file data?
Yes. This function is actually just a modified version of another function which counts the Ys and Ns.

This one works perfectly in the same program. Which is odd because it's way more complex and has nested for loops but w/e.

This might be a glitch in code::blocks?

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
void countAmendments()
{
    cout << "countAmendments();\n";
    char filecontents[20];
    for( int i = 0; i < numElements; i++ )
    {
        strcpy( filecontents, lineNoDupes[i].c_str() );
        if( i < 8 ) //the first 8 elements have one digit IDs and are tested differently
        {// I understand that the y variable is obtuse, but it just counts from 1 to 10 without involving a third loop
            for( int j = 1, y = 2; j < 20; j++, y+=2 )
            {
                if( filecontents[j] == 'Y' )
                    AmY[(y/2)-1]++;
                else if( filecontents[j] == 'N' )
                    AmN[(y/2)-1]++;
            }
        }
        if( i > 8 ) //these are all double digit IDs
        {
            for( int j = 2, y = 2; j < 20; j++, y+=2 )
            {
                if( filecontents[j] == 'Y' )
                    AmY[(y/2)-1]++;
                else if( filecontents[j] == 'N' )
                    AmN[(y/2)-1]++;
            }
        }

    }
    for( int i = 0; i < 10; i++ )
        cout << AmY[i] << "\t";
    cout << "\n";
    for( int i = 0; i < 10; i++ )
        cout << AmN[i] << "\t";
}


Also, I edited out numElements and replaced it with the number 55. The loop still does not run. I tried doing a cout in the first line of the loop to verify whether or not the loop was running, and it did not print anything. So the loop itself is not even starting.
Last edited on
I sacrificed some readability, but I integrated the two functions together and it worked.

So technically I did not resolve the issue, but I'm marking it solved.
Topic archived. No new replies allowed.