Ok i am writing a program with Arrays and could use a little help. I have to build a two dimensional array with data from a file. The file has date that first is two numbers followed by a list of charterers. So the fist line of data is 2 4ABCDEFGH which is 2 Rows and 4 Columns and array that looks like A B C D
E F G H

I need to have a function to read in the array and then a function to print the array. I am having trouble reading the rows and columns from the file and getting them in the array. Where R is 2 and C is 4 i Tried
infile>>R>>C
Array[R][C]={ }; // It kept saying i had an array of value 0

I am also not sure my function prototype and function header are right and this is really giving me some problems. The only way i can get it to work is by making R and C global and setting them equal to an integer not reading the value from the file. I am also have troubling filling the array with the charterers.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

 
 const int C=4;
 const int R=2;

void showarray(char [][C], int,char);
int _tmain(int argc, _TCHAR* argv[])
{ int rows,cols;
char X;
ifstream infile;
ofstream outfile;

infile.open("Infile.txt");
infile>>rows>>cols;
infile>>X;
char Array[R][C]={ };
showarray(Maze,R,X);


	return 0;
}

void showarray(char Array[][C], int R,char X)
{
	for(int row=0; row<R; row++)
	{   for(int col =0; col<C; col++)
	  {Array[row][col]=X;
             {cout<<Array[row][col];}
		
	  }
	} 
	  cout<<endl;
}

Recommended Answers

All 9 Replies

do you have any code so far?

do you have any code so far?

Yea i just hit post to early and had to go edit and put the rest of the post in.

first to read in a 2d array you need to use t2 for loops. one for the row and one for the column. also is this the file syntax

2 4ABCDEFG
// or is it
2 4 ABCDEFG

this will determine on how you would have to extract the data from the file.

first to read in a 2d array you need to use t2 for loops. one for the row and one for the column. also is this the file syntax

2 4ABCDEFG
// or is it
2 4 ABCDEFG

this will determine on how you would have to extract the data from the file.

the data file looks like number 1 2 4ABCDEFG
could you explain more on reading and printing the array with two functions.

alright to use functions to read in the data and to print out the data you have to declare the array in your main function and then pass that array to the read and print functions. in your read function you will have to use the getline() function to read in the entire line of text and then parse through it to get the data you need. in your print function you just need to parse through the array and display it in the correct format. as i said earlier both of these functions will require 2 for loops do this. the first for loop will control what row you are in and the second for loop will control what column your in.

alright to use functions to read in the data and to print out the data you have to declare the array in your main function and then pass that array to the read and print functions. in your read function you will have to use the getline() function to read in the entire line of text and then parse through it to get the data you need. in your print function you just need to parse through the array and display it in the correct format. as i said earlier both of these functions will require 2 for loops do this. the first for loop will control what row you are in and the second for loop will control what column your in.

I understand that but do you have an example and did you look at my code in the first post. I have not ever used the get line before. Also i wasn't sure how to prototype the functions. I also have trouble reading the R and C integers from the data file in to the Array.

alright just to be sure. you have one function that is supposed to read the first 2 number in the line of the txt file and then create a 2d array based on those values and then you are to populate that array with the rest of the data in that line. then you have to have another function that will print out that data right?

alright just to be sure. you have one function that is supposed to read the first 2 number in the line of the txt file and then create a 2d array based on those values and then you are to populate that array with the rest of the data in that line. then you have to have another function that will print out that data right?

Yes i am stuck now at getting the charters populated into the array. Here is my code. Im using Char X for the chars in the file. The way it is now it only prints the first letter. I know i need to set it up with another for loop and using Getline but im not sure how to do that.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;





void showarray(char [][15], int,int,char);

int _tmain(int argc, _TCHAR* argv[])
{ int R,C;
char X;
  
ifstream infile;
ofstream outfile;
infile.open("Infile.txt");
infile>>R>>C;
infile>>X;
char Array[10][15]={ };
showarray(Maze,R,C,X);


	return 0;
}

void showarray(char Array[][15], int R,int C,char X)
{ for(int row=0;row<R;row++)
     {for(int col=0;col<C;col++)
       {Maze[row][col]=X;
          {cout<<Array[row][col];
		  }
       }
       cout<<endl;
     }
}

well what you have isn't quite fulfilling the assignment. you are supposed to read the file in its on function lets say something like ReadFile(). with ReadFile() you will need to pass in the input stream or you could open the file in the function. then you need to read the first line of the file. i would use getline() for this. a link for getline is here: http://www.cplusplus.com/reference/iostream/istream/getline/ . after that you need to go through that line of text and pull out the row and column information. something like this would do that

char array[30]
int row, column;
getline(infile, array);
row = array[0] - '0';
column = array[3] - '0';

then you have to create your array based on that information and then put the rest of the line of text you got from getline into the appropriate place. i would try working on that and see what you get. it best to break up your coding into small little bits instead of trying to program the whole thing and then try to find out what doesn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.