Console - Edit answer?

I am starter in C++ and I made a small program.

When I write Edit and press Enter, it asks me to enter the file name to edit (this works). After I write the file name, it prints the text that the file contains (this works too). For example, if there is a text inside a text file (which is, for example, "ABC"), it prints "ABC" in console, but I cannot edit this "ABC" text. I can't press Backspace to edit the text, or any letter. I can only add new letters right next to the text.

Any suggestions? Code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
string button;

int main()
{
	bool exit = true;
	while (exit)
	{
		cout << "\nYour command: "; getline(cin, button);
		if (button == "edit" || button == "Edit" || button == "EDIT")
		{
			string filename;
			string textToSave;
			cout << "Please enter a file you want to edit: ";
			getline(cin, filename);
			ifstream inFile;
			if (ifstream(filename.c_str()))
			{
				fstream file;
				cout << "\nEnter the text you want to edit: ";

				char ch;
				fstream fin(filename.c_str(), std::fstream::in);
				while (fin >> noskipws >> ch)
				{
					cout << ch;
				}
				getline(cin, textToSave);
				ofstream saveFile((filename.c_str()));
				saveFile << textToSave;
				cout << "\nText edited! Press 'Enter' to finish!\n" << endl;
				cin.get();
			}
			else
			{
				cout << "\nExact file does not exist." << endl;
				cin.get();
			}
		}
	}
}
You will not be able to edit the output text using just standard C++. You might want to use a third-party library such as ncurses or the WinAPI.
Topic archived. No new replies allowed.