Problems with loops

I have a code whose job is to read in, approximately, 255^2 data sets from a .txt file, and then output one specific type of data from this file - i.e. the original file has data for x, y, and z, and the code is to only output the z points. It can read the file, but it stops after reading the first set, and then goes on to reading that same set, and outputting it, 255 times. Can anyone explain what it is I am doing wrong?

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
//take file produced by boundary
//read 255 lines, and print the densities
//next line down print 255 densities 
//etc etc 255x255
//p.s. include a space between the desnities

#include<iostream>
#include<cmath>
#include<fstream>
#include<string>
#include<vector>
#include<cstdlib>
#include<string>
#include<sstream>
using namespace std;

int main()
{
    vector<double> density;
    vector<double> plot;
    double X = 1.0;
    double Y = 1.0;
    double d = 0.0;
    int i;
    
    
    ifstream myfile("grid2.txt");
    ofstream file2("grid3.txt");
    
    if(myfile.is_open()) //works
    {
        while (myfile >> i >> X >> Y  >> d)
        {
            //for(int j = 1; j <= 255; j++)
            //{    
                
                for(int k = 1; k <= 255; k++)
                {
                    //plot.push_back(d); ///wrong out pit
                    //plot[k] = d; // there's still a problem....
                    
                    
                    cout<< d << " "; ///wrong output
                    
                    
                }
                ///density.push_back(plot);//"nonclass type vector<vector>>"
                ///file2 << density[j][0] << " ";
                
            //} 
                           
        }
  
        myfile.close(); //works
        file2.close();
     
     }
    
    
    return 0;
}
I think that your nested for loop is unnecessary, it's saying to output the same variable 'd' 255 times before your while loop has a chance to change the value of d. Your first while loop is already scrolling through the file line by line until it reaches the end. If you want to limit the lines read then I suggest looking up how to use sentry variables.

Also, if your original file (grid2.txt) has three numbers ( x y z ) each line, then why are you asking to retrieve four numbers ( i x y z ) in your while loop?
Last edited on
I tried that, but I only end up with the first value of 'd'. I need to output the first 255^2 values of it, and I can't see how to do that without a 'for loop' somewhere.
The file has 4 numbers in it, I used 3 as an arbitrary example.
Well to output the first 255^2 values of d you have to read each one before outputting it. Try removing lines 37,38, and 46 and see what happens.
All I get is '-1' one time - '-1' is the first value the program is to read from the file in question .
Topic archived. No new replies allowed.