Reputation: 37
I am currently trying to create a program in c++ that will allow a user to fill in a text file to define variables for a graph, these variables define the distance between nodes in the graph. The format is as follows:
int numberOfNodes
//node 0, node 1 , node 2
float distance1, distance2, distance3 //node 0
float distance1, distance2, distance3 //node 1
float distance1, distance2, distance3 //node 2
(number of nodes being 3) which is why there is a 3x3 grid, which would specify the distance between each node.
for contect: filedata.txt
3
0 1 2
1 0 3
2 3 0
I understand that to use file input you use fstream, along with ifstream to select the file to open. What I dont understand is how to put this data into context.
How do i tell c++ that the first line will always be how many nodes there are in the list, then how would I tell c++ that anything under that first line is the data I want to fill into their own lists?
while (infile >> size)
{
cout <<"Total number of Verticies in Graph = "<< size << endl;
}
Upvotes: 0
Views: 46
Reputation: 1
With input like that:
3
First read the number of rows, and go into a for loop:
infile >> numrows;
for(int row = 0; i < numrows; ++i) {
}
The subsequent input can be read as
0 1 2
1 0 3
2 3 0
infile >> distance1 >> distance2 >> distance3;
inside of the loop.
Upvotes: 1