Beginner for loop problems ):

Hi, my code seems to stop running(I get a process error and it prompts me to shut down the program) after the first for loop.

Could someone help me pinpoint my problem? Also, would there be a way to increment years once in a second for loop instead of me creating 3 for loops? Like maybe nesting them or something along those lines?

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
#include <iostream>

using namespace std;


int main()
{
    const char * months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    int year = 0;
    int month = 0;
    int sales[3][12];
    int sum[3] = { 0, 0, 0 };
    int overall = 0;

    for(int i = 1; i<=12; i++)
    {
        cout << "Enter data for " << months[month] << " : ";
        cin >> sales[year][month];
        sum[year] += sales[year][month];
        cout << endl;
        ++month;
    };

    ++year;

    for(int b = 1; b<=12; b++)
    {
        cout << "Enter data for " << months[month] << " : ";
        cin >> sales[year][month];
        sum[year] += sales[year][month];
        cout << endl;
        ++month;
    };

    ++year;

    for(int c = 1; c<=12; c++)
    {
        cout << "Enter data for " << months[month] << " : ";
        cin >> sales[year][month];
        sum[year] += sales[year][month];
        cout << endl;
        ++month;
    };

    for(int f = 0; f <= 2; f++)
    {
        overall += sum[f];
    }




    cout << "# of books sold during year 1 : " << sum[0];
    cout << "\n# of books sold during year 2 : " << sum[1];
    cout << "\n# of books sold during year 3 : " << sum[2];
    cout << endl;
    cout << overall << " is the total # of sales. ";
    cin.get();
    return 0;
}
closed account (zb0S216C)
You're overstepping the boundaries of months. There're a few thing wrong with your code.

1) The <= operator in each of the for loops should be <. Every index is offset by one. For example, 0 (first), 1(second), etc. By using the <= operator, you attempt to access the 12th element of months, which doesn't exist.

2) You never reset month. After the first for loop has finished, month contains 12. Then, within your second for loop, you increment month again. Again, month is incremented another 12 times. Since you never reset month, you're writing to unknown memory. The index range of months is 0 -to- 11.

To fix this code, you need to do the following:

- After each for loop, reset month to 0.
- Change each comparison statement within each for loop with: x < 12;

Wazzak
1) Argh, so obvious >=(

2) I thought the changes i made to months only applied to it inside the for loop?

Thank you for your help Framework
It's all in one function, whatever you do it in a loop, changes it for the whole function.
@ResidentBiscuit

What about variables? Those declared within a loop only exist in that loop, correct?
That's correct.
No they will still exist in that function, with the exception of the variable created in a for() statement. There's not really any point on declaring a variable inside a loop though
closed account (zb0S216C)
An object (variable, or whatever floats your boat) declared within the scope[1] of a function or loop construct is local. Allow me to elaborate further with this code segment:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void function( void )
{
    int local_variable( 10 );

    // ...
}

int main( )
{
    local_variable = 44; // What? What's 'local_variable' when its at home?

    for( int counter( 0 ); counter < 5; counter++ )
    {
        // Here, 'counter' is local to this 'for' loop. 'counter' doesn't exist outside this construct.
        // ... 
    }

    counter++; // I'm sorry? What's this 'counter' you speak of? 

    return 0;
}

If you compile this code you'll receive two errors; both errors will refer to counter++ and local_variable = 44.

References:
[1]http://msdn.microsoft.com/en-us/library/b7kfh662(v=vs.80).aspx


Wazzak
Last edited on
But lets say you do

for (x = 1, x < 5, x +=)
{
int number = 0;
other statements
}

what would the scope of number be? I dont know why anyone would declare a variable like this, or if it's even possible, but I am curious
@Framework, thank you for further clarification.

@Resident What i meant by a variable being declared in a loop was something along the lines of..

 
for (int i = 0; i<5; ++i)


where the declared integer is in the initialization part of the for loop.
@ResidentBiscuit: It is certainly legal. The scope of number would be the for loop braces.

@georgewashere: The scope of that variable is the loop as well, as you asked before.
Wouldn't it re-initialize every time it loops though? Like, it'll initialize the variable the first time, then it will redo it, or it will try to make another variable of the same name? I am just confused how it is working underneath the code I guess
Wouldn't it re-initialize every time it loops though?
That is the exact reason you would want to use it in a loop like that - you will find that sometimes you want a value that resets with each loop iteration, and this is the way to do it.
Wouldn't it be better to have it declared outside the loop and just have the value reset at the beginning of the loop?
closed account (zb0S216C)
ResidentBiscuit wrote:
Wouldn't it be better to have it declared outside the loop and just have the value reset at the beginning of the loop?

Not always. If you declare a variable outside the loop, it remains on the stack (wasting space) until it's popped.

Wazzak
@ L B

I believe he is asking if per loop, as in

int i = 0;
i < 5? true
statements
i++
i still < 5? true
statements
i++
i still < 5? true
statements
i++





The value of I in

for (int i = 0, i < 5, i++)

does not reset each time it loops. The initialization of the for statement only occurs once during a for loop(at least in the code above)

@ ResidentBiscuit


It doesn't re-initialize. That would make no sense and defeat the purpose of the loop unless your intention was a never ending loop.
Last edited on
I still don't think your understanding what im trying to say. Lemme break it down, I might be explaining weirdly. Ok so let's say we have a for loop:

for (int x = 0, x < 5, x++) //this is NOT the variable I am talking about
{
double y = 0.0; //this IS the variable I am talking about
other statements...
}

Now "double y = 0.0" will be read each time this loop passes correct?

So let's think this through a couple loops...

First pass:
initialize variable double y with value 0.0
other statements....

Second pass:
Now here, will it reinitialize double y back at 0.0, or does it just ignore this after the first time through?
other statements...


That is my question. I hope that better explains what I am getting at
It at the end of each iteration it deletes it, then at the beginning it makes a new one. Compiler optimizations may change this but the behavior you get from it will always be that of it being reinitialized.
Topic archived. No new replies allowed.