Good evening all, I need some help fixing the readgraph function for my minimal spanning tree assignment. This function reads a copy and pasted format such as this...
5
1 2 9
1 3 12
2 4 18
2 3 6
2 5 20
3 5 15
0
and will then proceed to build a graph and install edges for it.
The input starts with a line that tells how many vertices the graph has. If there are five vertices, then those vertices have numbers 1, 2, 3, 4 and 5. In general, if there are n vertices, then they are numbered 1, …, n.
Following the first line are the edges, one per line. Each edge line has three integers on it.
The issue at hand is that readGraph still attempts to read 3 numbers even on the last line, where there is only one integer. It's supposed to stop once u is equal to the value 0 (last number in the example input). How can I fix this issue?
Link to Assignment for further reference -
http://www.cs.ecu.ed...ssn4/assn4.html
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
|
graph* readGraph()
{
int n_vertices, u, v, w;
cin >> n_vertices;
graph *g = new graph(n_vertices);
while(true)
{
scanf("%d %d %d", &u, &v, &w);
if(u == 0)
{
break;
}
insertEdge(u, v, w, g);
}
return g;
}
|