Switch case, with ofstream <- noob
yesterday i posted a similar program,, but it was very confusing with many nested if statements, i had to many problems with it
im trying to use a switch statement now..but i get an error -
stuff like this, i get about 9 errors -
Code:
initialization of 'a_file' is skipped by 'case' label
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int cho;
string off = "working";
int toggle = 0;
string str;
string filename;
cout << "whats the file? include extenstion \n";
cin >> filename;
cin.ignore();
do{
cout << "\nwill you be editing or creating new file/starting over? \n 1.Edit \n 2.NEW \n\n";
cin >> cho;
cin.ignore();
switch ( cho )
{
case 1:
ofstream a_file(filename.c_str(), ios::app);
break;
case 2:
ofstream b_file(filename.c_str());
break;
default:
cout << "\nERROR , wrong input \n";
break;
}
}while((cho != 1) || (cho != 2)); //confused here, is it && or || i need?
if(cho == 1)
{
if(!a_file.is_open())
{
cout << "\nFile unopenable";
cin.get();
}
else if(b_file.is_open()){
toggle = 1;
cout << "\nNEW is on";
}
else{
off = "off";
}
}
if(cho == 2)
{
if(!b_file.is_open())
{
cout << "\nFile unopenable";
cin.get();
}
else if(a_file.is_open()){
toggle = 1;
cout << "\nEditing is on";
}
else{
off = "off";
}
}
cout << " \n What do you want to enter in the file?" << endl;
getline (cin,str);
cin.ignore();
if (cho == 2){
b_file<< str;
}
else
{
a_file << "\n" << str;
}
cin.get();
return 0;
}