0

I'm new to C++ (used to work with Java). So I'm trying to code my first program.

I'd like after executing getline() to move the cursor at the end of the line. Unfortunately it is at the beginning. Is there a way to do so ? Thanks. Below my code.

Sorry for not being clear. Let me complete my question. For example

cout >> "Hello Gerard how are you ?";

After this instruction the cursor is positioned before "Hello". Since the next instruction is cin I'd like to have the cursor positioned after "you ?" in order to type immediately the response.

But I'm not sure whether it is possible. Thanks a lot.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string question1 = "Quel est ton nom ? ";
  string question2 = "Où vis-tu ? ";
  string answer1, answer2;
  cout << question1;
  getline(cin,answer1);
  cout << question2;
  getline(cin,answer2);
  cout << "Bien le bonjour, " << answer1;
  cout << " de " << answer2 << "!\n";
  return 0;
}
  • 3
    *to move the cursor at the end of the line* - which line and beginning of what? – Rohan Bari Jul 16 '20 at 16:34
  • 2
    Please clarify what you want to change by showing expected and actual output. You want something like `Quel est ton nom ? answer1 Où vis-tu ? answer2` without any line breaks in between? – Lukas-T Jul 16 '20 at 16:39
  • 3
    Just as a side note: Since you are new to C++, you may want to read this: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Andreas Wenzel Jul 16 '20 at 16:49
  • 2
    Maybe you *really* want to use a library like [ncurses](https://en.m.wikipedia.org/wiki/Ncurses)..? – Jesper Juhl Jul 16 '20 at 16:50
  • The ISO C++ standard itself does not allow you to move the cursor on the screen. However, most implementations provide such features as extensions. These extensions are often not compatible. For example, on Microsoft Windows, you can use the function [`SetConsoleCursorPosition`](https://learn.microsoft.com/en-us/windows/console/setconsolecursorposition). This will not work on most other operating systems, though. Therefore, you will have to specify the operating system if you want further help. – Andreas Wenzel Jul 16 '20 at 17:01
  • Cursor positioning has always been dependent on the terminal. Some terminals support cursor positioning, others don't. Some terminals may obey the ANSI commands, some may have their own command sequences for terminal positioning. – Thomas Matthews Jul 16 '20 at 17:37

0 Answers0