19

Whenever I input a variable using cin, after one hits enter it automatically goes to a new line. I'm curious if there's a way to use cin without having it go to a new line. I'm wanting to cin and cout multiple things on the same line in the command prompt. Is this possible?

4
  • @andre When you press return it should stay on the same line. Commented Mar 4, 2013 at 19:28
  • 1
    No. It's not a language thing. It's how the console window works. Commented Mar 4, 2013 at 19:31
  • How about using a while loop to check if a newline is entered (by pressing enter), and if it is just use cin again to continue inputting? Commented Mar 4, 2013 at 19:32
  • 3
    Most applications that need this functionality use an OS specific function for getting single character input without echoing to the output. Commented Mar 4, 2013 at 20:07

7 Answers 7

8

You can't use cin or any other standard input for this. But it is certainly possible to get the effect you are going for. I see you're on Windows using Visual Studio, so you can use, for example, _getch. Here's an example that reads until the next whitespace and stores the result in a string.

#include <conio.h> // for _getch

std::string get_word()
{
    std::string word;
    char c = _getch();
    while (!std::isspace(c))
    {
        word.push_back(c);
        std::cout << c;
        c = _getch();
    }
    std::cout << c;
    return word;
}

It's not very good. For example, it doesn't handle non printing character input very well. But it should give you an idea of what you need to do. You might also be interested in the Windows API keyboard functions.

If you want a wider audience, you will want to look into some cross-platform libraries, like SFML or SDL.

Sign up to request clarification or add additional context in comments.

Comments

7

you can also use space for input instead of enter something like this:

cin >> a >> b >> c;

and in input you type

10 20 30

then

a=10
b=20
c=30 

Comments

2

As others have noted, you can't do this with cin, but you could do it with getchar(). What you would have to do is:

  1. collect each character individually using getchar() (adding each to the end of a string as it is read in, for instance), then
  2. after reading each character, decide when you've reached the end of one variable's value (e.g. by detecting one or more ' ' characters in the input, if you're reading in int or double values), then
  3. if you've reached the end of the text for a variable, convert the string of characters that you've built into a variable of the appropriate type (e.g. int, double, etc.), then
  4. output any content onto the line that might be required, and then
  5. continue for the next variable that you're reading in.

Handling errors robustly would be complicated so I haven't written any code for this, but you can see the approach that you could use.

1 Comment

Correction: When I saw @Benjamin Lindley's answer I tried writing some quick code to do this with getchar() as I'd suggested and found that I couldn't. No matter what I did (e.g. flushing the output, etc.) I couldn't get the output to write until after I had pressed "Enter" on the input line, so I couldn't intersperse input and output on the same line.
1

I don't think what you want to do can be achieved with cin. What you can do is to write all your input in one line, with a delimiter of your choosing, and parse the input string.

Comments

0

It is not possible. To quote @Bo Persson, it's not something controlled by C++, but rather the console window.

Comments

0

I can't comment but if you leave spaces between integers then you can get the desired effect. This works with cin too.

int a, b, c;
cin>>a; cin>>b; cin>>c;

If you enter your values as 10 20 30 then they will get stored in a, b, and c respectively.

Comments

0

just use the gotoxy statement. you can press 'enter' and input values in the same line for eg. in the input of a 3*3 matrix:

#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a[20][20],x,y;
cout<<"Enter the matrix:\n ";
for(int r=2;r<7;r+=2)
for(int c=2;c<7;c+=2)
{gotoxy(c,r);
cin>>a[r][c];
}
getch();}

1 Comment

int main! :P Please, by the gods, say that you don't write such code in the real world.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.