Home > Back-end >  String library isnt importing getline function
String library isnt importing getline function

Time:09-14

Im using string library but I can't seem to use the get line function used both string and string.h but its still not working I've added the code below and it basically is just to use input numbers in a text file and then using them in a sorting mehthod

the problem is that im only getting the error mentioned below and can't seem to wrap my head around the solution to it


,,assignmentDTS.cpp:34:17: error: no matching function for call to 'getline'
                getline(inputFile,tempnumstring);
#include<iostream>
#include<fstream>
#include<string.h>
#include<time.h>
using namespace std;
class sorting {  
    public:
    void bubblesort(int arraySize){
        int num;
        int *arr=new int[arraySize];
        ofstream inputFile("data.txt");
        if(inputFile.is_open()){
            for (int i = 0; i <arraySize; i  ){
                string tempnumstring;
                std::getline(inputFile,tempnumstring);
                num=stoi(tempnumstring);
                arr[i]=num;
            }

CodePudding user response:

std::getline is declared in #include <string> per the C Library Standard.

The #include <string.h> statement includes the C Library Standard string library which has no getline function. This header contains functions like memcpy and strcpy.

The C recommended way to reference the C "string.h" header is #include <cstring>.

CodePudding user response:

And there we go: ofstream inputFile("data.txt"); needs to be ifstream inputFile("data.txt"); The fstream starts with i for input stream, rather than o for output stream.

Answered on chat

  • Related