c ++笛卡尔类不要求用户输入

时间:2014-04-06 21:16:19

标签: c++ class operator-overloading

我的程序应该要求用户在运行时输入两组坐标。但是,当我编译并运行它时,它不会要求任何输入,而是给我输出

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

我该如何解决这个问题?

#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;
}

1 个答案:

答案 0 :(得分:0)

在Windows上,您需要专门配置项目才能从控制台接收输入

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