Graphs problem

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!

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using namespace 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?
Last edited on
Hello mysiarobin1987,

jonnin had talked about the same thing I found.

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.

Andy
@mysiarobin1987, What's up with those comments? They are clearly from another program! Where'd you get this code?

@HandyAndy, to try it out you can make a simple little graph, say 5 or 6 vertices labeled with letters, and connect them up arbitrarily.

      b      e
    /   \   /
   a      d
    \   /   \
      c      f

Then make the adjacency list:

a: b c             # a is joined to b and c
b: a d e
c: a d
d: b c e f
e: b d
f: d

The input for the program needs the lengths of the neighbors list, so:

a 2 b c
b 3 a d e
c 2 a d
d 4 b c e f
e 2 b d
f 1 d


There's a lot of errors in the program, though.
Last edited on
@tpb,

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.

Andy
Topic archived. No new replies allowed.