How to make text disapper in std::cin

closed account (DEhqDjzh)
Hi, I am creating a program where you can chat with a bot(not AI) but I have a problem, the output looks like this:
1
2
3
4
Bot: where are you from ?
America // I want this line to disapper
You: I am from America
 

the code:
1
2
3
4
string a;
std::cout << "where are you from ?" std::endl
std::cin >> a;
std::cout << "I am from " << a << std::endl;
Last edited on
i see what you are trying to do, but i question its sense.

doing it the way that you are will mean that every question the bot can ask has to have a preformatted answer that the user is just adding a word to.

in the example above, your terminal needs to know what the box asked so that it can prefix your answer with "I am from", will you do this for every possible question and answer? I wouldn't.

instead, dont try to interpret the conversation, just relay the text between the user and the bot. be clever with your prompts...
1
2
Bot: where are you from?
You: I am from America

1
2
3
4
std::string a;
std::cout << "Bot:" << "where are you from ?" std::endl;
std::cout << "You:";
std::cin >> a;
"\b" is the backspace character. You can loop and "print" the backspace as many times as your string is long:
1
2
3
4
5
6
string a;
cin >> a; //notice that there will be an endline too, so
for(int i = 0; i < a.length + 1 /*or size dont remember*/; i++)
    {
    cout << "\b";
    }


Also if your ENTIRE program revolves around the terminal with heavy usage of standard input-output everywhere, i suggest you use:
using namespace std;
or just
1
2
3
4
using std::cin;
using std::cout;
using std::endl;
using std::string;

This way you dont have to write std:: every single time

@Jaybob66 your solution would require havy lingustical recognition algorithms if he just wanted to memorize the user's location for future usage.
Last edited on
recognising and deconstructing/reconstructing language is the entire functionality of a chat bot, otherwise it is just a Q & A session, how do you expect it to interpret any questions the user may ask? or tie their responses into its responses to create a conversation? or to formulate responses based on user input. These may well be "heavy linguistic algorithms" in modern commercial chat bots, but that is not the case in most chat bots.

your ENTIRE program revolves around the terminal with heavy usage of standard input-output everywhere

this isnt true, only the actual input and output does that, processing user input and generating responses is where the meat is. there will probably be a single cout line and a single cin line.

see the grand mammy of them all http://en.wikipedia.org/wiki/ELIZA, interestingly this was considered AI at the time :)

I wrote my first version of eliza in 1983 at college and it was one of the best things i did regards learning about string manipulation and building cross referencing datasets.
Last edited on
there are nonstandard tools that can hide input that is typed -- eg typing in a password without displaying it on screen. Ye olde getch() does not show what is typed, getche() does, but that is a little crude. Gotoxy will let you overwrite the offending text (similar to backspace approach above). There are others as well; I have forgotten all the stuff in ansi and conio etc but I know there are additional ways to do this. If you want a pure c++ way I think you can inherit a stream and backfeed it to the keyboard with a different (unseen) buffer to throw away the output so that reading in just does not type in the console anymore, but this is a royal pain and I don't remember exactly how to do it (and it may depend on the OS how you have to do it??). And there are pure hardware solutions as well, you can just read the keyboard without doing cin at all (very OS / system dependent).
Last edited on
Topic archived. No new replies allowed.