How to Read in Float, String, Float from file?

Hello!

I am wondering how I would go about reading in values from a txt file, a float, then a string, then another float for each line. Delimited by commas.

as in:

15.1,Chico,17.4
34.4,Seattle,30.5
22.9,Portland,26.1
12.3,New York,9.4
.5,Death Valley,1.1
3,Las Vegas,13.8
60,Alajuela,54.3
17.3,San Francisco,20.3
29.5,Dublin,30.8

for the first line, 15.1 and 17.4 would be read in as two separate floats, and Chico as a string. Then the same thing for the next line.

I need then group these together into a 'City' class pointer, and into a vector, but I already know how to do that. I am just having trouble allocating the floats and string per line properly.

Here is what I have so far, although I know getline does not work for floats:

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
    ifstream cityRain;
    cityRain.open("inputfile.txt");
    
    if(!cityRain.good())
    {
        cout << "Woops! Could not open file." << endl;
        system("pause");
        return (1);
    }
    
    vector<City*> citVec;    
    float rOne;
    
//vvvvv Here is where the trouble starts vvvvv
    while (!getline(cityRain,rOne,',').eof())
    {
        string cName;
        float rTwo;
        getline(cityRain,cName,',');
        getline(cityRain,rTwo,',');
        City* cp;
        cp=new City(cName,rOne,rTwo);
        citVec.push_back(cp);
    }
    
    cityRain.close();
Topic archived. No new replies allowed.