-2

I am trying to enter values on the same line WITH pressing enter for example:

1 [enter],2 [enter],3[enter]

The output should look like this

Please enter values : 1, 2, 3

Thank you!


The only answer I can find is

cin >> value1 >> value2 >> value3

but I specifically need the comma in my output

Thank you all in advance

asendjasni
  • 963
  • 1
  • 16
  • 36
Anrich
  • 25
  • 4
  • Welcome to Stack Overflow! We're willing to help you. Please take the time to take the [tour](https://stackoverflow.com/tour) and read up on [How to Ask](https://stackoverflow.com/help/how-to-ask), then [edit](https://stackoverflow.com/posts/54987768/edit) the question accordingly. Please create [a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), and give sample inputs, outputs and the error messages you get, if any. This will help us to determine what is going on and improve your chances to get an answer. – asendjasni Mar 04 '19 at 16:46
  • `char comma1, comma2; cin >> value1 >> comma1 >> value2 >> comma2 >> value3;` – Eljay Mar 04 '19 at 16:48
  • 1
    What you are trying to do is read the comma or just show the comma? – Esdras Xavier Mar 04 '19 at 16:52

2 Answers2

0

I have written a program that might do what you want:

#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
    int value1;
    int value2;

    cin >> value1;
    cout << "\x1b[1A" << value1 << ", ";
    cin >> value2;

    cout << endl;

    cout << value1 << endl;
    cout << value2 << endl;
}

What it does is that it jumps back to the last line, reprints the old input and an additional comma, then prompts for new input.

The problem with this is that this uses the ANSI.SYS standard for the line jump sequence \x1b[1A, which means that this will not necessarily work anywhere. I tried to use it in cpp.sh, there it doesn't work (http://cpp.sh/63ggd), but it works when I compile it under the linux I'm currently using.

If you trust enough in it, go for it, and maybe extend for it for windows using winapi, see How To Use SetConsoleCursorPosition Func.

(The basic underlying problem is that the basic console sequences come from a time when the output of a computer was printed out physically, so jumping back did not make sense back then, although you can indeed jump at the start of the current line. The console sequences were never officially extended since then, there are only additional standards to do tricks like this.)

Aziuth
  • 3,652
  • 3
  • 18
  • 36
-1

if you want to use strings you can use " string " for cin so in your case ",".

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
McMic
  • 11
  • 3