I am making a program for an assignment. The purpose is to do a basic fstream (ifstream and ofstream) registry of books like for example:
- Reading the list of books
- Add a book on the list
- Erase all the list
- Close program (Just translating the spanish stuff I have written there)
Anyhow, There's a code here that I have made that is giving me a bit of trouble making it to work. The code below: (I want to show the "No books" text in the console while it is in the .txt file)
Also, it is giving me this error:
Error E0546 transfer of control bypasses initialization of line 56: variable "myfile" (declared at line 59) variable "line" (declared at line 60)
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
string Num = "1234";
ofstream myfiler("LibrosBooksSampl.txt");
int main()
{
string opcion;
char character;
cout << "Que quieres hacer: \n";
cout << "opcion 1: Ver libros guardados.\n";
cout << "opcion 2: Incluir libros nuevos.\n";
cout << "opcion 3: Borrar todo el file.\n";
cout << "opcion 4: Salir del programa.\n";
cout << "Opcion: ";
getline(cin, opcion);
stringstream(opcion) >> character;
while (opcion.find_first_not_of(Num) != string::npos)
{
cout << "Solo del 1 al 4 mano: "; getline(cin, opcion);
}
switch (character)
{
case '1':
ifstream myfile("LibrosBooksSample.txt");
string line = "no books";
if (myfile.is_open())
{
while (myfile >> line)
{
cout << line << endl;
}
myfile.close();
}
else
{
cout << "Unable to open file";
}break;
case '2': cout << "yes";
}
cout << "\n\n";
system("pause");
}