简体   繁体   中英

c++ Cartesian class not asking for user input

My program is supposed to ask the user to input two sets of coordinates at runtime. However, when I compile and run it it does not ask for any input and instead gives me this output

Please enter the first coordinates in the form x y: 
Please enter the second coordinates in the form x y:
(0, 0)
(0, 0)

How can I fix this?

#include <iostream>
#include <istream>
#include <ostream>

using namespace std;

class Cartesian
{
private:
    double x;
    double y;
public:
    Cartesian(double = 0, double = 0);
    friend istream& operator>>(istream&, Cartesian&);
    friend ostream& operator<<(ostream&, const Cartesian&);
};

Cartesian::Cartesian(double a, double b)
{
    x = a;
    y = b;
}

istream& operator>>(istream& in, Cartesian& num)
{
    in >> num.x; in >> num.y;

    return in;
}

ostream& operator<<(ostream & out, const Cartesian& num)
{
    cout << "(" << num.x << ", " << num.y << ")" << endl;

    return out;
}

int main()
{
    Cartesian coord1, coord2;
    cout << "Please enter the first coordinates in the form x y: ";
    cin >> coord1;
    cout << "Please enter the second coordinates in the form x y: ";
    cin >> coord2;
    cout << coord1;
    cout << coord2;

    return 0;
}

On Windows you need to specially configure your project in order to recieve input from console

http://www.cplusplus.com/doc/tutorial/introduction/visualstudio/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM