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.
#include<iostream>
#include<iomanip>
#include <cstdlib>
#include <fstream>
usingnamespace std;
int main()
{
constint row = 3;
constint 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;
}