-2

I need help with this problem. Is it possible to print the text before cin. For example:

#include<iostream>
#include<conio.h>
void main ()
{
char name[20];
cout<<"====================================\n";
cout<<"Enter your name: ";
cin>>name;
cout<<"\n====================================";
getch();
}

So i want the third cout to be displayed before entering the name(cin), so that the second =-line is visible while entering the name in the line above.

To visualize it, I am trying to create a screen matching the below:

====================================
Enter your name: <CURSOR>
====================================
Brian Nickel
  • 26,890
  • 5
  • 80
  • 110
Chinmay Dabke
  • 5,070
  • 11
  • 40
  • 63
  • I think remove `\n` in `cout<<"====================================\n";` – Grijesh Chauhan Jul 29 '13 at 14:29
  • 5
    `main` should be declared as `int main` – Lstor Jul 29 '13 at 14:30
  • 2
    Invert the order of the cin and cout lines? – Borgleader Jul 29 '13 at 14:31
  • 4
    Do you want to have the second =-line to be visible on the screen below the cursor? This is not possible with the cin/cout-method that represents a abstracted input and output stream. If you want to address a visual console only, you may use the Win32ConsoleAPI or libncurses. – urzeit Jul 29 '13 at 14:31
  • Lines get executed sequentially. Put the third `cout` line before the `cin` line and you will get what you want, unless I misunderstand your question. Also, if you're learning to program, you may want to use a different compiler, something standard and modern. You seem to be using some old DOS compiler instead (Turbo perhaps?) – DUman Jul 29 '13 at 14:31
  • @urzeit Can you please explain? Thanks – Chinmay Dabke Jul 29 '13 at 14:36

2 Answers2

3

If I read you correctly, you want the cursor to magically jump back and be next to the "name: " even after you've printed more output.

This is not available in iostream-style code. You have to use a special library for that...something that allows for cursor control, e.g. a curses-style functionality. There is a Windows Console API if your program is windows only.

These libraries often give you other extensions to textual output, like setting the colors. But they can make your program less portable. Usually best to avoid them, in my opinion.

However, if you want to ignore that advice and try it... here's some basics of the windows-only version:

Setting stdout/stderr text color in Windows

Setting the Cursor Position in a Win32 Console Application

...and some information about tutorials on the curses route, for platform independence:

Console interface tutorials and tips (pdcurses)

Community
  • 1
  • 1
  • I would definitely like to try that. Can you please post the links and explain how is it exactly done? Thanks! – Chinmay Dabke Jul 29 '13 at 14:39
  • @ChinmayDabke Added a couple of links, but "windows console" and "curses" will be good search keywords here on StackOverflow and elsewhere. – HostileFork says dont trust SE Jul 29 '13 at 14:47
  • @ChinmayDabke I overlooked the `` header in your source, which actually may have a [`gotoxy() function`](http://www.c4learn.com/gotoxy-function-conioh-header-file-move.html). But that file is only included with REALLY old compilers, and you should really avoid using it (and probably get a new compiler while you're at it). – HostileFork says dont trust SE Jul 29 '13 at 15:40
1

The stdio input-/output method works on streams. Streams are some kind of output or input medium and may be a file, a printer, the line console, a network stream, a pipe or some other input device. This abstraction does not allow to change something that has already been written. Because of this, it is not possible to print something below something else and then jump back to an earlier line (a printer is not able to do so, for example).

There are some libraries, that allow to control the output to some special output devices. In windows, the Win32ConsoleAPI represents a windows console window that is - of course - able to do the job and thus gives you methods to reposition the output cursor.

A more platform independent solution for the same problems is a library called libncurses and is available for other special terminal types (Linux console for example) too.

Tutorial for libncurses-beginners: http://www.writeka.com/ed/ncurses_library.html

Windows Console API-Reference: http://msdn2.microsoft.com/en-us/library/ms682087.aspx

urzeit
  • 2,863
  • 20
  • 36