Need to work on formatting a bit, there's a reason for the "preview" button.
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;
}
The "initialization of 'a_file' is skipped by 'case' label" error is caused by your code I've highlighted in red above. Depending on what choice the user makes, there may or may not be an instatiation of a particular ofstream object and this is what the compiler is complaining about. To fix that, you should rework things a bit so that the declaration of the a_file and b_file variable have scope within the entire function and not just the block of code they are currently in, the only thing you should be doing in the switch statement is opening the files.
Code:
int main()
{
...
ofstream a_file, b_file;
...
do
{
...
switch ( cho )
{
case 1:
a_file.open(filename.c_str(), ios::app);
break;
case 2:
b_file.open(filename.c_str());
break;
default:
cout << "\nERROR , wrong input \n";
break;
}
} while( ...
Code:
while((cho != 1) || (cho != 2)); //confused here, is it && or || i need?
I think you want "&&" and not "||" there.