tr6699 0 Newbie Poster

This program reads an the following input from a file

1 2 3
2 4 5
3 5
4 6
5 6
6 0

Then inserts the values into a two dimensional array. For example the first line 1 2 3 would insert a value of 1 into map[1][2] and 1[3]

After it goes through the array looking for different distinct paths, making the value a -1 if it has already been there. My output is where I am receiving errors. The program outputs the following:

12460 13560 1250124

the desired output would be

12460 13560 12560

I am not real sure why it skips the 6 on the third number and I know that the 124 is a result from running through the loop an additional 3 times before it quits out. I however have not been able to come up with a solution to fix this. Any and all help would be much appreciated. Thank you very much.

#include<iostream>
#include<string>
#include<fstream>
#include<sstream>

using namespace std;

int main() {
	int x = 0;
	string line;
	ifstream infile("basis.in");
	string s;
	int pos = 0;
	int path;
	
	if (!infile) {	
        cout << "There was a problem opening file ";
        return 0;
    }
	infile >> x;
    int map[x+1][x+1];
	for(int j=0;j<x+1;j++){
		for(int k=0;k<x+1;k++){
			map[j][k]=0;
		}
	}		
		
	for(int i = 0; i<x+1; i++){
       getline(infile, s);
	   for(int k = 1; k<s.size(); k++){
		s.erase(0,2);
		istringstream buffer(s);
		buffer>>pos;
		map[i][pos]=1;
		}
	}
	
		
	int m=1;
	int blah=0;
	bool path_found = false;
	do
	{
		for(int n=0;n<(x+1);n++){
			if(map[m][n]==-1){
				blah=(n+1);
				if(blah>=(x+1))
					blah=0;
				for(blah;blah<(x+1);blah++){
					if(map[m][blah]==1){
						map[m][blah]=-1;
						path=m;
						path_found = true;
						cout<<path<<endl;
						m=blah;
						n=0;
					}
				}
				path=m;
				path_found=false;
				cout<<path<<endl;
				m=n;
				n=0;	
				if(m==0){
					path=0;
					cout<<path<<endl;
					m=1;
					path_found=false;
					}
			}
			if(map[m][n]==1){
				map[m][n]=-1;
				path=m;
				path_found = true;
				cout<<path<<endl;
				m=n;
				n=0;
				}
			if(m==0){
				path=0;
				cout<<path<<endl;
				m=1;
				path_found=false;
			}
		}

	}
	while(m<(x+1) && path_found);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.