get data like (new York,) into an array

"NOTE:by the way I know I have to have a way to check for erors if inFile doesn't open properly. I know how to do that. So, don't worry about explaining that :)." I will assign the arrays after I am able to have my string city, double lowTemp, and double highTemp the way I want them

okay, I ve been searching this forum for an answer, and I have found quite similar threads here, but I haven't been able to resolve my issue. Well my main concern is the following:



I have to load this temp.txt into an array, but the items in the text file are delimiter by commas, (I know that I can use getline(inFile, loadedFile, ',') but that only works with strings right? Another way will be to store the loaded file without commas into an istringstream variable, like the one I have on my code. The issue I am facing doing it like that is that New York has a space between, and I need that word in a single array.If I do inFile >> city, it will store only new. So, how could I store (new york) into an array of strings and lowTemp and highTemp on an array of doubles?

My code below outputs everything the way I wanted, because I am using string variables. If i wanted to assign that to my array of strings, city will work, but lowTemp and highTemp will throw an error.

I know my explanation isn't that clear but I just need a suggestion.
Trust me I have been working on this since Friday. and I haven't found a logic on this.So, don't shoot me or call me lazy!!!

Thank you community

temp.txt
new york, 30, 80 <-- New York into Array of Strings, and 30 and 80 into an array of doubles
St. hellens, -20, 50
toronto, 38, 100



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;

int main (){
string loadedFile;

ifstream inFile;
string filePath;
int index =0;
cout << "Enter the file's path: ";
getline(cin, filePath);
fileIn.open(filePath.c_str(), ios::in);

while (inFile) {
++index;
   getline(inFile, loadedFile); 
   istringstream is( loadedFile );
string city;
string lowtemp;
string highTemp1;
getline(is,city , ',');
getline(is, lowTemp, ',');
getline(is, highTemp);

cout << city ;
cout << lowTemp;
cout << highTemp << endl;
}
fileIn.close();
return 0;
}


1
2
3
4
5
my output <-- just the way I wanted but before assigning it to an array.
new boston 60.00 100.00 
hillsboro 30.00 75.00
washington 50.00 100.00
new york 10.00 60.00

The string value can be converted to a numeric value using the atof() function from the <cstdlib> header.
1
2
        double lowT  = atof(lowTemp.c_str());
        double highT = atof(highTemp.c_str());

It's also worth pointing out that the while loop should be changed from this:
1
2
3
4
5
6
7
    string line;

    while (inFile)
    {
        getline(inFile, line);
        // now process the contents of the line
    }
to this:
1
2
3
4
5
6
    string line;

    while (getline(inFile, line))
    {
        // now process the contents of the line
    }
The first version can give incorrect processing when there are no more lines to be read from the file.
Thank you for your suggestion. I will implement it and report back :-)
Topic archived. No new replies allowed.