I'm making a program that works with points and files. I have no warnings or errors, but it still doesn't work as it should. I think the problem is with the ifstream, cause the ofstream works well and puts entered the values into the file.
The output that i get looks like this
Please enter seven (x,y) pairs:
//here the seven pairs are entered
These are your points:
//(...,...)x7 with the values
These are the points read from the file:
//and the program ends and returns 0
I hope someone can help me. Here's my code.
#include <iostream>
#include "std_lib_facilities.h"
using namespace std;
struct Point{
float x;
float y;
};
istream& operator>>(istream& is, Point& p)
{
return is >> p.x >> p.y;
}
ostream& operator<<(ostream& os, Point& p)
{
return os << '(' << p.x << ',' << p.y << ')';
}
void f() {
vector<Point> original_points;
cout << "Please enter seven (x,y) pairs: " << endl;
for (Point p; original_points.size() < 7;) {
cin >> p;
original_points.push_back(p);
}
cout << endl;
cout << "These are your points: " << endl;
for (int i=0; i < 7; i++) {
cout << original_points[i] << endl;
}
string name = "mydata.txt";
ofstream ost {name};
if (!ost) error("can't open output file", name);
for (Point p : original_points) {
ost << '(' << p.x << ',' << p.y << ')' << endl;
}
ost.close();
ifstream ist{name};
if (!ist) error("can't open input file", name);
vector<Point> processed_points;
for (Point p; ist >> p;) {
processed_points.push_back(p);
}
cout << endl;
cout << "These are the points read from the file: " << endl;
for (int i=1; i <= processed_points.size(); i++) {
cout << processed_points[i] << endl;
}
}
int main()
{
f();
return 0;
}
You output brackets and a comma but you don't consume them so your first read operation will fail. Try:
istream& operator>>(istream& is, Point& p)
{
char open;
char close;
char comma;
is >> open >> p.x >> comma >> p.y >> close;
if (open != '(' || close != ')' || comma != ',')
{
is.setstate(std::ios_base::failbit);
}
return is;
}
Your program will also crash at the end due to going out of the bounds of the vector, you are using indexes from 1
to length
, vectors are 0-indexed so should be accessed from 0
to length-1
:
for (int i = 0; i < processed_points.size(); i++) {
cout << processed_points[i] << endl;
}
Or just use a range based loop:
for (auto& point : processed_points) {
cout << point << endl;
}