Hello, I want to write a program that reads a text file, which contains a table of data where the columns are separated by spaces, and store each column in different arrays with different data types.
For example, let's say I have the following data:
1 hello 1.5
2 world 2.5
I want to store the data as:
int numberArray[] = {1,2}
string wordArray[] = {"hello", "world"}
double decArray[] = {1.5, 2.5}
Here is what I have so far:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string str[2];
ifstream myFile("File.txt");
for(int i = 0; i < 3; i++)
{
getline(myFile, str[i]);
}
return 0;
}
This stores the data in rows with only string type:
str[0] = {"1", "hello", "1.5"}
str[1] = {"2", "world", "2.5"}
So I want to:
1- Store columns, not rows
2- Change the values' data types
Things I tried but didn't work:
string numberArray[2], wordArray[2], decArray[3];
for(int i = 0; i < 3; i++)
{
getline(myFile, numberArray[i], ' ');
getline(myFile, wordArray[i], ' ');
getline(myFile, decArray[i], ' ');
}
string str;
string numberArray, wordArray, decArray;
getline(myFile, str);
numberArray = str.substr(0,1);
wordArray = str.substr(2,3);
decArray = str.substr(3,4);
Thank you