Моя программа должна попросить пользователя ввести два набора координат во время выполнения. Однако, когда я компилирую и запускаю его, он не запрашивает ввода и вместо этого дает мне этот вывод
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;
}
В Windows вам нужно специально настроить свой проект, чтобы получить вход с консоли
http://www.cplusplus.com/doc/tutorial/introduction/visualstudio/
<ostream>
<iostream>
, то вам не нужны<ostream>
или<istream>
. Эти заголовки просто предоставляют интерфейс ввода-вывода, но не создают экземплярыstd::cout
илиstd::cin
.