1

In C++, when writing to and taking information from the console using cout / cin, is it possible to do something like

ENTER YOUR DATA HERE --> __ <-- ENTER YOUR DATA HERE

With the user input cursor where the underscores are located, and output located on either side of the cursor, and then the user's typed input appearing between those two bits of output before being returned to cin?

If so how would I got about doing that?

I hope that between my title and explanation here that it's clear what I'm asking, if not I can try to explain further.

Ideally I'd like to do this using iostream / cin & cout, because those are what I've used in the past. If the solution is to use... printf or similar I'll do that but may need a bit of additional explanation since I'm not really experienced in using that for output.

NOTE: I tried to find an answer for this problem and I can't say for sure that there wasn't one, it was mostly just a matter of finding a huge amount of other input/output-related questions.

EDIT: This is using the DOS shell on Windows 7, compiling from the Windows Visual Studio 2012 command-line compiler.

  • What's your platform? – Matt Eckert Mar 31 '14 at 18:00
  • I'm writing this to run in the DOS shell on Windows 7, compiling from the Windows Visual Studio 2012 command-line compiler. (Thanks for looking at this, please let me know any other information is required) – stellars jay Mar 31 '14 at 18:02
  • I know this may sound picky, but that shell is *not* DOS. That is a 32-bit (or 64-bit) console. Since it is a console, you can look at my answer concerning the Console API. `DOS` is that OS that existed (and probably does exist somewhere) in the early 80's and fell off the map after Windows NT was released (it was still around during the Win 95 days). – PaulMcKenzie Mar 31 '14 at 18:05

2 Answers2

3

If Windows, use the Console API. http://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx

If Linux, use the curses library: http://en.wikipedia.org/wiki/Ncurses

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

If you are using windows you have the option to use the conio.h.

#include<iostream.h>
#include<conio.h>

int main()
{
    clrscr();
    gotoxy(5,3);
    cout<<"HELLO WORLD";
    gotoxy(60,10);
    cout<<"HELLO WORLD";
    gotoxy(35,20);
    cout<<"HELLO WORLD";
    getch();
    return 0;
}

If you wish to use ANSI libraries only, you can try to take a look at this discussion: http://www.cplusplus.com/forum/lounge/78225/

In both cases all you have to do is print the first text, then use gotoxy, then you print seconde text, gotoxy again where you want the input to happens, and read the user input.

  • I don't know what compiler that is, but there is no or in the Visual Studio compilers. – PaulMcKenzie Mar 31 '14 at 18:13
  • in most recent compilers iostream does not takes .h, try using only #include conio.h is a DOS/Windows default library. Which compiler are you using. You can always try to use ANSI solution, as the link at the answer. – Fabio A. Mazzarino Mar 31 '14 at 18:40
  • There is no in Visual Studio. The libraries you're talking about were used by the old compilers (Turbo C is one) that worked with the old MSDOS operating system. Unless you are using an OS that disappeared almost 20 years, the consoles for the current, popular OSes are controlled by either the ConsoleAPI (Windows) or ncurses (Linux/Unix). – PaulMcKenzie Mar 31 '14 at 18:47
  • Aging sign: Trying to use deprecated DOS libraries. – Fabio A. Mazzarino Mar 31 '14 at 19:33