Reputation:
I'm extremely new to C++ (this is actually the first program I've ever worked on) and am pretty much having to learn it as I write this program and I'm getting stuck.
The program is supposed to read in a text file, load it into an array and assign a character to each number in the array (so for this I'm thinking I'll be creating 2 arrays, an int array with the numbers read in from the file, and a char array with the characters assigned to the numbers). It will then print these two arrays. It's then supposed to go through the initial int array and search for any numbers whose values differ from their neighboring values by more than one, if such a value is found, it's supposed to give the number the value of the average of it's neighboring values. It's supposed to make all these corrections and then assign characters to these corrected numbers and print out both arrays.
I don't really know how to do any of this, but I'll try to narrow my question down to my initial problem. I have no idea how to load the array from the file. My textbook seems to do a good job of covering arrays and files, but it doesn't really bring them together and talk about building arrays from files.
Here is what the file looks like, the first number is the size of the array.
10
7 6 9 4 5 4 3 2 1 0
6 5 5 5 6 5 4 3 2 1
6 5 6 6 7 6 8 4 3 2
1 5 6 7 7 7 6 5 4 3
5 5 6 7 6 7 7 6 5 9
5 6 7 6 5 6 6 5 4 3
5 6 7 9 5 5 6 5 4 3
5 5 6 7 6 6 7 6 5 4
5 9 5 6 7 6 5 0 3 2
5 5 5 5 6 5 4 3 2 7
And here is what code I have so far, although the vast majority of it deals with opening the file. I'm assuming it at least is doing that correctly since when I run it I'm not getting the "Couldn't open file error".
#include <iostream>
#include <fstream>
#include <array>
int main() { // Main will be in prog1_test
//Open file
ifstream prog1File("prog1.dat", ios::in);
//If file can't be opened, exit
if (!prog1File) {
cerr << "File could not be opened" << end;
exit(EXIT_FAILURE);
}
int size;
int numArray[][];
}
While I do have the size and array variables declared, I have no idea if that's done correctly, like I said I'm still really new to this. I know some basic Java, but am having trouble figuring out how things translate across to c++. Any suggestions are greatly appreciated.
Upvotes: 0
Views: 227
Reputation: 2393
I didn't really get the hole problem. I assume that you are having troubles with creating a 2D array and reading it from file. So here is some code that shows how it can be done from my perspective:
#include <iostream>
#include <fstream>
#include <vector>
int main() { // Main will be in prog1_test
//Open file
ifstream prog1File("prog1.dat");
//If file can't be opened, exit
if (!prog1File) {
cerr << "File could not be opened" << end;
exit(EXIT_FAILURE);
}
int size;
prog1File >> size;
int[][] matrix = new int[size][];
for (int i = 0; i < size; ++i)
{
matrix[i] = new int[size];
for (int j = 0; j < size; ++j)
{
prog1File >> matrix[i][j];
}
}
/* do your stuff */
for (int i = 0; i < size; ++i)
{
delete matrix[i];
}
delete matrix;
return 0;
}
Upvotes: 1
Reputation: 418
Just replace ofstream with ifstream and ios::out with ios::in
#include <iostream>
#include <fstream>
#include <array>
int main() { // Main will be in prog1_test
//Open file
ifstream prog1File("prog1.dat", ios::in);
//If file can't be opened, exit
if (!prog1File) {
cerr << "File could not be opened" << end;
exit(EXIT_FAILURE);
}
int size;
int numArray[][];
}
ofstream is for writing and not reading from file and ios::out is output open mode and not input open mode.
I hope this helps.
Upvotes: 2