-1

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,
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    You're using the wrong `getline`. [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) only accepts `std::string`. [`std::istream::getline`](https://en.cppreference.com/w/cpp/io/basic_istream/getline) accepts `char` arrays. Prefer `std::getline` and `std::string`s though. Far less messy. – user4581301 Apr 17 '21 at 00:16
  • Side note: `char tempType[15];` defines an automatic variable local to the constructor. It will vanish as soon as the constructor exits, taking what was read into it with it. That's cool if you're just going to convert the token into a number or perform a computation based on it, but a lot of the time you want to read directly into a member variable to keep what you read around after the constructor finishes. – user4581301 Apr 17 '21 at 00:22
  • 1
    I recommend adding the complete text of the error message to the question. For all I know one of my comments has already answered your question and there is no way for me to know. – user4581301 Apr 17 '21 at 00:24
  • i used your implementation of istream::getline and I am now down to one error. It says "error: call to non-static member function without an object argument". – Ethan-Eill Apr 17 '21 at 01:06
  • @Ethan-Eill we would need to see that code and complete error message. But clearly you are actually doing what the error says you are - trying to call a non-static class method without specifying an object to call the method on. – Remy Lebeau Apr 17 '21 at 01:14

1 Answers1

0

The compiler error is referring to the C getline() function in <stdio.h> that is for reading a line as a char* string from a C FILE* stream. You are using a C++ std::ifstream instead, which that version of getline() does not support.

Also, there is no version of the C++ std::getline() function that reads a line from a std::ifstream into a char[] buffer, either.

However, std::ifstream derives from std::istream, which does have a getline() method for reading a line into a char[] buffer. That is what your constructor will need to call, eg:

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
    
    inFile.getline(tempType, 15, ',');
    inFile.getline(tempMake, 20, ',');
    inFile.getline(tempModel, 30, ',');
    inFile.getline(tempPropulsion, 15, ',');
    inFile.getline(tempEngine, 15, ',');
    inFile.getline(tempHp, 15, ',');
    inFile.getline(tempColor, 25, ',');
    inFile.getline(tempLength, 15, ',');
    inFile.getline(tempBase_price, 15, ',');
    inFile.getline(tempTotal_price, 15, ',');
    
    cout << tempType << endl;
}

Though std::getline() would be better, especially if you are going to be passing the strings to std::stoi() or std::stod(), which take std::string inputs, not char[] inputs:

watercraft::watercraft(ifstream &inFile){
    //use getline
    //stoi to convert string to int
    
    std::string tempType;
    std::string tempMake;
    std::string tempModel;
    std::string tempPropulsion;            //int
    std::string tempEngine;
    std::string tempHp;                    //int
    std::string tempColor;
    std::string tempLength;                //int
    std::string tempBase_price;            //double
    std::string tempTotal_price;           //double
    
    std::getline(inFile, tempType, ',');
    std::getline(inFile, tempMake, ',');
    std::getline(inFile, tempModel, ',');
    std::getline(inFile, tempPropulsion, ',');
    std::getline(inFile, tempEngine, ',');
    std::getline(inFile, tempHp, ',');
    std::getline(inFile, tempColor, ',');
    std::getline(inFile, tempLength, ',');
    std::getline(inFile, tempBase_price, ',');
    std::getline(inFile, tempTotal_price, ',');
    
    cout << tempType << endl;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770