I'm currently working on a program to create special lists of people which should be written into files. I'm still quite at the beginning because i was not able tho compile the code below because the compiler gave out the error message : no matching function for call to 'std::basic_ofstream<char>::open(std::string&)' (line 58)
I thought that filename is already a string. In the tutorial the filename was even used directly. ( But it didn't work in my case as well)
If i replace myfile.open(filename); with myfile.open(filename.c_str()); i get another error from the compiler: crosses initialization of 'std::ofstream myfile'
This can be a little hard to understand when beginning with C++. Within the switch statement, usually the code for each case ends with a break statement, in order to stop execution from 'falling through' to the next case(s). In a somewhat similar way, when a variable is declared within a switch statement, it can be accessed at any point in the following cases. That can cause problems.
The simplest solution is to enclose the code for the case between open and close braces { }. That limits the scope of the variables to just the part where they are used.