I'm currently taking a introduction course to C++ and I've been able to do the first 3 assignments without much problems, but this new assignment is kicking my butt. The goal of this program was to make a simple text editing program. I simply open a saved text document and search for what I want to change and then replace it with what I want. The text I want to edit will only change the first or last parts of what I want or the entire thing. After I finish changing everything, I want to be able quit the editing process and save it as a new file or display it on the screen.
This is what I kind of want it to do
Filename:
Check if it opens successfully
Transfer the text into a variable
Edit the text in the variable, then when I'm done save it to a new file.
Somewhere along the line I have to close the file and check if it closes successfully also.
This is the code I have at the moment, but I'm having a lot of troubles with it. I need some help on what I should do and how to change this program :(
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void filestatus (string, string, int, fstream);
string ChangeAll(string, string, string);
string ChangeRight(string, string, string);
string ChangeFirst(string, string, string);
void filestatus (string fina, string erme, int exco, fstream fs)
{
if(fs.fail())
{
cerr << erme << fina << endl;
exit(exco);
}
}
string ChangeAll(string New, string Old, string s)
{
string Left, Right;
int P;
while((P=s.find(Old))!= string::npos)
{
Left = s.substr(0, P);
Right = s.substr(P + Old.size(), s.size());
s = Left + New + Right;
}
return(s);
}
string ChangeRight(string New, string Old, string s)
{
string Left, Right, snew = "";
int P;
while((P=s.rfind(Old))!= string::npos)
{
Left = s.substr(0, P);
Right = s.substr(P + Old.size(), s.size());
snew = Left + New + Right;
}
return(snew);
}
string ChangeFirst(string New, string Old, string s)
{
string Left, Right, snew = "";
int P;
while((P=s.find(Old))!= string::npos);
{
Left = s.substr(0, P);
Right = s.substr(P + Old.size(), s.size());
snew = Left + New + Right;
}
return(snew);
}
int main()
{
fstream fs, ofi;
string fname, line, cont, newfname, inew, iold, choice;
bool screen = false;
bool all = true;
cout << "File name please: ";
getline(cin, fname);
fs.open(fname.c_str(),ios::in);
filestatus (fname, " is unable to open", 1, fs);
while(getline(fs, line))
{
cout << line << endl;
}
fs.close();
filestatus (fname, " is unable to close", 2, fs);
for(;;)
{
cout << "Would you like to edit file?('yes' or 'no') ";
cin >> cont;
cin.ignore(256,'\n');
if(cont == "no")
{
cout << "New output file name: ";
getline(cin,newfname);
if (newfname.empty())screen = true;
if(screen) cout << line << endl;
else ofi << line;
}
else if (cont == "yes")
{
cout << "What would you like to change? ";
cin >> iold;
cout << "What would you like to replace it with? ";
cin >> inew;
cout << "Do you wish the change the first or last ";
cout << "in every line or all?('first' or 'last' or 'all') ";
cin >> choice;
if (choice == "first")
{
ChangeFirst(inew, iold, line);
}
else if (choice == "last")
{
ChangeRight(inew, iold, line);
}
else if (choice == "all")
{
ChangeAll(inew, iold, line);
cout << ChangeAll(inew, iold, line);
}
return(0);
}
}
}