file output.

What am i doing wrong here? The file distances.txt stays empty even when i get output by the cout.

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
int main ()
{
  int i,j;
  float l;
  ofstream myfile ("distances.txt");
  myfile.open("distances.txt");
  float distances[5][5],pos[5][2],param;
  bool neighbors[3][3];
    for(i=0;i<5;i++)
     {
       cout<<"dose to x tou node   "<<i+1<<"  ";
       cin >> pos[i][1];
       cout << "dose to psi tou node   " << i+1<<"  ";
       cin >> pos[i][2];
      }
         for(j=0;j<5;j++)
            {
            for(i=j+1;i<5;i++)
              {
                 param=(pos[j][1]-pos[i][1])*(pos[j][1]-pos[i][1])+(pos[j][2]-pos[i][2])*(pos[j][2]-pos[i][2]);
                 l=sqrt(param);
                 distances[j][i]=l;
				 if (l<4)
				       {
				         neighbors[j][i]=true;
				         myfile<<j+1<<","<<i+1<<"=neighbors" << endl;
				         cout<<j+1<<","<<i+1<<"=neighbors" << endl;
					   }	 
				 else neighbors[j][i]=false;     
                 
               }   
            }
            myfile.close();
            wait(200);                   
}
Maybe program doesn't enter in scope of if
1
2
3
4
5
6
if (l<4)
{
        neighbors[j][i]=true;
         myfile<<j+1<<","<<i+1<<"=neighbors" << endl; 
         cout<<j+1<<","<<i+1<<"=neighbors" << endl;
}

BTW , you don't need to enter file name twice
1
2
ofstream myfile ("distances.txt");
 myfile.open("distances.txt"); // <-- this line is unecessary 
Thanks. I erased the line you indicated and it worked.
Topic archived. No new replies allowed.