### Need help with possibly a FOR loop or Multiple FOR loop

OK, any help would be very much appreciated. I’m trying to make a program where I’m creating a grid pattern (Easting, Northing and Elevation) and then exporting it into a .txt sheet. Were I'm getting caught up is how to format the FOR loop. Can you do more than a nested FOR loop (ie 4 or 5 times)?Here are the questions I ask:

• What is your starting Easting point.
• What is the Easting spacing (This is how far the new point would be east)
• What is your starting Northing point.
• What is the Northing spacing(This is how far the new point would be North)
• How many columns will there be (this is how many times the Easting will repeat itself. Essentially columns)
• How many rows will there be(this is how many times the northing will repeat itself. Essentially rows)
• What elevation will it be starting? (essentially how deep the Z will start to output)
• What elevation will it be terminating? (essentially when the Z will stop outputting)
• What is the elevation spacing? (How many times I want the Z to output)

I’ll give you an example of what I’m looking for:
• What is your starting Easting point. = 100
• What is the Easting spacing (This is how far the new point would be east) = 5
• What is your starting Northing point. = 200
• What is the Northing spacing(This is how far the new point would be North) 10
• How many columns will there be (this is how many times the Easting will repeat itself. Essentially columns) = 5
• How many rows will there be(this is how many times the northing will repeat itself. Essentially rows) = 10
• What elevation will it be starting? 10
• What elevation will it be terminating? 50
• What is the elevation spacing? 5

EAST NORTH Elevation
100 200 10
100 200 15
100 200 etc to 50
105 200 10
105 200 15
105 200 etc to 50
This will go on to when Easting reaches 150 because I wanted 5 foot spacing and wanted it to happen 10 times.
Then we start on the next row where the north has changed to 210 and it will end at 300 because I wanted 10 spacing
EAST NORTH Elevation
100 210 10
100 210 15
100 210 etc to 50
100 220 10
100 220 15
100 220 etc to 50

Here is my current code:
#include <iostream>
#include <fstream>
#include <ostream>
#include <cmath>
#include <string>
#include <iomanip>
using namespace std;

void mainInfo(double & easting, double & northing, double & elevation, int & rows, int & columns, int & eastSpace, int & northSpace, int & elevSpace);
void getInfoSame(double & easting, double & northing, double & elevation, int & rows, int & columns, int & eastSpace, int & northSpace, int & elevSpace);
void getInfoDiff();

int main()
{

double easting;
double northing;
double elevation;
int rows;
int columns;
int eastSpace;
int northSpace;
int elevSpace;

mainInfo(easting, northing, elevation, rows, columns, eastSpace, northSpace, elevSpace);

cout << "Will the elevation of each row the same? (Y or N)";

{
getInfoSame(easting, northing, elevation, rows, columns, eastSpace, northSpace, elevSpace);
}
else
getInfoDiff();

return 0;
}

void mainInfo(double & easting, double & northing, double & elevation, int & rows, int & columns, int & eastSpace, int & northSpace, int & elevSpace)
{
cout << "What is your starting Easting point: ";
cin >> easting;
cout << "\nWhat is the Easting spacing: ";
cin >> eastSpace;
cout << "\nWhat is your starting Northing point: ";
cin >> northing;
cout << "\nWhat is the Northing spacing: ";
cin >> northSpace;
cout << "\nHow many columns will there be: ";
cin >> columns;
cout << "\nHow many rows will there be: ";
cin >> rows;
}

void getInfoSame(double & easting, double & northing, double & elevation, int & rows, int & columns, int & eastSpace, int & northSpace, int & elevSpace)
{
ofstream outFile;
int injPoint = 1;
int depth=1;
system("cls");
cout << "What is the elevation: ";
cin >> elevation;
outFile.open("z:\\Outfiles\\Outfile.txt");

outFile << "x\ty\tElevation\t@@InjectionPoint\tInj_Point\n";
outFile << "depth\tfeet\n";
outFile << "=count(A4:A10000)\t1\n";

for(int i=0; i<=columns; i++)
{
for(int j=0; depth<=elevation;depth++)
outFile << easting << "\t" << northing << "\t" << depth << "\t1" << "\t" << "Inj_Point-" << injPoint << endl;

}
outFile.close();
}

void getInfoDiff()
{
cout << "You got info";
}

Hi there,

Yes, it's perfectly fine to nest for-loops.
Do keep in mind that as you nest them. complexity grows quite fast, ie:

 ``1234567891011`` ``````for (int i=0; i<10; i++) //this line is executed 10 times for (int i=0; i<10; i++) for (int j=0; j<10; j++) //this line is executed 100 times for (int i=0; i<10; i++) for (int j=0; j<10; j++) for (int r=0; r<10; r++) //this line is executed 1000 times ``````

This may have an impact if you're executing heavy duty code within the loops.

On a sidenote, please wrap your code in [code][/code] tags in the future - it makes it more readable for us.

Please do let us know if you have any further questions.

All the best,
NwN
Last edited on
Thank you very much for the help and the future side note.
Topic archived. No new replies allowed.