I\'ve got the following code. The program is supposed to read the input file ent
ID: 3650673 • Letter: I
Question
I've got the following code. The program is supposed to read the input file entered by the user. But everytime I try to enter the path of the input file, I keep getting the following error, "Enable To Open Input.txt File.Program Is Terminating Now...."
Here's the content of the input file.
5 1
0 1 3 -1 -1
1 0 3 6 -1
3 3 0 4 2
-1 6 4 0 5
-1 -1 2 5 0
The program is supposed to read the datas from the input file and print out the output.
Can anyone please correct this code where the program will indeed read the input file and won't give me the error. Thanks everybody.
Here's the code:
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
#include <iomainp>
#define MAX 10
using namespace std;
class prims
{
private : int cost[MAX][MAX], tree[MAX][MAX];
int n;
public : int readmatrix();
int spanningtree(int);
void display(int);
void init();
};
void prims :: init()
{
for( int i = 1; i <= n ; i++ )
{
for( int j = 1; j <= n; j++ )
{
tree[i][j] = -1;
if( i == j )
{
tree[i][j] = 0;
}
}
}
}
int prims :: readmatrix()
{
ifstream textfile;
int source;
cout<<"Enter The File Name : ";
cin>>filename;
string filename;
ifstream textfile;
textfile.open(filename.c_str());
if(textfile.is_open())
{
cout<<" Open Input.txt File For Reading."<<endl;
cout<<" Reading Data From Input.txt File.........."<<endl;
cout<<" Reading Number Of Vertices From File"<<endl;
textfile>>n; // Reading Number Of Vertices In The Graph
cout<<"Number Of Vertices In The Tree Is : "<<n<<endl;
cout<<" Reading The Source Vertice From File"<<endl;
textfile>>source;
while(!textfile.eof())
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
textfile>>cost[i][j];
if( i == j)
{
cost[i][j] = -1;
}
if(cost[i][j]== -1)
{
cost[i][j]=999;
}
}
}
}
cout<<"Process Of Reading Is Now Complete."<<endl;
}
else
{
cout<<"Enable To Open Input.txt File";
cout<<"Program Is Terminating Now......";
exit(1);
}
cout<<"Closing File Input.txt........."<<endl;
textfile.close();
system("pause");
return source;
/*int i = 0;
int j = 0;
cout << " Enter the number of vertices in the Graph : ";
cin >> n;
cout << " Enter the Cost matrix of the Graph ";
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
cin >> cost[i][j];
if( i == j)
{
cost[i][j] = -1;
}
if(cost[i][j]== -1)
{
cost[i][j]=999;
}
}
}*/
}
int prims :: spanningtree(int src)
{
int visited[MAX], d[MAX], parent[MAX];
int i, j, k, min, u, v, stcost;
for (i = 1; i <= n; i++)
{
d[i] = cost[src][i];
visited[i] = 0;
parent[i] = src;
}
visited[src] = 1;
stcost = 0;
k = 1;
for (i = 1; i < n; i++)
{
min=999;
for (j = 1; j <= n; j++)
{
if (!visited[j] && d[j] < min)
{
min = d[j];
u = j;
}
}
visited[u] = 1;
stcost = stcost + d[u];
//tree[k][1] = parent[u];
tree[parent[u]][u] = d[u];
tree[u][parent[u]] = d[u];
//tree[k++][2] = u;
for (v = 1; v <= n; v++)
if (!visited[v] && (cost[u][v] < d[v]))
{
d[v] = cost[u][v];
parent[v] = u;
}
}
return (stcost);
}
void prims :: display(int cost)
{
int i;
cout << " The Cost Matrix of the Mininum Spanning Tree is ";
for (i = 1; i <= n; i++)
{
for( int j = 1; j<= n; j++ )
{
cout << tree[i][j] << " ";
}
cout<< endl;
}
cout << " The Total cost of the Minimum Spanning Tree is : " << cost<<" ";
}
int main()
{
int source=0;
int treecost = 0;
prims pri;
source=pri.readmatrix();
pri.init();
cout<<" Source Vertice Is : "<<source;
/*cout << " Enter the Source : ";
cin >> source;*/
treecost = pri.spanningtree(source);
pri.display(treecost);
system("pause");
return 0;
}
Explanation / Answer
Try opening file with following code - string path; cin>>path; ifstream myfile (path); if (myfile.is_open())
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.