This is my assignment: (it is related to data structures: graphs)
Write an interactive program reads the name of cities and distances between them and provides the following functionality:
Given a city, print a list of all of the neighboring cities
Print a list of all cities and their neighbors
Given a city delete the city from the graph
Given a city, find the closest city
I am having a problem with the printNeighborCities function. I am getting these two errors: [Error] 'struct node' has no member named 'begin' [Error] 'struct node' has no member named 'end'. I am trying to use the STL for lists. Any help would be appreciated ! Thank you!
usingnamespace std;
#include <iostream>
#include <fstream>
#include<string>
#include<list>
#include<algorithm>
typedef list<string> adj_list;
typedef adj_list :: iterator adj_it;
struct node
{
string vertex;
adj_list adj;
};
typedef list<node> graph;
typedef graph :: iterator g_it;
void buildGraph(graph &);
void printGraph(graph);
void printNeighborCities(node, graph);
void deleteCity(graph);
void findClosestCity(graph);
int main()
{
// Variable declaration
int choice;
graph G;
buildGraph(G);
node city;
do
{
// Menu options to carry out specific operations for student list
cout << "Please Choose an Option from this Menu" << endl << endl;
cout << "1. Print a List of all Cities and Their Neighbors " << endl;
cout << "2. Input a City and Print a List of All of the Neighboring Cities " << endl;
cout << "3. Input a City and Delete it from the Graph " << endl;
cout << "4. Input a City and Find the Closest City " << endl;
cout << "5. Exit the Program" << endl;
cin >> choice; // Asks user to input their choice
// Switch menu that carries out the specific operations for student list based on the user's input
switch (choice)
{
case 1: printGraph(G); // Function call to retrieve and print a student's information from the student list
break;
case 2: printNeighborCities(city, G); // Function call to insert a student's information into the student list
break;
case 3: deleteCity(G); // Function call to delete a student's information from the student list
break;
case 4: findClosestCity(G); // Function call to print the contents of a student's record
break;
case 5: exit(0);
default: cout << "Please enter a value 1-5" << endl; // Default case in which user does not enter a number between 1 and 7, asks user to reenter value
}//end switch
} while (choice != 5);
return 0;
}
//build the graph
void buildGraph(graph & G)
{
ifstream infile;
infile.open("citiesgraph.dat");
node temp;
int num_adj_nodes;
string adj_vertex;
while (!infile.eof())
{
infile >>temp.vertex;
infile >> num_adj_nodes;
temp.adj.clear();
for(int i = 0; i < num_adj_nodes; i++)
{
infile >> adj_vertex;
temp.adj.push_back(adj_vertex);
}
G.push_back(temp);
}
}
//print the graph
void printGraph(graph G)
{
cout << endl;
cout << "The List of All Cities and Their Neighbors: " << endl;
for(g_it it = G.begin(); it != G.end();it++)
{
cout << it->vertex<<":" << " ";
for(adj_it it1 = it->adj.begin(); it1 != it->adj.end(); it1++)
{
cout << *it1 << " ";
}
cout <<endl;
}
}
void printNeighborCities(node city, graph G)
{
node temp;
cout << endl;
printGraph(G);
cout <<"Please Enter the City: "<<endl;
cin >> temp.vertex;
g_it it = find (city.begin(), city.end(), temp);
cout << "Last Name: " << it->vertex << " * " << endl;
cout << endl;
}
somewhere in there you have confused your node and your list<node>. Node clearly has no begin or end, but a list does, so you have simply used the wrong variable somewhere. Find the line it fusses about and change it to use the list, not the node.
I found one of them, its in
void printNeighborCities(node city, graph G)
{
node temp;
cout << endl;
printGraph(G);
cout <<"Please Enter the City: "<<endl;
cin >> temp.vertex;
g_it it = find (city.begin(), city.end(), temp); //should be G here?
Now my problem is with out the input file that you are using I can not do any further testing because I do not know what this input file looks like.
Please post the input file or at least a fair sample (5 to 10 lines maybe 20 lines) if it is large or even a link to download this file. Anything would help.
I did come up with one line that works for the input file.
My problem now appears to be g_it it = find (city.begin(), city.end(), temp);. This is OP code not what I changed it to. I think the g_it it part is the problem, but have not been able to solve it.