Trying to add 'backspace' Functionality to my Text Editor

I have a text editor that uses the 'getch' function. I did this simply because 'std::cin' would not allow me to add spaces. I do have one problem now; although the loop is working perfectly, the 'backspace' button now prints a character to the console instead of erasing it! I was just going to find the ASCII code for the 'backspace' button, but I thought it would be much more useful to add functionality for the button instead of limiting its use. Here is the current program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    std::string filename;
    std::string input;
    char before;
    std::cout << "Input the name of the file you want to create. ";
    std::cin >> filename;
    std::ofstream outfile(filename.c_str());
    std::cout << "Input the data that you want to insert into this file." << std::endl;
    while(before != '\r'){
                 before = getch();
                 std::cout << before;
                 input = input + before;
    }
    outfile << input;
    outfile.close();
    std::cout << std::endl << "Press the 'Enter' key to close the program.";
    std::cin.ignore();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    return 0;
}
Last edited on
'\b' is backspace.

You might want to consider using NCurses / PDCurses if you plan to do some text editing.

You might also want to consider how you want to store your data. I recommend you use a std::deque <std::string> for the lines of data.

Hope this helps.
OP also wants to use Alt+Key combinations.

(Sorry to respond so late.)

You cannot get them using the standard input streams. You need to use specialized input methods.
I recommend you switch to Curses. It really isn't too steep a learning curve and you will have a lot more fun with it.

Getting started with PDCurses links
http://www.cplusplus.com/forum/windows/15935/#msg79025

Good luck!
Topic archived. No new replies allowed.