reading file into a structure array

Im trying to read my file into this structure array and I'm getting an error when using getline {no matching function for call to get line}?? the elements include spaces so i have to use get line to read them in ????

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct element {
    char name[22];
    char symbol[4];
    char number[4];
    char meltingPoint[12];
    char boilingPoint[8];
    
};
/******************************prototypes************************************/
void readfile (element elements[], const int SIZE);
void outputArray(element elements[], const int SIZE);

int main(){
    const int SIZE = 118;
    element elements[SIZE];
    outputArray(elements, SIZE);
    
    return 0;
}

/****************************functions*********************************/
    void readfile (element elements[], const int SIZE){
        ifstream infile("elementsAssign.txt");
        if (infile){
            
            for (int i = 0; i < SIZE && !infile.eof(); i++){
                getline(infile, elements[i].name);
                getline(infile, elements[i].symbol);
                getline(infile, elements[i].number);
                getline(infile, elements[i].meltingPoint);
                getline(infile, elements[i].boilingPoint);
            }
            
            infile.close();
        }
        else
            cout << "file not found"<< endl;
        
    }
void outputArray(element elements[], const int SIZE){
    for (int i = 0; i < SIZE; i++){
        cout << "Element name: " << elements[i].name << endl;
        cout << "Symbol: " << elements[i].symbol << endl;
        cout << "Number: " << elements[i].number << endl;
        cout << "Melting Point: " << elements[i].meltingPoint << endl;
        cout << "Boiling Point: " << elements[i].boilingPoint << endl;
    }
}
Last edited on
getline reads into a string but you use char arrays and they are not compatible.
Use cin.getline instead.
can i have an example of how to use that because when i tried it didn't compile?
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void readfile (element elements[], const int SIZE)
{
  ifstream infile ("elementsAssign.txt");
  if (infile)
  {

    for (int i = 0; i < SIZE && !infile.eof (); i++)
    {
      infile.getline (elements[i].name, sizeof (elements[i].name));
      infile.getline (elements[i].symbol, sizeof(elements[i].symbol));
      infile.getline (elements[i].number, sizeof (elements[i].number));
      infile.getline (elements[i].meltingPoint, sizeof (elements[i].meltingPoint));
      infile.getline (elements[i].boilingPoint, sizeof (elements[i].boilingPoint));
    }

    infile.close ();
  }
  else
    cout << "file not found" << endl;

}

Also have a look here:
http://www.cplusplus.com/reference/istream/istream/getline/
Topic archived. No new replies allowed.