Reputation:
When I try to run the following program, I get the following error from the getline()
call.
The message is:
In function 'int main()':
error: no matching function for call to 'getline(std::ofstream&, std::string&)'
I don't know why I got this - I have the string
library included.
My code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int NumeroLetras;
cout << "Juego de El Ahorcado\n--------------------\n" << endl;
cout << "N\243mero de letras de la palabra: ";
cin >> NumeroLetras;
/* Creamos el fichero con las palabras a adivinar
------------------------------------------------------------------------*/
cout << "Creando fichero con palabras..." << endl;
ofstream fichero("palabras.txt");
fichero << "baloncesto\n";
fichero << "beisbol\n";
fichero << "futbol\n";
fichero << "golf\n";
fichero << "rugby\n";
fichero << "tenis\n";
fichero << "boxeo\n";
fichero << "sumo\n";
fichero << "judo\n";
fichero << "nascar\n";
fichero << "atletismo\n";
fichero << "caminata\n";
fichero << "ciclismo\n";
fichero << "esgrima\n";
fichero << "natacion\n";
fichero << "polo\n";
fichero << "clavados\n";
fichero << "remo\n";
fichero << "vela\n";
fichero << "ajedrez\n";
fichero.close();
cout << "Fichero creado exitosamente..." << endl;
/* Determinamos el tamaño de las palabras
------------------------------------------------------------------------*/
string Palabra;
fichero.open("palabras.txt");
while (! fichero.eof())
{
getline(fichero, Palabra);
cout << Palabra << endl;
}
return 0;
}
Why do I get that error?
Upvotes: 0
Views: 125
Reputation: 5680
You are trying to read from an ofstream
(output file stream)
Create a new ifstream
variable and read from it.
Upvotes: 4