2D Array

I'm suppose to read a file in(which is in hex), store the data in a 2d array, and write the data in binary to another file. I think that my input and output file are good but I am wondering if I did the print in binary correct with the bitset. I know for sure that my main() function is all screwed up because I am not storing the data correctly. Should I by putting my input function before the creating the 2d array?

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
62
63
64
65
66
67
68
69
70
#include <fstream>
#include <iostream>
#include <bitset>
using namespace std;

// Funtion definitions 
void getInput();				// function that gets input file name 
void getOutput();			// function that gets output file name
void Printoutput();			// function that prints to output file

// Variable definitions
int rows, cols;
ifstream fin;
ofstream fout;
bool outputToFile = "False";
string outputfile = "output.txt";	
string inputfile = "input.txt";

int main()
{
	int**data = new int*[rows];			// Creating the array 
	
	// getInput();					// function that gets input file 
	// getOutput();						// function that gets output file 
	
	for(int i = 0; i < rows; i++) 
		data[i] = new int[cols];
	
	getInput();
	getOutput();
	Printoutput();					// function that prints hex numbers in binary 
}

void Printoutput()		// Function that prints the data in binary 
{
	for(int i = 0; i < rows; i++)
		for(int j = 0; j < cols; j++)
		{
			std::bitset<4> x(j);
			std::cout << x;	
		}
}

void getInput()				// function that gets input file name 
{
	start:;
	cout << "Enter a file name: " << endl;
	cin >> inputfile >> hex>> data[i][j];		// Puts the hex numbers into an 2d array
	fin.open (inputfile.c_str());
	
	if (fin.fail())
	{
		cout << "The input file cant be opened." << endl;
		goto start;		// If file fails to open it repeats the question 
	}
}

void getOutput()				// function that gets output file name
{
	start:;
	cout << "Enter an output file names: " << endl;
	cin >> outputfile;
	fout.open (outputfile.c_str());
	
	if (fin.fail())
	{
		cout << "The output file cant be opened." << endl;
		goto start;		// If file fails to open it repeats the question 
	}
}
Last edited on
You'll definitely want to get the size of the array you're allocating before you allocate it. Global variables are zero-initialized, so you're are currently allocating an array with 0 elements.

You need a [/code] tag at the end of you code to finish formatting the OP. (Feel free to edit the post and add it.)
But the point of the program is that I don't have a set size of the array so why would I want to declare it a certain size?
I didn't say to declare it a certain size.

At some point you have to determine what the size is that you're allocating. Figure that out before you allocate it.

You can't just say "Oh magical compiler, please read my mind and allocate an array of size X when I tell you to allocate an array with no elements."

Now, it may be that you start out with an array of size 1 and increase the size by allocating a new array and copying the old one to it every time you determine you need another element, but you have to explicitly do this. The compiler cannot do it for you.

Things would be much easier if you used a std::vector.
Topic archived. No new replies allowed.