How to input 2 inputs in 1 line???

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

  1. Jay

    Jay Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. Jay

    newtothis Guest

    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
    #4
  5. Jay

    Brett 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???


    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
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hugo
    Replies:
    10
    Views:
    1,320
    Matt Humphrey
    Oct 18, 2004
  2. jeff regoord
    Replies:
    10
    Views:
    612
    Irrwahn Grausewitz
    Sep 13, 2003
  3. kaushikshome
    Replies:
    4
    Views:
    775
    kaushikshome
    Sep 10, 2006
  4. scad
    Replies:
    23
    Views:
    1,174
    Alf P. Steinbach
    May 17, 2009
  5. Marek Stepanek
    Replies:
    12
    Views:
    417
    Peter J. Holzer
    Sep 2, 2006
Loading...

Share This Page