Cursor Movement
Sep 27, 2011 at 9:55am UTC
hi
Can anybody tell me how to move the cursor in the console window after taking an input from the keyboard.
like
1 2 3 4 5 6 7
string name;
for (int i=0;i<5;i++)
{
cout<<"\t" ;
cin>>name;
cout<<"\t" ;
}
what i mean is i want to take the names side by side
Robert Rakesh Raamu
If i press enter then the cursor automatically moving to the new line , but i need to move it by certain number of positions in the same line.
Thanking you in advance.......
Last edited on Sep 27, 2011 at 9:56am UTC
Sep 27, 2011 at 12:49pm UTC
You can't do that with standard C++.
Sep 27, 2011 at 2:06pm UTC
You could try doing it this way. This compiles and runs under MS Visual C++ 2008 Express Edition. If you're using something else, you may need to make a few changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
// Names.cpp : Defines the entry point for the console application.
//
#include <stdafx.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
void gotoXY(int ,int );
void gotoXY(int ,int ,string);
int _tmain(int argc, _TCHAR* argv[])
{
string name;
gotoXY(6,3,"Please enter three names." );
for (int i=0;i<3;i++)
{
gotoXY(i*10,8);
cin>>name;
}
return 0;
}
void gotoXY(int x, int y)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
}
void gotoXY(int x, int y, string text)
{
CursorPosition.X = x;
CursorPosition.Y = y;
SetConsoleCursorPosition(console,CursorPosition);
cout << text;
}
Sep 28, 2011 at 5:27am UTC
Thanking you Its working exactly.................
Sep 28, 2011 at 4:59pm UTC
@Srija
You're welcome. You may now want to update this thread as 'Solved'.
Topic archived. No new replies allowed.