I don't know how to retrieve files for 2D arrays. Help is really appreciated.
Write a program that creates a two-dimensional array, reads data from file numbers.txt, assigns the values to the array items, and process them. The program should have the following functions: I just need help writing a code that retrieves the numbers and puts them into an array and processes them.
I just need the code, not inputting this info into the code but retrieving them.
#include<fstream>
#include<iostream>
#include<string>
#include<sstream>
usingnamespace std;
ifstream fin("input.txt");
int arr[100][100];
void getInput(){
string currLineNum=""; //string to store the numbers
int row = 0;
while (getline(fin, currLineNum)){ //getline from ifstream
istringstream getNums(currLineNum); //make an istringstream so you can read from string
int column = 0;
int numInput;
while (getNums>>numInput){ //read input
arr[row][column] = numInput;
column++; //counter for column
}
row++; //counter for row
currLineNum = ""; //reinitialize currLineNum
}
}
int main(){
getInput();
for (int i=0; i<12; i++){
for (int j=0; j<4; j++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
if there are any empty lines between each row, some rows would be missed. If that is so, just put anoter getline() at the end of the first while to "clean" the rubbish.