0

I was wondering if it is possible to output something on the same line where the user gave its input

int money;
cout << "How much money do you have?" << endl;
cin >> money;
//I want this to appear next to the user input
cout << " $";

I hope that you know what i mean and that you can help :)

Edit: I was just wandering if this is possible but as it seems it's not. Not with the standard c++ at least but thanks to everyone who tried to help anyway

frankakn_7
  • 31
  • 1
  • 6

4 Answers4

1

The problem is, that when you read the user's input, the terminal interface reads all the way to when the user press the Enter key, and a newline is printed, i.e. the cursor moves to the next terminal line. And then whatever you print appears on that next line.

You can't really avoid this in purely platform-independent C++, since it requires manipulating the file descriptor "under the hood" of the input stream:

  • Making it not read all the way to the next newline, or
  • Making it not echo all of the characters it reads

(or both). This can be done in a standard way on systems conforming to the POSIX standard(s) - but it's complicated. You can read a bit more about it in this SO answer; but the bottom line is that you should use a library to do this. One of:

should work for you.

PS: Sometimes your input is not a terminal but a file and you might also need to check whether that is the case.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    For Windows, one can use the IC library, developed in a german C++ forum (https://www.c-plusplus.net/code/ic/), as the conio.h support gradually deteriorated over the years. Despite being in German, the interface should be pretty self-explanatory. – Jodocus Sep 20 '17 at 17:11
1

If you want the $ to be printed just beside the input money value, one way can be to move one line up and remove the user input line after taking the input and printing money and $ on the very same line.

i.e can be done as:

 int money;
 std::cout << "How much money do you have?" << std::endl; 
 std::cin >> money;
 std::cout << "\033[F";
 std::cout << money << " $";`

Here's the reference to answer by Philipp Claßen

Seems to serve the purpose :)

Ankush
  • 573
  • 7
  • 11
0

I think if I am reading this correctly, you just need to put the $ before your cin statement.

#include <iostream>

int main()
{
    int money;
    std::cout << "How much money do you have?" << std::endl;
    std::cout << "$";
    std::cin >> money;

    return 0;

}

It will then show on the console as
How much money do you have?
$(insert input here)

Sailanarmo
  • 1,139
  • 15
  • 41
-1

You can do damn near anything with judicious use of the comma operator.

int money;
std::cout << "How much money do you have?" << std::endl;
std::cin >> money, std::cout << money << " $";

Will perform your input and output on one line. You could even do all three on the same line by changing the semicolon on the std::endl; out for another comma.

I'm guessing that isn't what you wanted though. You wanted to use the output from std::cin directly without the variable, right? You can take a step further toward that with the comma operator:

int money;
std::cout << "How much money do you have?" << std::endl;
std::cout << (std::cin >> money, money) << " $";

You could also use a lambda for this purpose, but the syntax overhead of that is greater than what I wrote above

Unfortunately I don't know that you can get rid of two steps of formatted read, then return variable, without using some kind of sequence operator (as I did) or sticking the steps in a function (eg: a lambda), or using some formatted I/O routine that isn't standard C++ (which kind of amounts to the same thing). The problem is that ">>" is the C++ standard routine for formatted input, and that operator is designed to return the stream, not the variable read out.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134