Good! I am trying to implement the recommendation that @eferion made in a previous question: Read string and floats of a file to calculate weighted average in C ++
But the IDE throws me several errors. The code is as follows:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(){
// Nombre del programa
cout << "\t\t Promedio Ponderado\n" << endl;
int num_Mat;
string nombre;
// Solicita el numero de materias
cout << "Ingrese el numero de materias\n";
cin >> num_Mat;
//Crea vectores para la variable Nombres
vector <string> vNombres;
// Creación del archivo
ofstream fNombres;
// Apertura y comprobación
if ( !fNombres.open("Nombres.txt") ) {
cout << "Error al intentar abrir el archivo Nombres" << endl;
} else {
// Loop para obtener el nombre de las materias y enviarlas al vector
for (int i = 0; i < num_Mat; i++){
cout << "Ingresa el nombre de la materia " << i+1 << endl; // "+1" porque no existe Materia 0
cin >> vNombres[i] ;
fNombres << vNombres[i] << endl;
}
}
fNombres.close(); // Cierra el archivo 1
system("pause");
return 0;
}
And it throws me the following errors:
error: could not convert 'fName.std :: basic_ofstream < _CharT, _Traits > :: open > (((const char *) "Names.txt"), std :: operator | ((std :: _ Ios_Openmode) 16u, (std :: _ Ios_Openmode) 32u)) 'from' void 'to' bool '|
error: in argument to unary! |
What am I doing wrong?
Also, I saw in another question that to create a vector, first write the type of variable that will go inside it and then name it, instead here, first declares the vector and then choose the type of variable to use . Source: C ++ with vectors
Is it the same in the end?
Thank you very much in advance!