# How to input 2 inputs in 1 line???

Discussion in 'C++' started by Jay, Oct 22, 2003.

1. ### JayGuest

Enter the first point coordinates: 3 4
Enter the second point coordinates: 0 0
Distance between (3.00, 4.00) and (0.00, 0.00) is 5.00

the value 3, 4, 0, 0 are the input position for users
if i program as:

{
int x1, y1;
cout << "Enter the first point coordinates: ";
cin >> x1;
cin >> y1;
return 0;
}
the x1 and y1 input position are separated in 2 lines!
how can i solve this problem???

Jay, Oct 22, 2003

2. ### =?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=Guest

Jay wrote:
> Enter the first point coordinates: 3 4
> Enter the second point coordinates: 0 0
> Distance between (3.00, 4.00) and (0.00, 0.00) is 5.00
>
> the value 3, 4, 0, 0 are the input position for users
> if i program as:
>
> {
> int x1, y1;
> cout << "Enter the first point coordinates: ";
> cin >> x1;
> cin >> y1;
> return 0;
> }
> the x1 and y1 input position are separated in 2 lines!
> how can i solve this problem???

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int main() {
double x,y;
while (1) {
cout << "Prompt:" << flush;
cin >> x >> y;
cout << "x=" << x << " y=" << y << " Module:" << sqrt(x*x+y*y) << endl;
}
return 1;
}

=?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=, Oct 22, 2003

3. ### Karl Heinz BucheggerGuest

Jay wrote:
>
> Enter the first point coordinates: 3 4
> Enter the second point coordinates: 0 0
> Distance between (3.00, 4.00) and (0.00, 0.00) is 5.00
>
> the value 3, 4, 0, 0 are the input position for users
> if i program as:
>
> {
> int x1, y1;
> cout << "Enter the first point coordinates: ";
> cin >> x1;
> cin >> y1;
> return 0;
> }
> the x1 and y1 input position are separated in 2 lines!
> how can i solve this problem???

Did you try the above?
Even if you have 2 seperate cin >> ...
statements you still can enter 2 numbers. cin >> ... works
by reading from the input stream what it needs to read (a number
in your case). The rest waits in the input stream until a cin >> ...
comes to fetch it.

But of course you could do:

cin >> x1 >> y1;

--
Karl Heinz Buchegger

Karl Heinz Buchegger, Oct 22, 2003
4. ### newtothisGuest

yuo could always resort to something nasty like printf() and scanf().
They are still available in C++. You would also still have your prompt
and input on one line.

--
Posted via http://dbforums.com

newtothis, Oct 22, 2003
5. ### BrettGuest

Jay wrote:
> Enter the first point coordinates: 3 4
> Enter the second point coordinates: 0 0
> Distance between (3.00, 4.00) and (0.00, 0.00) is 5.00
>
> the value 3, 4, 0, 0 are the input position for users
> if i program as:
>
> {
> int x1, y1;
> cout << "Enter the first point coordinates: ";
> cin >> x1;
> cin >> y1;
> return 0;
> }
> the x1 and y1 input position are separated in 2 lines!
> how can i solve this problem???

Try this:
int main(){
int a, b;
a = 0;
b = 0;
cout << "Enter the 2 coords (space seperated):\n";
cin >> a >> b;
cout << "a: " << a << " b: " << b << endl;
return 0;
}

Brett, Oct 25, 2003