Error- no matching function for call to 'getline (std :: ifstream &, int &, std :: string &)' (dev c ++)

0

I'm with the question of making a phone book. I miss the error that I put in the title, in the line "getline (data, number, name);" (by the way, it is almost at the end indicated with a comment) and I do not know what is due.

/*programa para crear un archivo de texto, leer y agregar directorios telefonicos*/
  #include<stdio.h> //para el printf y el scanf
  #include<fstream> //para trabajar con los archivos
  #include<iostream>
  using namespace std;
  int opcion; //la variable para las opciones del switch
  string nombre; //la variable para guardar el nombre del contacto
  int numero; //la variable para guardar el numero de telefono

  int main(){ //funcion principal
  printf("Directorios telefonicos\n\n Opciones:\n 1-Crear archivo de 
  texto\n2-Agregar contactos\n3-Leer datos");
  //la linea anterior es como luciria el menu
  printf("\nIngresa una opcion:");
  cin>>opcion;
  system("cls");
  //limpiado luego de ingresar la opcion
  //empieza el switch
  switch(opcion){
    case 1:
        void crear();
        break;
    case 2:
        void agregar();
        break; 
    case 3:
        void leer();
        break;
    default:
        printf("Solo se admiten numeros del 1 al 3");   
               }//fin switch 
cin.get();
cin.get();
}
//fin funcion principal main


void crear(){ //funcion para crear el archivo de texto
    ofstream dato;
    string nombredArchivo;
    printf("Ingrese el nombre para el archivo:");
    getline(cin,nombredArchivo);
    dato.open(nombredArchivo.c_str(), ios::out);
    printf("El archivo se ha creado correctamente");
    dato.close();
              }

void agregar(){ //funcion para agregar contactos en el mismo documento
    string nombredArchivo;
    ofstream dato(nombredArchivo.c_str(), ios::app);
    printf("Agregar datos\nNombre:");
    getline(cin,nombre);
    fflush(stdin);
    printf("\nNumero de telefono local:");
    scanf("%i", &numero);
    fflush(stdin);
    dato<<"\n"<<nombre;
    dato<<"\n"<<numero;
    dato.close();
}

void leer(){ //funcion para leer el documento
    string nombredArchivo;
    ifstream dato;
    dato.open(nombredArchivo.c_str(),ios::in);
    if(dato.fail()){
        printf("No se pudo abrir el archivo");
        exit(1);        
    }
    while(!dato.eof()){
        getline(dato,numero, nombre);//aca da el error
        cout<<nombre<<numero<<endl;
    }
    dato.close();
}
    
asked by Mist Chery 17.06.2018 в 22:13
source

1 answer

2
  

I miss the error no matching function for call to 'getline(std::ifstream&, int&, std::string&)' and I do not know what is due.

It is mainly because you have not read the manual or have not taken a look at the on-screen help that DevC ++ offers you.

The std::getline function has four overloads and none of them receives as parameters a std::ifstream together with int and% std::string . The std::getline function, as its name indicates, is used to obtain a line of text from a data stream ( stream ); It seems that you are using it to obtain two separate data.

As far as I can understand in your agregar function, each entry in your phone book is composed of pairs lines, the first being the contact's name and the second the phone number, so you should get the data like this:

void leer() {
    string nombredArchivo;
    ifstream dato;
    dato.open(nombredArchivo.c_str(),ios::in);
    if(dato.fail()){
        printf("No se pudo abrir el archivo");
        exit(1);        
    }
    while(!dato.eof()){
        dato >> nombre;
        dato >> numero;
        cout<<nombre<<numero<<endl;
    }
    dato.close();
}

As simple as that.

Other things to consider.

I advise you to take a look at your previous question in which I have pointed out several things that you should improve from your code :

  • Do not use global variables; in C ++ it is advised that the variables have the smallest possible scope.
  • Do not use the using namespace clause in global scope.
  • You use the header <string> without including it, this header is included through other standard headers but you should not trust that it will stay that way, so it is necessary to include it if you use it.
  • The ofstream can be built by passing the name of the file to be treated, it is not necessary to call open .
  • The main function should return a value .
  • Do not use <stdio.h> , and if you have to use it (which you should not use in this code) use its C ++ version: <cstdio> (read this thread to find out why).
answered by 17.06.2018 в 23:40