I am writing a program right now and I am currently trying to read an input from a file and put the members into variables. It is giving me an error when I am trying to use getline. Here is the line from the text file I am trying to read from.
pontoon,Crest,Carribean RS 230 SLC,1,Suzuki,115,Blue,26,134595.00,135945.00
Here is my code for the constructor:
watercraft::watercraft(ifstream &inFile){
//use getline
//stoi to convert string to int
char tempType[15];
char tempMake[20];
char tempModel[30];
char tempPropulsion[15]; //int
char tempEngine[15];
char tempHp[15]; //int
char tempColor[25];
char tempLength[15]; //int
char tempBase_price[15]; //double
char tempTotal_price[15]; //double
getline(inFile, tempType, ',');
getline(inFile, tempMake, ',');
getline(inFile, tempModel, ',');
getline(inFile, tempPropulsion, ',');
getline(inFile, tempEngine, ',');
getline(inFile, tempHp, ',');
getline(inFile, tempColor, ',');
getline(inFile, tempLength, ',');
getline(inFile, tempBase_price, ',');
getline(inFile, tempTotal_price, ',');
cout << tempType << endl;
}
Here is my mainDriver
code:
int main(int argc, const char * argv[]){
int choice;
int numFor1 = 0;
ifstream inFile("watercraft.txt");
if(!inFile){
cout << "Something wrong with input file" << endl;
exit(0);
}
do{
choice = printMenu(); //call print menu and assign user input to choice
switch(choice){ //switch statement to do as the user pleases
case 1:
if(numFor1 == 0){ //if file hasnt been initialized
watercraft test(inFile);
numFor1++;
}else{
printf("You have already Initialized list\n"); //if file has been initialized
}
break;
}
}while(choice != 12); //do while user does not want to exit the program
return 0;
}
I am getting a lot of errors. Here is just one block of error message that seems to be repeating for each iteration of getline
.
watercraft.cpp:32:5: error: no matching function for call to 'getline'
getline(inFile, tempType, ',');
^~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/stdio.h:355:9: note:
candidate function not viable: no known conversion from
'std::__1::ifstream' (aka 'basic_ifstream<char>') to 'char **' for 1st
argument
ssize_t getline(char ** __restrict __linep, size_t * __restrict __lineca...
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/istream:1507:1: note:
candidate template ignored: could not match
'basic_string<type-parameter-0-0, type-parameter-0-1, type-parameter-0-2>'
against 'char [15]'
getline(basic_istream<_CharT, _Traits>& __is,