nested loop problem

Feb 15, 2009 at 3:30pm
I'm sure this is a rookie mistake, but I can't figure my way out of it. I'm trying to list the lines from a text file in a particular order. My "input.txt" file looks like this:
1 2 3 4 5
6 5 4 3 2
3 4 5 6 7
8 7 6 5 4

That's it -- four lines of five numbers each.

When I run this program, I'm hoping that it will list the each line and all lines that come after it. Output SHOULD look like this:
1 2 3 4 5
6 5 4 3 2

1 2 3 4 5
3 4 5 6 7

1 2 3 4 5
8 7 6 5 4

6 5 4 3 2
3 4 5 6 7

6 5 4 3 2
8 7 6 5 4

3 4 5 6 7
8 7 6 5 4

But it doesn't. It starts out ok (except that it lists line 1 with itself, rather than starting with line 1/line 2), but once the bottom line of each pair reaches 8 7 6 5 4, it just sticks there.

I'm sure that it's something with my nested for loop, but I can't figure out what's wrong with it.

If you have any ideas, I'd love to hear them!! Thank you so much.


code/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream ifs("input.txt");
ifstream ifs2("input.txt");
string num1, num2, num3, num4, num5;
string num6, num7, num8, num9, num10;
int loop; // for each line, file 1
int loop2; // for each line, file 2

for (loop=0; loop<=4; loop++)
{
ifs >> num1 >> num2 >> num3 >> num4 >> num5;

for (loop2=loop+1; loop2<=4; loop2++) // I thought loop2=loop+1 should tell it to start at the line AFTER where the first loop is, but it doesn't.
{
ifs2 >> num6 >> num7 >> num8 >> num9 >> num10;

cout << "ifs: " << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << endl;
cout << "ifs2: " << num6 << " " << num7 << " " << num8 << " " << num9 << " " << num10 << endl << endl;
}
}
return 0;
}

/code
Feb 15, 2009 at 3:41pm
You can keep each line in a single string. Lets say we have 4 integers: 1, 2, 3 and 4. Now, we need to find an algorithm to display all possible combinations. The first with the second, third and fourth; the second with the third and fourth and the third with the fourth:
1
2
3
4
5
6
7
for (int i=1;i<=4;i++)
{
    for (int j=i+1;j<=4;j++)
    {
        cout<<i<<" + "<<j<<endl;
    }
}
Feb 15, 2009 at 5:28pm
Thanks, Scipio, this seems to work...at least for the loop. But I need to keep the elements of my string separate from each other, because I'm actually going to do some stuff to the individual elements.

Your loops look the same as mine, but somehow it doesn't work when I stream the text file and extract the elements of the string. Hmmm.
Feb 15, 2009 at 6:33pm
Note that you got only four lines, while you're looping 5 times (you start at zero, I started at 1). That may be part of the problem.

About seperating a line into integers, I posted before (on your previous question I believe :) ):
http://www.cplusplus.com/forum/beginner/7785/

You could use a two-dimensional array to store the integers in:

1
2
3
4
5
6
7
8
const int LINES; //number of lines
const int NUMBERS; //number of integers per line

int value[LINES][NUMBERS]; //an array to store all the numbers in

//now you can easily print all the numbers of a line, eg for the first line:
for (int i=0;i<NUMBERS;i++)
      cout<<value[0][i]<<endl;
Feb 15, 2009 at 8:52pm
I haven't forgotten about your help with the separating lines into integers. I just learn this stuff so slowly, so I'm not ready to put that into this program yet. But I will! :-)

Thanks for the idea about the two-dimensional array. I like that...and I think I can do it! Correct me if I'm wrong -- the advantage would be that I wouldn't be streaming info in during the for loops. It would all stream in, and be held in memory, and then I could access whatever number I wanted to by calling array[x][y] (or whatever)?

If I'm understanding it right, that's a great idea! Thanks!!!
Feb 15, 2009 at 9:01pm
Exactly, the advantage is that you can do the loading of the file and the operations on the integers seperatly. That way you dont have to care about several problems the same time.
Those subjects are pretty complex and it's good to practize a lot with them and really understand them to move on. Good luck.
Feb 16, 2009 at 9:12pm
Scipio -- you are quickly becoming my new best friend.

I was able to get this to work correctly with a two-dimensional array. But now I have a new problem. The text file I REALLY want to use has 11! lines of text (I'm doing this small text file as a test)...so when I set up my array[39916800][23], the compiler yells at me, saying that I can't have an array that large. I suppose I should have seen that coming.

Anyway, I'm back to square one. Can't seem to figure out why the program in my original post loops incorrectly. I'll post it here, with the correction Scipio suggested (I had it looping too many times)

[code=c++]
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ifstream ifs("input.txt");
ifstream ifs2("input.txt");
string num1, num2, num3, num4, num5;
string num6, num7, num8, num9, num10;
int loop; // for each line, file 1
int loop2; // for each line, file 2

for (loop=1; loop<=4; loop++)
{
ifs >> num1 >> num2 >> num3 >> num4 >> num5; // read a line using the extraction operator

for (loop2=loop+1; loop2<=4; loop2++)
{
ifs2 >> num6 >> num7 >> num8 >> num9 >> num10;

cout << "ifs: " << num1 << " " << num2 << " " << num3 << " " << num4 << " " << num5 << endl;
cout << "ifs2: " << num6 << " " << num7 << " " << num8 << " " << num9 << " " << num10 << endl << endl;
}
}
return 0;
}
[/code]

Thanks, everybody, for your help!
Feb 16, 2009 at 9:35pm
Scipio -- you are quickly becoming my new best friend.


I'm honored :)

11! is a lot... You realize it won't fit on the screen (on a normal consal window, you have a few hundred lines or so, including scrolling)

I think the best way to handle this, is by seperating your files in blocks: first the first 100 lines, do whatever you want to do with them, then the second 100 lines etc. etc. You can use a while()-loop to keep on going until the complete file is read.

[edit]
You can use the memberfunction ifstream::good() as condition for the while loop: this function returns true if the file can be read. http://www.cplusplus.com/reference/iostream/ios/good.html
Last edited on Feb 16, 2009 at 9:46pm
Topic archived. No new replies allowed.