Creating a program that will read in an adjacency matrix, create a graph with it and the vertex count, and then use the necessary information to write a .dot file for GraphViz.
Every time I try to run the program, the last-accessed string fileNameX variable is always corrupted (stack around the variable fileName[x] is corrupt). I don't know what I'm doing wrong, and why it's doing this, as I've never had this error come up before. Any ideas?
On a lesser note, when I run the program, it does not create or open the files in question in the directory, but I assume that's part of this problem. If not, feel free to correct me.
The main function and the print function of the graph class is below.
#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.get(c);
i = (int)c;
// 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;
}
}
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;
}
// Notes paths between vertices with "X -- Y" notation.
void graph::CreateEdges(std::ofstream &outfile)
{
// If matrix is symmetrical, graph is undirected
if (isSymmetrical == true)
{
for (int x = 0; x < VertCount; x++)
{
for (int y = x; y < VertCount; y++)
{
if (adjacency[x][y] == true)
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] == true)
outfile << x << " " << "--" << y << "\n";
}
}
}
return;
}
// Creates the file, writes header information, and then calls CreateEdges above necessary info write to it
void graph::GraphVizOut(std::string filename)
{
std::ofstream VizOut;
VizOut.open(filename);
VizOut << "// Trey Brumley \n";
VizOut << "// File created by C++ BST Project \n";
VizOut << "graph G { \n";
CreateEdges(VizOut);
VizOut << "} \n";
VizOut.close();
return;
}