Loading a 2d array from a file in C++

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2392 Views - Last Post: 11 September 2014 - 05:41 AM Rate Topic: -----

#1 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Loading a 2d array from a file in C++

Posted 10 September 2014 - 07:33 PM

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 the code I have so far:

 #include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream prog;
prog.open("prog1.dat");

    //If file can't be opened, exit
    if (!prog) {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
    }
    
int size;
prog >> size;
int[][] numArray = new int[size][];
	for(int row = 0; row < size; row++) {
		numArray[row] = new int[size][];
		for(int col = 0; col < size; col++) {
			prog >> numArray[row][col];
		}
	}
} 


When I attempt to compile this (I'm working on a Linux system), I'm getting several errors:

prog1.cpp: In function ?int main()?:
prog1.cpp:37: error: expected unqualified-id before ?[? token
prog1.cpp:39: error: ?numArray? was not declared in this scope
prog1.cpp:39: error: expected primary-expression before ?]? token

Like I said, I'm extremely new to this so really have no idea what I'm doing. I'm basically going through different tutorials to learn each individual step, so any help is greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Loading a 2d array from a file in C++

#2 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 07:41 PM

Do you know how many are in the array at the beginning? If not I would suggest you use vector<>.

One suggestion is to use a struct. The way you only need to deal with 1 array. And make function to help.

#include <vector>
struct pair
{
    int  number;
    char character;
};

bool Compare(pair a, pair b )
{
    // returns if differ enough
}

int main
{
   pair list[size];
   // or 
   vector<pair> list;

}




This post has been edited by infernorthor: 10 September 2014 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,018
  • Joined: 25-December 09

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 08:18 PM

You may want study the following Introduction to dynamic two dimensional arrays in C++. But I also recommend you think about using std::vector instead of the arrays, if possible.


Jim
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 08:50 PM

Hi, in C++ you could just create the array in the usual way.

#include <iostream>
#include <fstream>

using namespace std;

int main() 
{
  ifstream prog;
  prog.open("prog1.dat");

  //If file can't be opened, exit
  if (!prog) {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
  }
    
  int size;
  // read size from file
  prog >> size;

  // create array
  int numArray[size][size];

  //...

  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 09:34 PM

View Postinfernorthor, on 10 September 2014 - 07:41 PM, said:

Do you know how many are in the array at the beginning? If not I would suggest you use vector<>.


Everything I'm reading looks like this would be the simplest way, but since part of the directions say to use a 2D array, I guess I better stick to that (this professor's really picky). Technically I know how many will be in the array, but I think he wants us to write it in a way that if he were to add numbers to the prog1.dat file, that our program would still work.
Was This Post Helpful? 0
  • +
  • -

#6 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 09:42 PM

View Post#define, on 10 September 2014 - 08:50 PM, said:

Hi, in C++ you could just create the array in the usual way.


Hi, I went back and created the array as like you wrote, but am still getting these three errors:

error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? c:\users\hitechredneck\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 50 1 ConsoleApplication1

IntelliSense: expression must have a constant value c:\Users\HiTechRedneck\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 39 15 ConsoleApplication1

IntelliSense: expression must have a constant value c:\Users\HiTechRedneck\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 39 21 ConsoleApplication1

View Postjimblumberg, on 10 September 2014 - 08:18 PM, said:

You may want study the following Introduction to dynamic two dimensional arrays in C++. But I also recommend you think about using std::vector instead of the arrays, if possible.


Thanks, I'll definitely be taking a look at that, I need all the help I can get with this class. Like I mentioned in one of my other comments, part of the directions is that we must use a 2d array, so I guess I better stick with that even though a vector sounds like a much better option.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,248
  • Joined: 05-May 12

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 09:47 PM

View PostHiTechRedneck3, on 11 September 2014 - 12:42 AM, said:

View Post#define, on 10 September 2014 - 08:50 PM, said:

Hi, in C++ you could just create the array in the usual way.


Hi, I went back and created the array as like you wrote, but am still getting these three errors:

error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? c:\users\hitechredneck\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\consoleapplication1.cpp 50 1 ConsoleApplication1

IntelliSense: expression must have a constant value c:\Users\HiTechRedneck\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 39 15 ConsoleApplication1

IntelliSense: expression must have a constant value c:\Users\HiTechRedneck\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp 39 21 ConsoleApplication1


The error says it all. You setup your project to use precompiled hearders so the compiler is expecting you to include the precompiled header.

Next time create the project without the precompiled header option, or go modify your current project to not use precompiled headers. (The setting is buried in the Project Settings... Compiler Settings section.)
Was This Post Helpful? 0
  • +
  • -

#8 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 10:03 PM

View PostSkydiver, on 10 September 2014 - 09:47 PM, said:

Next time create the project without the precompiled header option, or go modify your current project to not use precompiled headers. (The setting is buried in the Project Settings... Compiler Settings section.)


Found it, you weren't kidding, it's definitely buried in there pretty deep ha. I've been kind of playing around with the constants for size, but can't get it to work right. When I looked up how to create constants in C++, the example showed something like this:

 const int size = [i]value[/i];



But I couldn't get that to work since the size is found in the file, not a value that I assign to it.

Would I instead need to do something like this?:

 int size;
prog >> size;
const int row = size;
const int col = size;


Any ideas?
Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 10:39 PM

Hi, you can try that but I have a feeling it might not work with your compiler.

You could compromise and make a large array of size 100 or 200 or more, and check that the size you read from the file is smaller, and report an error if it is larger. You don't need too use all of the array.

Later you could look at other options if you wish.
Was This Post Helpful? 0
  • +
  • -

#10 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 10:49 PM

The rules of C along with C++ have changed over the years.
So having variable constants and VLA variable length arrays. Could be a problem.

But, I would guess should work. Simply test this

int size;

std::cin >> size;

const int m = size;
const int n = size;

int array[m][n];



If that compiles , if you fixed the previous linker problems. Then you can do that.
Was This Post Helpful? 0
  • +
  • -

#11 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Loading a 2d array from a file in C++

Posted 10 September 2014 - 11:38 PM

I've finally managed to get it to compile by just giving size a value of 10 which is the first number in the file. However, I'm still not doing something correctly because when the program runs it's displaying a black pop up box for a couple seconds then it disappears. I'm not getting any errors like it crashes or anything, it's just not working correctly.

Here is the code as I have it written now:

 #include <iostream>
#include <fstream>
using namespace std;

int main() {
ifstream prog;
prog.open("prog1.dat");

    //If file can't be opened, exit
    if (!prog) {
        cerr << "File could not be opened" << endl;
        exit(EXIT_FAILURE);
    }
    
	else {
	const int size = 10;
	int i;
	int j;
	int numArray[size][size]; 
		for(i = 0; i < size; i++) {
			for(j = 0; j < size; j++) {
				prog >> numArray[i][j];
			}
		}
		return 0; 
		cout << numArray[i][j];
	}
}		


Any thoughts?
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Loading a 2d array from a file in C++

Posted 11 September 2014 - 12:05 AM

You are finishing the program with return before printing anything.

#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
  ifstream prog;
  prog.open("prog1.dat");

  //If file can't be opened, exit
  if (!prog) {
    cerr << "File could not be opened" << endl;
    exit(EXIT_FAILURE);
  }
  else {
    const int size = 10;
    int i;
    int j;
    int numArray[size][size]; 

    // no need for indentation here  
    for(i = 0; i < size; i++) {
      for(j = 0; j < size; j++) {
        prog >> numArray[i][j];
      }
    }

    // return 0 will finish program before any output
    // return 0;

    // you need to print each element of the array
    // i and j are now out of bounds of the array and are incorrect indexes
    // cout << numArray[i][j];
  }

  // may need to hold execution window open
  // see C++ FAQs top of forum
  // cin.get();

  // return will do at end
  return 0;
}


Was This Post Helpful? 0
  • +
  • -

#13 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Loading a 2d array from a file in C++

Posted 11 September 2014 - 12:15 AM

I moved the return statement to the end, but now that I've fixed that and a couple other problems, the program crashes and I get an error at my
cout << numArray[i][j] 
portion of the code that says "First-chance exception at 0x0085912E in Program1.exe: 0xC0000005: Access violation reading location 0xFDFDFF8D."
Was This Post Helpful? 0
  • +
  • -

#14 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Loading a 2d array from a file in C++

Posted 11 September 2014 - 12:28 AM

Are i and j out of bounds? Otherwise the array wasn't created.
Was This Post Helpful? 0
  • +
  • -

#15 HiTechRedneck3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 169
  • Joined: 28-February 14

Re: Loading a 2d array from a file in C++

Posted 11 September 2014 - 12:42 AM

View Postinfernorthor, on 11 September 2014 - 12:28 AM, said:

Are i and j out of bounds?


I think so, j shows the correct value of 10, but here is what it shows as the value of i:

0xfdfdfdfd

So I have no idea what's going on with that.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2