0

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:

  1. Reading the list of books
  2. Add a book on the list
  3. Erase all the list
  4. 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");
     }
  • Put the content of `case '1':` into `{`....`}` to get rid of the error. For more info: https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement – Killzone Kid Mar 11 '18 at 15:25
  • Possible duplicate of [Why can't variables be declared in a switch statement?](https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement) – zett42 Mar 11 '18 at 15:42
  • If I keep going, it may go broad... I love that it is so specific to one part and done. I replaced it with an if/else so it's safe to say that I am "good"... thanks anyway.. – Koran J Miranda Viera Mar 11 '18 at 18:06

0 Answers0