Hello i'm having a list of 3 int's spaced by 2 white-spaces, and i want to read them and create an object. I will paste my code from data strucutre, class and pre/post data. I cant find the reason for the error. Any help is welcome:
Class.h:
class RAngle{
private:
int x,y,l,b;
int solution,prec;
RAngle(){
x = y = solution = prec = b = l = 0;
}
RAngle(int i,int j,int k){
x = i;
y = j;
l = k;
solution = 0; prec=0; b=0;
}
friend ostream& operator << (ostream& out, const RAngle& ra){
out << ra.x << " " << ra.y<<" " << ra.l <<endl;
return out;
}
friend istream& operator >>( istream& is, RAngle& ra){
is >> ra.x;
is >> ra.y;
is >> ra.l;
return is ;
}
};
dataStructure.h:
template <class T>
class List
{
private:
struct Elem
{
T data;
Elem* next;
};
Elem* first;
void push_back(T data){
Elem *n = new Elem;
n->data = data;
n->next = NULL;
if (first == NULL)
{
first = n;
return ;
}
Elem *current;
for(current=first;current->next != NULL;current=current->next);
current->next = n;
}
main.cpp:
void readData(List <RAngle> &l){
*RAngle r;
int N;
ifstream f_in;
ofstream f_out;
f_in.open("ex.in",ios::in);
f_out.open("ex.out",ios::out);
f_in >> N;
for(int i=0;i<13;++i){
f_in >> r;
cout << r;
l.push_back(r);
}
f_in.close();
f_out.close();
}*
input data:
3 1 2
1 1 3
3 1 1
output(at printing when reading):
1 2 1
1 3 3
1 1 3
As you can see somehow it starts reading with 2-nd position and finishes with first.
The line that reads f_in >> N
is reading a count into a variable first, but your input file lacks that count. (Or rather, it sees the first '3' as the count, which means that the tuples actually start with '1 2 1 ...').
As you can see somehow it starts reading with 2-nd position and finishes with first.
That's because you extract the first value.
In readData
function:
f_in >> N; // This line will extract the first value from the stream.
// the loop starts at the second value
for(int i=0;i<13;++i){
f_in >> r;
cout << r;
l.push_back(r);
}
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.