Output from array is incorrect. Help please

Thanks for taking the time to read my post. I am writing a program where I read in data from a file into an array and a 2D array. However, when I cout that data to insure that it was all read in correctly, I get only the first full line of that input file(where there are actually 25 rows and 12 columns).

What am I doing wrong or should be doing differently? Thanks again.

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
ifstream fin;
    //open the input file
    fin.open("store_data.txt");

    //If input file was opened, read input file data into array and 2d array
    if(fin){
        for (int i = 0; i < NUM_STORES; i++){
            fin >> storeID[i];

            for(int j = 0; j < MONTHS; j++){
                fin >> sales[i][j];
            }
            return true;
        }
    //if file was not opened, return false.
    }else
        return false;

 ofstream fout;
    //Open output file
    fout.open("prog9_out_cal155.txt");
    if(fout){
        for (int i = 0; i < NUM_STORES; i++){
            cout << storeID[i] << " ";

            for(int j = 0; j < MONTHS; j++){
                cout << sales[i][j] << " ";
            }
                cout << endl;

        return true;
Try counting your brackets.
Nope. Brackets are fine.
Nope. Braces are not fine.
7
8
9
10
11
12
13
14
        for (int i = 0; i < NUM_STORES; i++){
            fin >> storeID[i];

            for(int j = 0; j < MONTHS; j++){
                fin >> sales[i][j];
            }
            return true;
        }

That return true; is part of the for loop you started on line 7. As such, that for loop will run only once. I am assuming that you misplaced the bracket on line 14 and that you wanted to have the loop run more than once.

I'm also assuming that lines 19 and below are part of another function because otherwise it makes no sense to have them since they never run.
Last edited on
Ah, I thought it meant that my braces didnt pair up correctly with each other.

Thanks for the input! Ill code it in and give it a shot. :)
That did it! Thanks for the comments and the help!

If Im understanding the logic correctly on this, it loops once but returns, and then it wont loop again because it has returned, correct?

Therefore, that is why the return true(or false) is required to be outside of any loop.
Ah, I thought it meant that my braces didnt pair up correctly with each other.

I did think you were missing a bracket at first, since I didn't notice the bracket in front of else.

If Im understanding the logic correctly on this, it loops once but returns, and then it wont loop again because it has returned, correct?

Correct.
Topic archived. No new replies allowed.