file vector data transport

Is it possible that the same code works for one program but not in another cpp file? when I write it exactly same in another cpp file it doesn't work!!!
here is the code
vector<float>row;
vector<vector<float>>myvec;
int i,j;
float y;
ifstream myfile("C:\\Users\\dell\\Desktop\\iris_test.txt");
if(myfile.is_open())
{
for(j=0;j<75;j++)
{
row.clear();
for(i=0;i<5;i++)
{
myfile>>y;
row.push_back(y);
}
myvec.push_back(row);
}
}
else{cout<<"myfile is not open"<<endl;}
cout<<myvec.at(0).at(3);

If this is the entire source then I think you are missing main?
First of all, format use the [code] tags to format your code. Second, post your entire code. Third, what does "not work" mean?
actually it works but the data inside the vector is not the same with the file.
It'S all 1. what this program tries to do is that it takes 5 column 75 rows of data from text file. ıt takes each row compares it with the 74 other rows and predicts its class which is also given in the text file as the 5th column.


this is my class implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include"easyiris.h"
using namespace std;
bool sorting (float a, float b) {return a<b;} 
IRIS::IRIS(){}
int IRIS::process(int l,int k)
{
	ifstream myfile ("C:\\Users\\dell\\Desktop\\iris_test.txt");
	float y;
	int i,j;
	vector<vector<float>>iris;
	iris.clear();
	vector<float>row;
	if(myfile.is_open()){
		for(i=0;i<75;i++)
	{
		row.clear();
		for(j=0;j<5;j++)
		{
			myfile>>y;
			row.push_back(y);
		}
		iris.push_back(row);
	}
	}else
		cout<<"the file is not open"<<endl;

	
	
	float currentcol1=iris.at(l).at(0);
	float currentcol2=iris.at(l).at(1);
	float currentcol3=iris.at(l).at(2);
	float currentcol4=iris.at(l).at(3);
	float currentcol5=iris.at(l).at(4);
	iris.erase(iris.begin()+l);




	vector<float>forrow;
	vector<float>diff;
	vector<vector<float>>diffandclass;
	float d;
	for(int i=0;i<74;i++)
	{
		forrow.clear();
		forrow.push_back(iris.at(i).at(4));
		d=sqrt((currentcol1-iris.at(i).at(0))*(currentcol1-iris.at(i).at(0))+
			(currentcol2-iris.at(i).at(1))*(currentcol2-iris.at(i).at(1))+
			(currentcol3-iris.at(i).at(2))*(currentcol3-iris.at(i).at(2))+
			(currentcol4-iris.at(i).at(3))*(currentcol4-iris.at(i).at(3)));
			diff.push_back(d);
			forrow.push_back(d);
			diffandclass.push_back(forrow);
	}


	vector<float>neighbor;
	for(int i=0;i<k;i++)
	{
		for(int j=0;j<74;j++)
		{
			if(diffandclass.at(j).at(1)==diff.at(i))
			{
				neighbor.push_back(diffandclass.at(j).at(0));
				
				break;
			}
		}
	}


	vector<int>classholder;
	int counter1=0,counter2=0,counter3=0;
	for(int i=0;i<k;i++)
	{
		if(neighbor.at(i)==1)
			counter1++;
		else if(neighbor.at(i)==2)
			counter2++;
		else
			counter3++;
	}
	classholder.push_back(counter1);
	classholder.push_back(counter2);
	classholder.push_back(counter3);
	sort(classholder.begin(),classholder.end(),sorting);
	if(classholder.at(2)==counter3)
		return 3;
	else if(classholder.at(2)==counter2)
		return 2;
	else
		return 1;
}

	
	
	
IRIS::~IRIS(){}


this is my class definition
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<math.h>
#include<vector>
#include<fstream>
#include<algorithm>

using namespace std;
class IRIS
{
	
public:
	IRIS();
	int process(int l,int k);
	~IRIS();
};

and this is main part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include"easyiris.h"
using namespace std;

void main()
{
	IRIS myiris;
	int k,l,clas;
	cin>>k;
	cin>>l;
	clas=myiris.process(l,k);
	vector<int>classholderr;
	classholderr.push_back(clas);
	
	system("pause");
}

Last edited on
Topic archived. No new replies allowed.