Apr 27, 2016 at 3:39am UTC
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 Apr 27, 2016 at 4:00am UTC
Apr 27, 2016 at 8:30am UTC
getline reads into a string but you use char arrays and they are not compatible.
Use cin.getline instead.
Apr 27, 2016 at 8:35pm UTC
can i have an example of how to use that because when i tried it didn't compile?