Output Troubleshooting Assistance!

asasasasas
Last edited on
How much time do you have left before the deadline (I mean, after the late submission)?
2 days
You have plenty of time. Spend your efforts a bit more before you come here to ask for help.
I understand what you mean, but I'm just absolutely stumped at the moment and just can't figure out where I went wrong.
I understand what you mean, but I'm just absolutely stumped at the moment and just can't figure out where I went wrong.

You mean you are having segmentation faults now and you can't fix your code?
Well yeah, that and problems figuring out how to fix the output to print

The shortest path from 1 to 5 has length 27.000 and is
1 -> 3 -> 5.
noelsaan

You have posted on this topic before - but you haven't taken on board all the advice given in that.

The biggest cause of a segmentation fault is writing beyond the end of an array.

At present your line 117, which was drawn to your attention last time, will not compile.
You can't write:
i nVertices
for the loop condition.

Without your included files nobody is going to be able to compile your code.
Well the files are actually included, and line 117 was a typo.
Here - before you go back and change it and confuse the thread - is the code that you have for int main().

Please tell us where, in that code, sV and eV (start and end vertices) are declared? Because my compiler immediately complains. At the time of writing they aren't to be found as global variables or in either header file, either.

You need to supply us complete compileable code, preferably broken down into files - not all slung inside the same code tags. It really doesn't help if your code contains "a typo".


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char** argv)
{
    Graph * g = readGraph();
    int n,c;

//1st Assignment Portion #1-6
    cin>>n>>c;
    printf("\n");
    printGraph(g);

//2nd Assignment Portion #7-12
    djikstra(g, sV, eV);
    cout << "The shortest path from " << sV << " to " << eV << " has length " << distances[eV] << " and is " << endl;
    path(sV, eV);
    cout << endl;
    return 0;
}
Line 158 and 159 are where they're declared. Lines 11 and beyond (on the the code you copied above) give compiler errors because that's where I started to get problems. I don't know how to output it correctly. Also, I'm new to using this forum so I wasn't aware that you could set up multiple files, it should be fine now.
Last edited on
If sV and eV are declared on lines 158 and 159 then they will only be known to function processEvent.
main() will not know what they are, and as they aren't known in the scope of main() your program won't compile.
How would I get sV and eV to be able to be used in the main? That's really what I wanted to know.
Your very first paragraph of the whole post gave the impression that you were going to ask the user for them (1 and 5 there).

Declare them as int in main.

Use cin (preferably with a cout prompt first).

BTW, what are n and c in main? - they don't seem to be doing anything. Also, whenever you expect an input from the user ... then you should prompt him for it. How else would he know when and what to put in.
Yeah I know what you mean by providing a prompt for the user, but my professor is just asking us to write this program and then he puts in the input example + other similar inputs into the command prompt. So basically he knows what to do already, but regardless I should know better anyway and always prompt the user, my bad. But anyways, would this suffice? The thing is, is that I want the 2nd portion to work by just the 1st input alone, I don't believe we're allowed to use two inputs.

int main(int argc, char** argv)
{

//1st Assignment Portion #1-6
printf("Please enter the graph input\n");
Graph * g = readGraph();
printf("\n");
printGraph(g);

//2nd Assignment Portion #7-12
int sV, eV;
cin >> sV >> eV;
djikstra(g, sV, eV);
cout << "The shortest path from " << sV << " to " << eV << " has length " << distances[eV] << " and is " << endl;
path(sV, eV);
cout << endl;
return 0;
}
Last edited on
Why don't you try it and see? Only you know what you are intending with the code.

Repost once you have some code that compiles. If there are error messages then tell us what they are. However, what you ask about must correspond to the code that you post - there is no point talking about segmentation faults when the code that you post could never run.
Have you actually tried to run the program and test it on your end, because it compiles and works.

When I first enter:

5
1 2 9.0
1 3 12.0
2 4 18.0
2 3 6.0
2 5 20.0
3 5 15.0
0
1 5



The 1st output is:

There are 5 vertices and 6 edges.
(1,2) weight:9
(1,3) weight:12
(2,4) weight:18
(2,3) weight:6
(2,5) weight:20
(3,5) weight:15


Which is correct and works fine, but I want it to where it can output:

The shortest path from 1 to 5 has length 27.000 and is
1 -> 3 -> 5


Then, it asks for a second round of input which isn't what I'm supposed to do. So having a 2nd cin in the main isn't necessary.
The goal here is to not have 2 inputs and I would really like to get actual help about this instead of being told the program isn't compiling repeatedly, when it does compile, so if anybody could actually help me with this I'd appreciate it very much.
Can you by looking at your output determine the shortest path? Even when your inputs and outputs change? If so, how?
Last edited on
Have you actually tried to run the program and test it on your end, because it compiles and works. 
I would really like to get actual help about this instead of being told the program isn't compiling repeatedly, when it does compile,

Noelsaan - you have the habit of going back and changing the code in previous posts. The version you have at the top now is not the one you started with. You will notice that I had to cite your (then current version) of main in full because I noticed that you were doing this. It completely confuses the thread - which is meant to be read sequentially - if you change previous code, as nobody knows which version you are talking about. As an example of what happens, note that you write in a previous post about sV and eV (which I will have to quote before you go back and change it):
Line 158 and 159 are where they're declared.

and when I look at the"latest" version of the code (at the time of writing), these lines are now
    event *h;
    h = new event();

Do you not realise that it makes it very difficult for people to follow this thread as your previous post is now null and void? The 'original' version of code which you posted DID NOT compile (and I showed you why by, amongst other things, pointing out your errors in main()) and so could not run - yet you claimed that it was giving segmentation faults.


OK - rant over.

You appear to ask for input in two places:-
In main - at the time of writing(!) this is
    cin >> sV >> eV;
    djikstra(g, sV, eV);

In readGraph() - at the time of writing(!) this is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Graph * readGraph()
{
    int nV;

    cin >> nV;
    Graph * g = new Graph(nV);



    for(int i = 0 ; i <= nV; ++i)
    {
        distances[i] = -1;
        previous[i] = -1;
    }


    while(true)
    {
        int u, v;
        double wt;
        cin >> u;
        if(u == 0)
        {
            break;
        }
        cin >> v >> wt;
        g->vertices[u].addEdge(new Edge(v, wt, NULL));
    }
    return g;
}

Now, main() needs to KNOW the values of sV and eV in order to call djikstra(). Moreover, they must be declared as int somewhere within a scope applicable to main. So it is up to you to decide (a) which is the most appropriate place to enter them and (b) how to get them known by main().
Topic archived. No new replies allowed.