0

I am writing a code for a graph in c++ but there is some problem. It is not working properly. Please help me what is the problem with it? Its code for a graph which can take inputs for graph from user and each edge in graph has specific weight. Here is the code:

#include <iostream>
#include <vector>

using namespace std;

struct edge {
    char src;
    char dest;
    int weight;
};

class Graph {
public:
     vector<edge> edges;
     int size,j=0;

     //Constructor
     Graph(int c) {
     size=c;
     }

     void graphDesign(char s,char d,int w) {
         edges[j].src=s;
         edges[j].dest=d;
         edges[j].weight=w;
         j++;
     }

    void printGraph() {
         for(int i=0; i<size; i++) {
            cout<<edges[i].src<<"->"<<edges[i].dest<<"  :  
               <<edges[i].weight<<endl;
         }
    }
 };


int main() {

    int e,i,w;
    char s,d;
    cout<<"Enter number of edges of graphs: ";
    cin>>e;
    Graph graph(e);
     for(i=0; i<e; i++) {
        cout<<"Enter source: ";
        cin>>s;
        cout<<"Enter destination: ";
        cin>>d;
        cout<<"Enter weight of the edge: ";
        cin>>w;

        graph.graphDesign(s,d,w);
    }

    graph.printGraph();

    return 0;
}
9
  • 1
    Advice -- Do not use extraneous variables like size to denote the number of entries in a container. Use what the container gives you, i.e. vector::size(). By using unnecessary variables, you run the risk of bugs occurring due to not updating that variable when the size changes. Your code is evidence of this -- what is edges.size()? I bet it isn't what you believe it is. Commented Dec 28, 2017 at 17:09
  • Can you elaborate on what "is not working properly" means? How is it not working? Commented Dec 28, 2017 at 17:12
  • When I enter the values for 1st time its get terminated while it should ask for values for several time and then it should print the all values. Commented Dec 28, 2017 at 17:15
  • @MuhammadAfaqRiaz -- There is nothing in your edges vector, but your code assumes there is at least one entry in the graphDesign function. That is wrong. Commented Dec 28, 2017 at 17:17
  • 1
    When edges is constructed, it is empty. You don't do anything to change that. Commented Dec 28, 2017 at 17:24

1 Answer 1

1

One issue is here:

void graphDesign(char s,char d,int w) {
         edges[j].src=s;
         edges[j].dest=d;
         edges[j].weight=w;
         j++;
     }

Since edges is an empty vector, accessing edges[j] is an illegal access.

You need to size the edges vector appropriately before you use it.

class Graph {
public:
     vector<edge> edges;

     //Constructor
     Graph(int c) : edges(c) {}

This creates a vector with c entries.

Also, do not use extraneous, unnecessary member variables such as size here. The vector class has a size() member function to tell you how many items are in the container.

Using extraneous variables like size runs the risk of bugs occurring due to having to update this variable anytime the vector changes size. Instead of trying to do this housekeeping yourself, use the size() function provided to you by std::vector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.