1

In c++, if we make program to take input from user, it is either some integer or character.

After that input, the next output or next input is written on the next line automatically.

#include <iostream>

int main()
{
    int a,b;
    std::cout<<"Enter two numbers to add\n";
    std::cin>>a;
    std::cin>>b;
    std::cout<< a+b;
}

the output is like this

Enter two numbers to add
3
5
8

I want 3 and 5 written in same line..

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    SuperUser is not about programming, this rather belongs to StackExchange. Flagged for moving. – slhck May 02 '11 at 17:42
  • 5
    @slchk "Stack Exchange" is a quite generic destination ;) I suggest StackOverflow instead. – Daniel Beck May 02 '11 at 17:53
  • @Daniel Whoops, I spent waaay too many hours in front of the screen today. – slhck May 02 '11 at 18:10
  • While this does belong on Stack Overflow, and probably will be moved there shortly, you will probably want to provide a coded example of what you are trying to do and/or what you have already. When asking for coding help, you will get the best response if you provide at least some code. – MaQleod May 02 '11 at 18:42
  • Please post a small but complete program that demonstrates what you're talking about, or at least *some* code. Your question is probably answerable, but only if we can see the exact code that's causing you problems. – razlebe May 02 '11 at 22:09

2 Answers2

1

The word you're looking for is called "parsing". You take the entire input in as 1 variable, and split it up using whatever method is appropriate for you. Some programming languages have some built-in methods for breaking up a string input into an array based on a value you supply. You might want to take a look at: Split a string in C++?

Community
  • 1
  • 1
TheCompWiz
  • 233
  • 1
  • 10
  • I don't think this is what the OP's problem is. He speaks of writing output across multiple lines rather than on one line - what has that to do with parsing? – razlebe May 02 '11 at 22:11
  • @razlebe: Quite a lot. The root cause of the OP's problem is that, when taking an input per line, most terminals will render the newline so that _any_ further activity will render on the next line. Parsing two inputs from one single line of input will, clearly, prevent the second input from appearing on a second line in the terminal. – Lightness Races in Orbit May 02 '11 at 22:39
  • @Tomalak Geret'kal - Fair point. :) I guess the water was muddied for me by that first sentence in the answer. Parsing is a related problem he'll have to solve once he understands the root cause of his current problem, rather than the root cause of the problem in of itself. – razlebe May 02 '11 at 22:53
  • @Numair Siddique This answer and the comments here should have helped you figure it out. – razlebe May 02 '11 at 22:55
0

That the input is appearing on two seperate lines has nothing to do with your program; this is because, while typing the input, you pressed <Enter> after 3. The resulting newline was rendered by your terminal / console, not your program.

Using istream::operator>>( int ) will automatically skip leading whitespace. So your user could also write 3 5 on one line. std::cin>>a; would consume the 3, and std::cin>>b; would skip the space and consume the 5. Your terminal / console would look like this:

Enter two numbers to add
3 5
8

Note that this is completely unrelated to your program code, though. If the user presses <Enter> between numbers, there is nothing you can do about it, short of taking over complete control of the terminal / console.

This can be done, using _getch() on Windows, ncurses on many other systems, or whatever the OS in question provides. You would be reading keypresses directly, without the terminal / console echoing what is entered. You would then be responsible for the echo, line editing etc.

That is a completely different question, though.

DevSolar
  • 67,862
  • 21
  • 134
  • 209