Thread: Outfile function not properly calling another one

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    73

    Outfile function not properly calling another one

    Working on a program where the program will read in an adjacency matrix and create a .dot file for GraphViz using that information. All class code is below.

    I am only trying to create three files, and all three are being properly created and filled with the header text, but it's not filling in the graph information for some reason. I'm assuming a flaw in the way the CreateEdges function is coded, but I don't see what's wrong with it. Can anyone help?

    Code:
    // graph.h
    #include <iostream>
    #include <fstream>
    #include "string"
    #pragma once
    
    
    class graph
    {
    private:
            bool adjacency[10][10];
            int VertCount;
            bool isSymmetrical;
    public:
        graph();
        ~graph();
        graph(bool adj[][10], int count);
        void setSymmetry();
        void CreateEdges(std::ofstream &outfile);
        void GraphVizOut(std::string filename);
    };
    
    // graph.cpp
    #include "graph.h"
    #include <fstream>
    #include "string"
    
    
    graph::graph()
    {
    }
    
    
    graph::~graph()
    {
    }
    
    
    graph::graph(bool adj[][10], int count)
    {
        VertCount = count;
        
        for (int x = 0; x < count; x++)
        {
            for (int y = 0; y < count; y++)
            {
                adjacency[x][y] = adj[x][y];
            }
        }
    
        setSymmetry();
        return;
    }
    
    
    void graph::setSymmetry()
    {
        for (int x = 0; x < VertCount; x++)
        {
            for (int y = x; y < VertCount; y++)
            {
                if (adjacency[x][y] != adjacency[y][x])
                    isSymmetrical = false;
                    return;
            }
        }
    
    
        isSymmetrical = true;
        return;
    }
    
    
    void graph::CreateEdges(std::ofstream &outfile)
    {
    
    
        // If matrix is symmetrical, graph is undirected
        if (isSymmetrical)
        {
            for (int x = 0; x < VertCount; x++)
            {
                for (int y = x; y < VertCount; y++)
                {
                    if (adjacency[x][y])
                        outfile << x << " " << "--" << y << "\n";
                }
            }
        }
        // If matrix is not symmetrical, graph is directed
        else
        {
            for (int x = 0; x < VertCount; x++)
            {
                for (int y = 0; y < VertCount; y++)
                {
                    if (adjacency[x][y])
                        outfile << x << "--" << y << "\n";
                }
            }
        }
    
    
        return;
    }
    
    
    void graph::GraphVizOut(std::string filename)
    {
        std::ofstream VizOut;
        VizOut.open(filename);
        VizOut << "// Trey Brumley \n";
        VizOut << "// File created by C++ GraphGen Project \n";
        VizOut << "graph G { \n";
        CreateEdges(VizOut);  // This isn't working here, for whatever reason.  Everything on either side is, but not this.
        VizOut << "} \n";
        VizOut.close();
    
    
        return;
    }
    
    // main.cpp
    #include <fstream>
    #include "graph.h"
    #include "string"
    using namespace std;
    
    
    int main()
    {
    	ifstream infile;
    	infile.open("input.txt");
    	char c;
    	int i, count = 0;
    	string fileName1 = "graph0.dot", fileName2 = "graph1.dot", fileName3 = "graph2.dot";
    
    
    	while (infile>>c)
    	{
    		bool adjacencies[10][10];
    
    
    		infile >> i;
    
    
    		// Loops looking for 1s and 0s to "store" in adjacency matrix
    		for (int x = 0; x < i; x++)
    		{
    			for (int y = 0; y < i; y++)
    			{
    				infile.get(c);
    				while (c != '1' && c != '0')
    					infile.get(c);
    				if (c == '1')
    					adjacencies[x][y] = true;
    				else if (c == '0')
    					adjacencies[x][y] = false;
    			}
    		}
    
    
    		for (int x = 0; x < i; x++)
    		{
    			for (int y = 0; y < i; y++)
    			{
    				cout << adjacencies[x][y] << " ";
    			}
    			cout << endl;
    		}
    
    
    		cout << endl << endl;
    
    
    		graph G(adjacencies, i);
    		if (count == 0)
    			G.GraphVizOut(fileName1);
    		else if (count == 1)
    			G.GraphVizOut(fileName2);
    		else if (count == 2)
    			G.GraphVizOut(fileName3);
    
    
    		count++;
    	}
    
    
    	system("pause");
    	return 0;
    }
    Last edited by Trey Brumley; 05-06-2015 at 07:18 AM. Reason: Commenting in some information

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    lines 62-64 will not do what you think based on the code alignment

    In the future - it would help if you provide a sample input, desired output and actual output for problems of this type
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    With any complex and/or long code, one will most certainly want to run the code under a debugger to figure out what's wrong. This is much easier than trying to stare at the code to figure out what's wrong. To that end, when posting code, consider:
    - Post the entire code. Don't leave anything out. It should compile.
    - If possible, pose it in a single code tag to make it easy to copy and paste.
    - Provide an input and the desired output for that input to make sure it's fast and simple to get to the problem.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. delete function does not work properly...
    By fkmk in forum C Programming
    Replies: 5
    Last Post: 01-18-2014, 09:43 PM
  2. How to properly use a function?
    By Who in forum C Programming
    Replies: 2
    Last Post: 02-01-2013, 03:25 AM
  3. advise on how to properly use pow function
    By libchk in forum C Programming
    Replies: 12
    Last Post: 08-31-2011, 12:50 AM
  4. How to properly return MySQL results to a calling function?
    By Effenberg0x0 in forum C Programming
    Replies: 3
    Last Post: 06-09-2011, 03:42 AM
  5. How to properly use a C function?
    By John_L in forum C Programming
    Replies: 4
    Last Post: 05-30-2008, 02:01 AM