Reading from file into an array of structures

I am reading from a file into an array of structures but when I try to read into the second and third members (cost and number) of the structure it tells me no matching function for call to 'getline'.

Here is what the File looks like,

Coca-Cola,0.75,20
Root Beer,0.75,20
Sprite,0.75,20
Spring Water,0.80,20
Apple Juice,0.95,20

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
#include <iostream>
#include <string>
#include <fstream>

struct drinkMachineSim
{
    std::string name;
    double cost;
    int number;

};
const int MAX_DRINKS = 5;

int main() {

    drinkMachineSim myMachine[MAX_DRINKS];

    
    return 0;
}
void populateArray(drinkMachineSim myMachine[])
{
    std::ifstream inputFile;
    inputFile.open("DrinkMachineInventory.txt");
    for (int i = 0; i < MAX_DRINKS && !inputFile.eof() ; i++)
    {
        getline(inputFile, myMachine[i].name, ',');
        getline(inputFile, myMachine[i].cost, ',');
        getline(inputFile, myMachine[i].number, '\n');
    }
    inputFile.close();
}
Last edited on
There are basically two getline() functions:
1
2
istream& getline (istream&  is, string& str, char delim);
istream& getline (char* s, streamsize n, char delim );

They read into string and char*. Text.

double is not text. int is not text. They are numbers.

Formatted input operator>> can read numbers.

std::string has helper functions to convert text into number. http://www.cplusplus.com/reference/string/
Topic archived. No new replies allowed.