简体   繁体   中英

Creating a simple text editor in C++

I'm in my CS162 class and my assignment for this week is to create a very, very simple text editor which prompts the user to input a paragraph, type # when finished, and then the program will make simple edits such as capitalizing any beginning-of-the-sentence words and changing common errors such as "teh" to "the." Now, I always have trouble getting started with these things; I know exactly how I'm going to correct the errors (have the program search for misspellings and replace those words with the correct spelling/using .upper to change to upper case), but I can't get started on simply having the user input a paragraph and end it with #. Would I use a loop that allows the user to continue typing until they type #? what would that look like? Sorry if this seems excessively basic; I just always have trouble getting started with programs, since I am a very early beginner. Thank You.

Use CONIO.H

You can use functions like:

getch() - reads a character from the console without buffer or echo

kbhit() which determines if a keyboard key was pressed.

to get what you're looking for.

Edit: This is from user Falcom Momot, for Linux systems:

#include <unistd.h>
#include <termios.h>
char getch() {
    char buf = 0;
    struct termios old = {0};
    if (tcgetattr(0, &old) < 0)
            perror("tcsetattr()");
    old.c_lflag &= ~ICANON;
    old.c_lflag &= ~ECHO;
    old.c_cc[VMIN] = 1;
    old.c_cc[VTIME] = 0;
    if (tcsetattr(0, TCSANOW, &old) < 0)
            perror("tcsetattr ICANON");
    if (read(0, &buf, 1) < 0)
            perror ("read()");
    old.c_lflag |= ICANON;
    old.c_lflag |= ECHO;
    if (tcsetattr(0, TCSADRAIN, &old) < 0)
            perror ("tcsetattr ~ICANON");
    return (buf);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM