Frankakn7
Frankakn7

Reputation: 31

How to cout on same line as user input? C++

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

Upvotes: 0

Views: 10551

Answers (4)

einpoklum
einpoklum

Reputation: 131515

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.

Upvotes: 1

Hydes
Hydes

Reputation: 668

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 :)

Upvotes: 1

T.E.D.
T.E.D.

Reputation: 44804

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.

Upvotes: -1

Sailanarmo
Sailanarmo

Reputation: 1181

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)

Upvotes: 0

Related Questions