0

Hello all,

I have a text file set up as follows.

1 1 1
2 2 2

The program below is not reading the first line. The code below will only read 2 2 2. What am I missing here?


main.cpp

#include <iostream>
#include <limits>
#include <cmath>
#include <cstdlib>
#include <cstddef>
#include <fstream>
#include <cctype>
#include <ctime>
#include <string>
#include <iomanip>
#include <queue>
using namespace std;#include "utility.h"
#include "Graph.h"
int main()
{
	Graph g;

	g.load_graph("GraphDemo1.txt");


}

Graph.h

#ifndef KEY_H
#define KEY_H

#include "utility.h"

class Graph
{
public:
	Graph();
	void load_graph(string fileName);


private:
	int graph_size;
	int matrix[25][25];
};

Graph::Graph()
{

}

void Graph::load_graph(std::string fileName)
{
	ifstream fin(fileName.c_str());
	if (!fin.is_open())
	{
		cout << endl << "Unable to open input file" << fileName	<< endl;
	}

	graph_size = 0;
	while(getline(fin,fileName))
	{
		for(int i = 0; i < 3; i++)
		{
			fin >> matrix[graph_size][i];
			cout << "graph: " << graph_size << " i: " << i << "   " << matrix[graph_size][i] << endl;
			
		}
		//cout << graph_size << endl;
		graph_size++;

	}
}


#endif //KEY_H
2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by coolbeanbob
0

I changed lines 32-43 to a "do-while loop" instead of a "while loop" and it works.

Initially, I thought the "fin" in line 23 might be tripping the first line, and making me skip over it. But this logic does not make sense, because it would seem like I would miss the second line with the code below, but I don't. So I am not sure why the "do-while loop" works?

do
	{
		for(int i = 0; i < 3; i++)
		{
			fin >> matrix[graph_size][i];
			cout << "graph: " << graph_size << " i: " << i << "   " << matrix[graph_size][i] << endl;
			
		}
		//cout << graph_size << endl;
		graph_size++;

	}while(getline(fin, fileName));
0

What does line 32 do?

Doesn't "getline(fin,fileName)" return true if there is another line, and false if we're at the end of the file?

0

I've been playing around with this. Now, instead of getline(), I am using good().

My text file now looks like this

A 12 7
A 45 3
A 13 6
D
A 19 8
D
D

The program is stopping on line 4. What do I need to change so the program pulls in the entire file?

#include "utility.h"
#include "message.h"
#include "pQueue.h"

int main()
{	int test = 0;
	string fileName = "lab10pqDemo.txt";

	// Test file availabilty
	ifstream fin(fileName.c_str());
	if (!fin.is_open())
	{
		cout << endl << "Unable to open input file" << fileName	<< endl;
	}

	// Load file into pQueue
	string status;
	int data;
	int priority;
	pQueue<message> messageQueue;
	do
	{
		fin >> status;
		fin >> data;
		fin >> priority;	

		cout << "test: " << test << " status: " << status << " data: " << data << " priority: " << priority << endl;
/*	
		if(status == "A")
		{
			message m;
			m.create_message(status, data, priority);
			messageQueue.push(m);
		}
*/	

	}while(fin.good());
	//}while(getline(fin, fileName));



}
0

Figured out a solution...

#include "utility.h"
#include "message.h"
#include "pQueue.h"

int main()
{	int test = 0;
	string fileName = "lab10pqDemo.txt";

	// Test file availabilty
	ifstream fin(fileName.c_str());
	if (!fin.is_open())
	{
		cout << endl << "Unable to open input file" << fileName	<< endl;
	}

	// Load file into pQueue
	string status;
	int data;
	int priority;
	pQueue<message> messageQueue;
	do
	{
		fin >> status;

		if(status == "A")
		{		
			fin >> data;
			fin >> priority;	
		}
		else
		{
			data = NULL;
			priority = NULL;
		}

		cout << "test: " << test << " status: " << status << " data: " << data << " priority: " << priority << endl;
/*	
		if(status == "A")
		{
			message m;
			m.create_message(status, data, priority);
			messageQueue.push(m);
		}
*/	

	}while(fin.good());
	//}while(getline(fin, fileName));



}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.