2D Array problem

I'm trying to write a program challenge out of this text I bought. The problem is to write a 2d Array to a file. I'm having trouble getting the file to write correctly. The columns are suppose to be 30 characters long and when it tries to write it writes crazy characters. Here is my code:
#include<iostream>
#include<iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;


int main()
{
const int row = 3;
const int col = 30;
char Arr[row][col];
ofstream outputfile;
for (int count = 0; count < row; count ++)
{
for (int index = 0; index < col; index++)
{
cout << "Enter 'R' for rainy, 'C' for cloudy, and 'S'";
cout << " for sunny for day " << index << ":";
cin >> Arr[count][index];
}
}
for (int count = 0; count < row; count++)
{
for (int index =0; index < col; index++)
{
cout << Arr[count][index] << endl;
}
}
outputfile.open("Statistics.txt");

for (int count = 0; count < row; count ++)
{
for (int index= 0; index < col; index++)
{
outputfile << Arr[count][index] << endl;
}
cout << endl;

}
outputfile.close();

return 0;
}
....Also I cannot get it to write row by row instead of one long string of characters. I'm using netbeans compiler. Any help would be appreciated.
closed account (4izT0pDG)
Ive commented the minor changes I've made. I didn't any crazy characters you speak of. I am using GCC's g++ compiler via Code::Blocks IDE.
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
#include<iostream>
#include<iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;

int main()
{
    const int row = 3;
    const int col = 30;
    char Arr[row][col];
    ofstream outputfile;

    for (int count = 0; count < row; count ++)
        {
            for (int index = 0; index < col; index++)
            {
                cout << "Enter 'R' for rainy, 'C' for cloudy, and 'S'";
                cout << " for sunny for day " << index << ":";
                cin >> Arr[count][index];
            }
        }
    for (int count = 0; count < row; count++)
        {
            for (int index =0; index < col; index++)
                cout << Arr[count][index]<<" ";          //Space between each character

            cout<<endl;                                   //New line for each row
        }

    outputfile.open("Statistics.txt");

    for (int count = 0; count < row; count ++)
        {
            for (int index= 0; index < col; index++)
                outputfile << Arr[count][index]<<" ";   //Space between each character

            outputfile<<endl;                           //New line for each row
        }

        outputfile.close();

        return 0;
}

Last edited on
Topic archived. No new replies allowed.