I've been trying to import text from a .txt
file into a 2D array of string, but it doesn't seem to be working. Each row in the .txt
file has three values/elements separated that I need to copy.
This is the code:
// i am only allowed to use these libraries.
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main()
{
const int rows = 38;
const int columns = 3;
string companies[rows][columns];
// Inputting file contents
ifstream file;
file.open("companies.txt");
while(!file.eof())
{
for(int i = 0; i < 38; i++)
{
for(int j = 0; j < 3; j++)
{
getline(file, companies[i][j], ',');
}
}
}
file.close();
cout << endl << endl;
// displaying file contents using for loop
for(int i = 0; i < 38; i++)
{
for(int j = 0; j < 3; j++)
{
cout << companies[i][j] << endl << endl;
}
}
cout << endl << endl;
return 0;
}
This is the data that I want to import :
Symbol,Company Name,Stock Price
ATRL,Attock Refinery Ltd.,171.54
AVN,Avanceon Ltd. Consolidated,78.1
BAHL,Bank AL-Habib Ltd.,54.97
CHCC,Cherat Cement Company Ltd.,126.26