Why is my program outputting garbage here?

Sep 26, 2018 at 5:37pm
I write 1,2,3, etc, up to 24, and it outputs the 24 numbers fine, but then it outputs a load of garbage numbers like 37960 or a bunch of 0's. Why is the program outputting garbage numbers after the end of the loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    cout << "Input the temperatures for each month out of two years." << endl;
    int temperatures[12][2]; 
    for(int i = 0, j = 0; i < 24; i++)
    {
        if(i == 11) {j = 1;}
        cin >> temperatures[i][j];
    }
    for(int i = 0, j = 0; i < 24; i++)
    {
        if(i == 11) {j = 1;}
        cout << temperatures[i][j] << endl;
    }
}
Sep 26, 2018 at 5:42pm
You are probably printing outside of the array, most likely. If you have a good debugger you can watch the variables and their values.
Sep 26, 2018 at 5:52pm
Im pretty new myself, but in your loop, you never reset i. After j gets set = to 1, its inputting values for i12 j1, i13 j1, i14 j1 not i0 j1 i1 j1 i2 j1
etc.

Try this:
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
#include <iostream>
using namespace std;

int main()
{
    cout << "Input the temperatures for each month out of two years." << endl;
    int temperatures[12][2]; 
    for(int i = 0, j = 0; i < 12; i++)
    {
      
        if(i == 11) {
            if(j==0) {
                j = 1; i=0;}
                }
        cin >> temperatures[i][j];
    }
    for(int i = 0, j = 0; i < 12; i++)
    {
        if(i == 11) {
            if(j==0) {
                j = 1; i=0;}
                }
        cout << temperatures[i][j] << endl;
    }
}
Last edited on Sep 26, 2018 at 6:16pm
Sep 26, 2018 at 6:02pm
Thanks syvel. In heindsight it was really dumb for me to not have realized that I guess. Reminds me of when I kept redefining a number during a rock paper scissors' while loop and couldn't figure out why the number of wins was equal to 0.

Also, I found a better syntax, disregarding your preference for brackets after the parentheses instead of under them:
1
2
3
4
5
6
7
for(int j = 0; j < 2; j++)
{
    for(int i = 0; i < 12; i++)
    {
        //theCode
    }
}
Last edited on Sep 26, 2018 at 6:34pm
Sep 26, 2018 at 11:50pm
I pasted your code exactly and got a different error, but your program has no stop in it, so it doesn't know what to do after the loop is executed.

return 0;
Oct 29, 2018 at 1:05pm
Inspireftw wrote:
your program has no stop in it, so it doesn't know what to do after the loop is executed.

return 0;

This makes no sense. When the loop ends, execution will proceed to the next statement in the function. When the last line in the main function has executed, the main function will return and the program will exit.

In the main function (and only the main function), if there is no return statement, then 0 is automatically return.

@Inspireftw, in every single thread in which you have posted, you have given "advice" which is nonsense, and have had to be corrected by people who know better. You're starting to look very like a troll. If you continue to behave this way, you will be treated as one.
Last edited on Oct 29, 2018 at 1:06pm
Topic archived. No new replies allowed.