working with queue's

adding item to queue from text file

Page 1 of 1

11 Replies - 5748 Views - Last Post: 29 September 2008 - 11:43 PM Rate Topic: -----

#1 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

working with queue's

Post icon  Posted 28 September 2008 - 09:08 PM

Hey Everyone,
I am new to queue's. What I am doing is opening up a text file and reading the first line into the end of the queue. The problem that I am having is that I am only able to read the first line in the text file. When I add another item to the queue I need the second line in the text file to be read into the queue. Any help would be appreciated.

bool OpenFile(queue<string>&Line, string &n)		
{
	
	if (!rdFile)
		return false;   //Return false if queue.txt file is not found
	else  
	{
		rdFile.open(FILE_IN);	  //Opens file queue.txt
		getline(rdFile, n);		   //Reads entire line into n			
		return true;	  //Returns true back to main if the file is found
	}
}

void add2Line(queue<string>&Line, string &n)  //Adds an item to the end of the queue 
{	
	
		Line.push(n);
		
		cout << "Adding " << n << " to the back of the line...." << endl;	
		rdFile.close();
}



Is This A Good Question/Topic? 0
  • +

Replies To: working with queue's

#2 skaoth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: working with queue's

Posted 28 September 2008 - 10:27 PM

It would be helpful to know how you are doing this

Quote

When I add another item to the queue I need the second line in the text file to be read into the queue


One thing that I see is this line
rdFile.close();
inside of add2Line(). This doesn't make a whole lot of sense.
If you are trying to read from a file that contains multiple lines of input, you wouldn't want to close the file your reading from as your adding those items to your queue.
Was This Post Helpful? 0
  • +
  • -

#3 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: working with queue's

Posted 29 September 2008 - 06:21 AM

Quote

One thing that I see is this line
rdFile.close();
inside of add2Line(). This doesn't make a whole lot of sense.


I figured i will have to close the file when i am done reading from it. Is this not correct? I am only reading one line at a time. Every time I call the add2Line function I would like to read the next line in the text file. That is the part that I do not understand. I know how to read until the end of file, but not just one line at a time, and at different times.

Thanks in advance.
Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: working with queue's

Posted 29 September 2008 - 07:20 AM

In that case, you can have another function called closeFile which does the job of closing the file. and call it once you are done with reading all lines. when you do that inside add2Line. you are actually closing the file as soon as you add 1 line to queue, then when you try to add another line, it must be reopening and pushing first line again [as reopened file will be pointing to 1st line again.]
Was This Post Helpful? 0
  • +
  • -

#5 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: working with queue's

Posted 29 September 2008 - 08:10 AM

I modified my code a little. I am now seeing the items being added to the queue. But again I am only getting the first line from the text file. I need help understanding how to read the second line when the add2Line function is called again. When it is called a third time I need the add2Line function to read the third line in the text. So on and so forth.

bool OpenFile(queue<string>&Line)		
{
	int i = 0;
	if (!rdFile)
		return false;				//Return false if queue.txt file is not found
	else  
	{
		rdFile.open(FILE_IN);	  //Opens file queue.txt
		return true;			   //Returns true back to main if the file is found
	}
}
void add2Line(queue<string>&Line, string &n)  
{	
		
		getline(rdFile,n);		//Reads entire line into n	
		Line.push(n);			//Adds an item to the end of the queue 
		
		cout << "Adding " << n << " to the back of the line...." << endl;	
	
}



Thanks
Was This Post Helpful? 0
  • +
  • -

#6 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: working with queue's

Posted 29 September 2008 - 10:45 AM

I am not having any luck finding out how to do this. Please help if you can.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: working with queue's

Posted 29 September 2008 - 11:37 AM

The code you're offering is incomplete, so no one can really offer constructive help.

Why don't you look at how to read data from files ( http://www.cplusplus...tream/open.html ) and see how far you get.
Was This Post Helpful? 0
  • +
  • -

#8 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: working with queue's

Posted 29 September 2008 - 01:57 PM

Hi baavgai. I looked over the article. Thanks. I will try explaining again. Basically I just need help reading from a text file. I know how to open up the file and also how to read the first line of text from the file. I do not know how to get the other 14 lines of text from the file. I just don't know if i should loop, iterate, etc... to get the rest of the text read into my queue.

Here is the rest of my code. Thanks for looking it over.

//Main
#include <iostream>
#include <queue>
using namespace std;


#include "Functions.h"		  //Makes functions available to main


int main()
{
int choice = 0;
bool fileFound;				 
queue <string> theQ;
string n;
int i = 0;


do
{	
	fileFound = OpenFile(theQ);	
	choice = ShowMenu();   //ShowMenu will return the choice back to main so 
			  //that the switch statement can use it.
	switch(choice)			
	{
	
	case 1:			//Adds a name to the queue
		add2Line(theQ,n);
		break;	
	case 2:			//Serves the first item in the queue
		serve(theQ);
		break;
	case 3:
		howMany(theQ);	//How many items are in the queue
		break;
	case 4:
		ShowFLine(theQ);	//Displays the first item in the queue
		break;
	case 5:
		ShowBLine(theQ);	//Displays the last item in the queue
		break;
	case 6:
		clrLine(theQ,n);	//Clears the queue
		break;
	default:
		cout << "Goodbye." <<endl;
		exit(1);
	}
}while(choice !=7);
cout << "Goodbye." << endl;


cin.get();
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#9 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: working with queue's

Posted 29 September 2008 - 03:57 PM

Come on, man, show a little initiative! A quick google will get all kinds for file reading examples. Let's try this:
void readFile(const char *fileName) {
	string line;
	ifstream infile(fileName);
	while (!infile.eof()) {
		getline(infile, line);
		cout << line << endl; 	
	}
	infile.close();
}



Yep, that works. What does it take to add those lines to a queue?

void readFile(const char *fileName, queue<string> &q) {
	string line;
	ifstream infile(fileName);
	while (!infile.eof()) {
		getline(infile, line);
		q.push(line);
	}
	infile.close();
}



That's about it.


The other things you need to do? Built into the class:
#include <iostream>
#include <fstream>
#include <queue>

using namespace std;

void add2Line(queue<string> &q, string &n) {
	q.push(n);
	cout << "Adding " << n << " to the back of the line...." << endl;    
}

void readFile(const char *fileName, queue<string> &q) {
	string line;
	ifstream infile(fileName);
	while (!infile.eof()) {
		getline(infile, line);
		add2Line(q, line);
	}
	infile.close();
}


int main() {
	queue <string> theQ;
	
	readFile("test.cpp", theQ);
	cout<< "Size: " << theQ.size() << endl;
	if (theQ.empty()) {
		cout<< "First item: None" << endl;
	} else {
		cout<< "First item: " << theQ.front() << endl;
		cout<< "Last item: " << theQ.back() << endl;
	}
	return 0;
}



If that don't help, um, good luck.
Was This Post Helpful? 0
  • +
  • -

#10 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: working with queue's

Posted 29 September 2008 - 04:25 PM

I did google it! I didn't want the damn code just some help!
Was This Post Helpful? 0
  • +
  • -

#11 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: working with queue's

Posted 29 September 2008 - 10:21 PM

Nice to hear that you don't want ready to run code and you want to try your own code.

What I found in your main code is that you are opening file every time when you loop [first statement after do]. This is one of the main causes of first line addition every time.
just take that outside [before] do...while and it will solve your problem. Just make sure that you close file after while loop.

One more thing, if you want to make program even more flexible then I would suggest you to check for both whether the file is open and reached the end in add2line, if this is the case then reopen the file in function. And you can close file on exit option and clear queue option. But this is not compulsory code to make your program run. It will work if you do thing I have said before.

I hope this will help you. :)

This post has been edited by AmitTheInfinity: 29 September 2008 - 11:54 PM

Was This Post Helpful? 0
  • +
  • -

#12 wartech   User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 203
  • Joined: 16-October 06

Re: working with queue's

Posted 29 September 2008 - 11:43 PM

Thanks AmitTheInfinity!
After working all day on this (yes i am still in the beginning stages of c++)
I came up with this. I am finally able to read each line as needed (per spec). You are right once the openFile() was out of the loop it solved the problem.

I will clean it up tomorrow but it works like it should. Finally.... :^:

Here is my code:
Main.cpp
#include <iostream>
#include <queue>
#include <string>
using namespace std;


#include "Functions.h"		 //Makes functions available to main


int main()
{
int choice = 0;				 
queue <string> theQ;
bool fileFound;
string n;
string hdr;

fileFound=OpenFile(theQ,hdr);

do
{	
	choice = ShowMenu();	//ShowMenu will return the choice back to main so that 
							//the switch statement can use/ it.
	switch(choice)			
	{
	
	case 1:	
		
		add2Line(theQ,n,hdr);		//Adds a name to the queue
		break;	
	case 2:					//Serves the first item in the queue
		serve(theQ);
		break;
	case 3:
		howMany(theQ);		//How many items are in the queue
		break;
	case 4:
		ShowFLine(theQ);	//Displays the first item in the queue
		break;
	case 5:
		ShowBLine(theQ);	//Displays the last item in the queue
		break;
	case 6:
		clrLine(theQ,n);	//Clears the queue
		break;
	default:
		cout << "Goodbye." <<endl;
		exit(1);
	}
}while(choice !=7);

cout << "Goodbye." << endl;


cin.get();
return 0;
}



//Functions.h
int ShowMenu();									
bool OpenFile(queue <string> &Line, string &hdr);	 //Opens file and reads from the queue.txt file
void add2Line(queue<string>&Line, string &n, string &hdr); //Adds an item to the end of the queue 
void serve(queue<string>&Line);			//Serves the first item in the queue
void howMany(queue<string>&Line);		//Displays the amount of items in queue
void ShowFLine(queue<string> &Line);	//Displays the first item in the queue
void ShowBLine(queue<string> &Line);	//Displays the last item in the queue
void clrLine(queue<string>&Line, string &n); //Clears the queue by popping off each item until empty.



Function.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <queue>
using namespace std;

#define FILE_IN "queue.txt"
ifstream rdFile(FILE_IN);			//ifstream is declared globally so all functions
									//have access to it.

int ShowMenu()						//Dispalys Menu Options and allows
									//user to enter a choice.	
{
	int choice = 0;							
	
	cout <<"\n 1. Add item to the que.";
	cout <<"\n 2. Serve out first item in the queue.";
	cout <<"\n 3. How many items are in the queue.";
	cout <<"\n 4. What/Who is at the front of the queue?";
	cout <<"\n 5. What/Who is at the back of the queue?";
	cout <<"\n 6. Clear the queue.";
	cout <<"\n 7. Exit the program.";
	cout << "\n";
	cout <<"\n Pick a menu item ==>"; 
	cin >> choice;
	cin.ignore();
	return choice;					//Choice will be returned to main for use in the
									//switch statement
}


bool OpenFile(queue<string>&Line, string &hdr)		
{
	 
	 string n;
	if(!rdFile) 
	{ 
		return false;
	}
	else
	{
		getline(rdFile,hdr);	   //Gets first line from text file and stores in hdr
		n = hdr;				   //it will be used each time a flight is added to the queue.
		return true;
	}
   
}
void add2Line(queue<string>&Line, string &n, string &hdr)  
{		
		cout << hdr << endl;
		getline(rdFile,n);
		Line.push(n);			  //Adds an item to the end of the queue 
		cout <<  n << "	has been added to the back of the line." << endl;	
		
}
	  

void serve(queue<string>&Line)	 //Serves the first item in the queue
{
	if (Line.empty() == true)
	{
		cout << "There are currently no flights in line. " << endl;
		cin.get();
		exit(1);
	}
	cout << "Now serving " << Line.front() << " ....";
	
	Line.pop();							//Removes first item from the queue

}
void howMany(queue<string>&Line)		//Gives the amount of items in queue
{
	cout << "There are " << Line.size() <<  " flights in the line now." << endl;
}

void ShowFLine(queue<string> &Line)		//Displays the first item in the queue
{
	if (Line.empty() == true)
	{
		cout << "There are currently no flights in line. " << endl;
		cin.get();
		exit(1);
	}
	cout << Line.front() << " is at the front of the line." << endl;

}
void ShowBLine(queue<string> &Line)		//Displays the last item in the queue
{
	if (Line.empty() == true)
	{
		cout << "There are currently no flights in line." << endl;
		cin.get();
		exit(1);
	}
	cout << Line.back() << " is at the back of the line." << endl;

}
void clrLine(queue<string> &Line, string &n) //Clears the queue by popping off 
{											 //each item till empty.	
	while(!Line.empty()) 
	{	 
		Line.pop(); 

	}   
	cout << "Clearing the queue...." << endl;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1