0

You can see by the "In what file is the data for the graph contained?" prompt, that I have to input a file to get the code to function properly. The program gets up to "In what file is the data for the graph contained?", then I input a file name, it freezes then crashes. My whole computer freezes and other applications begin to crash. I even ran the code by removing my own code, and the same problem occurs. What is causing this? And how can I fix this?

#include <fstream>
#include <iostream>
using namespace std;

struct edge {
    struct vertex* Vertex;
    int weight;
    edge* nextedge;
    edge(edge* e = 0, struct vertex* v = 0, int w = 0)
    {
        Vertex = v;
        weight = w;
        nextedge = e;
    }
};
struct vertex {
    char name;
    vertex* nextvertex;
    edge* edgelist;
    int index;
    bool final;
    vertex* pre;
    vertex(char n = '\0', vertex* v = 0)
    {
        name = n;
        nextvertex = v;
        edgelist = 0;
        index = -1;
        final = false;
        pre = 0;
    }
};

int main()
{
    char input_file[128];
    cout << "In what file is the data for the graph contained?\n> ";
    cin.getline(input_file, 128);
    ifstream infile(input_file);
    vertex* graph = 0;
    vertex *startptr = 0, *finishptr = 0;
    vertex *vertexsearch = 0, *vptr = 0;
    vertex* last;
    vertex* w;
    edge* edgeptr = 0;
    int weight;
    char start, finish, comma;
    bool startnotfound = true, finishnotfound = true;
    infile >> start >> comma >> finish >> comma >> weight;
    while (!infile.eof()) {

        /* build the edge list */
        startptr = new vertex(sizeof(struct vertex));
        finishptr = new vertex(sizeof(struct vertex));
        finishptr->name = finish;
        startptr->name = start;
        startptr->nextvertex = finishptr;

        edgeptr = new edge();

        edgeptr->Vertex = startptr;
        edgeptr->weight = weight;
        edgeptr->nextedge = NULL;

        graph = new vertex(sizeof(struct vertex));
        graph->name = finish;
        graph->edgelist = edgeptr;
    }

    // Output the graph
    vptr = graph;
    while (vptr) {
        cout << vptr->name << '\n';
        edgeptr = vptr->edgelist;
        while (edgeptr) {
            cout << " Edge to " << edgeptr->Vertex->name
                 << " with weight " << edgeptr->weight << '\n';
            edgeptr = edgeptr->nextedge;
        }
        vptr = vptr->nextvertex;
    }

    // From where to where
    cout << "From where: ";
    cin >> start;
    cout << "to where: ";
    cin >> finish;

    vertex* s = graph;
    startptr = finishptr = 0;
    while (s) {
        if (s->name == start) {
            startptr = s;
        }
        if (s->name == finish) {
            finishptr = s;
        }
        s = s->nextvertex;
    }

    if (!startptr) {
        cout << "Start point given is not a valid vertex.\n";
        return 1;
    }
    last = startptr;
    last->index = 0;
    last->final = true;
    while (!(finishptr->final)) {
        /* Search for shortest path */
    }

    vptr = finishptr;

    if (vptr->pre)
        while (vptr) {
            cout << vptr->name << '\n';
            vptr = vptr->pre;
        }
    else
        cout << "No such path.\n";

    return 0;
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
sdf ERG
  • 1
  • 1
  • 1
    https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons?r=Saves_AllUserSaves – πάντα ῥεῖ Dec 12 '22 at 19:28
  • Unfortunately, we're not allowed to change any of the given code. If I had looked at the problem earlier, I would've contacted my teacher, but it's too late to ask now. – sdf ERG Dec 12 '22 at 19:32
  • 1
    `startptr=new vertex(sizeof(struct vertex));` Looks like you are mixing up `new` and `malloc` – 001 Dec 12 '22 at 19:33
  • You should try steeping through the code with your debugger to see where it might be causing an issue. It could be something like a memory leak in an infinite loop bringing your machine to its knees. – NathanOliver Dec 12 '22 at 19:34
  • 4
    The loop `while (!infile.eof())` doesn't seem to read from`infile`, so how is it supposed to terminate? – BoP Dec 12 '22 at 19:36
  • @BoP And there is the loop with the memory leak \o/ ;) – NathanOliver Dec 12 '22 at 19:37

0 Answers0