I am quite new to C++. I need to make a small application which reads the content of a txt file and then displays the content in the console. I have three points forming a triangle that I will plot later on. I want to do all this operation in a function called read2dFile, so my main is actually empty (except for the call of the function). When I tried this code in a main in another project, everything was working fine. It seems like my function is not properly declared. Here is my code :
**Test.cpp** (FOR THE MAIN)
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "read2dFile.h"
int main()
{
read2dFile();
return 0;
}
read2dfile.h (FOR THE FUNCTION HEADER)
#ifndef READ2DFILE_H_
#define READ2DFILE_H_
#include <iostream>
#include <iomanip>
#include <array>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
using namespace std;
void read2dFile();
#endif
read2dFile.cpp (FOR THE FUNCTION CODE)
#include "read2dFile.h"
int row = 0;
int col = 0;
void read2dFile() {
string line;
int x;
int array[100][100] = { { 0 } };
string filename;
ifstream fileIN;
// Intro
cout
<< "This program reads the number of rows and columns in your data
file."
<< endl;
cout << "It inputs the data file into an array as well." << endl;
cout << "\nPlease enter the data file below and press enter." << endl;
cin >> filename;
fileIN.open(filename);
// Error check
if (fileIN.fail()) {
cerr << "* File you are trying to access cannot be found or opened *";
exit(1);
}
// Reading the data file
cout << "\n" << endl;
while (fileIN.good()) {
while (getline(fileIN, line)) {
istringstream stream(line);
col = 0;
while (stream >> x) {
array[row][col] = x;
col++;
}
row++;
}
}
// Display the data
cout << "# of rows ---> " << row << endl;
cout << "# of columns ---> " << col << endl;
cout << " " << endl;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << left << setw(6) << array[i][j] << " ";
}
cout << endl;
}
}