Question

The program stops working as it is not reading the text file properly. The text file has the following content:

1 2 
1 3 
1 4 
2 4 
3 4 
0 0 
1 2 
3 2

I want to get two graphs from the text file and 0 0 separates the two.

Thanks in advance.

int main()
{
    ifstream file2;
    file2.open("ass3.txt");
    int i=1;
    int t1;
    int t2;
    file2>>t1>>t2;
    Graph* graph1;
    Vertex* u;
    Vertex* v;
    cout<<t1<<" "<<t2;//this is giving value -2 and some other random number.
    while(t1==0 && t2==0){
        if(t1==i){
            u=addVertex(t1);
            insert(u,graph1);
            i++;
        }
        if(t2==i){
            u=addVertex(t1);
            insert(u,graph1);
            i++;
        }
        *u = graph1->vertices[t1];
        *v = graph1->vertices[t2];
        addEdge(u, v, true);
        addEdge(v, u, false);
        file2>>t1>>t2;
    }
    Graph* graph2;
    i=0;
    while (!file2.eof()){
        file2>>t1>>t2;
        if(t1==i){
            u=addVertex(t1);
            insert(u,graph2);
            i++;
        }
        if(t2==i){
            u=addVertex(t1);
            insert(u,graph2);
            i++;
        }
        *u = graph1->vertices[t1];
        *v = graph1->vertices[t2];
        addEdge(u, v, true);
        addEdge(v, u, false);
    }
return 1;
}
Was it helpful?

Solution

You have the wrong condition in the first while loop, try setting

while(t1!=0 && t2!=0){

instead of

while(t1==0 && t2==0){

With the condition you use, you immediatly hop to the 2nd loop and the entire file is put inside the 2nd graph, resulting in uninitialized values in the first one .

Hope this helps..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top