I’m having a problem with a part of code. It should work,since it is error free, and it has no logical problems since it does work on someone else pc, but on my computer the result is the same as the input.
code(runnable):
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class RAngle{
public:
int x,y,l;
RAngle(){}
RAngle(int i,int j,int k){
x=i,y=j,l=k;
}
bool operator<(const RAngle& rhs)const{
if(l < rhs.l){
return true;
}
return 0;
}
friend ostream& operator << (ostream& out, const RAngle& ra){
out << ra.x << " " << ra.y << " " << ra.l;
return out;
}
friend istream& operator >>( istream& is, RAngle &ra){
is >> ra.x;
is >> ra.y;
is >> ra.l;
return is ;
}
};
void descrSort(vector <RAngle> &l){
for(unsigned i=0; i<l.size();i++){
cout<<l[i]<<endl;
}
cout << "==================" << endl;
sort(l.begin(),l.end());
reverse(l.begin(),l.end());
for(unsigned i=0; i<l.size();i++){
cout<<l[i]<<endl;
}
}
void readData(vector <RAngle> &l){
RAngle r;
ifstream f_in;
f_in.open("test.txt",ios::in);
for(int i=0;i<10;++i){
f_in >> r;
l.push_back(r);
}
}
int main(){
vector <RAngle> a;
readData(a);
descrSort(a);
return 0;
}
DATA:
1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
1 7 31
The output on other machine(only print part after, the descr sort):
1 7 31
2 3 25
5 4 15
10 3 10
1 1 9
4 5 7
3 3 3
2 2 2
10 5 1
4 5 1
on my computer(hoel output):
1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
==================
1 7 31
2 2 2
3 3 3
4 5 1
10 5 1
1 1 9
10 3 10
4 5 7
5 4 15
2 3 25
This means your code has an error. C and C++ let things compile and run that actually have errors in them. Like this:
Will do different things on different machines. I would call it an error. The compiler won’t.
Now as for where your error is, here is my “use a debugger” speech. Use a debugger. It will take you less time to use a debugger and have a decent chance of finding the error than it did for me to read your code to see if anything jumped out. Compile with
-g. Google “gdb cheat sheet.” Run with gdb. Follow the cheat sheet. See where your code does something unexpected.Seems smart to do this on the machine that is giving the wrong output. And see where it’s doing something wrong.