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.
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.
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;
}
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.
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.
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
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().