Hi..
I suppose to have a programe to read a data from a file and enqueu it to the Queue.
The programme works like a Priority queue whether the ladies queue need to be inserted to queue first before the gentleman...

But i think i need to try first how to actually read the data from the file and put it into Queue before i need to implement the priority ...

Here is the function prototype

#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

const int MAX = 20;

class Person
{
	friend ostream& operator << (ostream&, Person&);
	
	public:
			Person();	// Do nothing
			Person(char [],char);
			Person(const Person&);
			
			char getSex () const;
			Person& operator*= (const Person&);
			
	private:
			char name [MAX];
			char sex;
};

struct Node;
typedef Node* NodePtr;

struct Node
{
	Person p;
	NodePtr next;
};

class Queue
{
	friend ostream& operator << (ostream&, Queue&);
	
	public:
			Queue();	// head&tail
			
			void enqueue (const Person&);	// insert person to Q
			Person dequeue ();	//delete person from Q
			
			int getSize();	//get size of infile
			bool isEmpty();	// if Q isEmpty
			
	private:
			NodePtr head,tail;
			static int size;
			
};

int Queue::size = 0;

Here is what i try to do but it can't works

Person::Person (char name[],char sex)
{
	int i;
	while (i<MAX)
	{
		if(name[i] != '\0')
			this->name[i]=name[i];
		else
			break;
		i++;
	}
	
	this-> sex = sex;	 	 
}

void Queue::enqueue (const Person& name)
{
	NodePtr temp = new Node;
	temp -> p = name;
	temp -> next = NULL;
	
	if (tail == NULL)
		head = temp;
	else
		tail -> next = temp;
		
	tail = temp;
	size++;
}

int main ()
{
	char name [MAX];
	char sex;
	fstream infile;
	Queue q;
	
	q.dequeue ();
	
	infile.open ("infile.txt", ios::in);	
	if (!infile.good())
	{
		cout <<" opened for reading failed" << endl;
		cout << "End of task" << endl;
		exit (1);
	}
	else
	{
		cout << " successfully opened for reading" << endl;
	}
	
	while (!infile.eof())
	{
		for (int i=0;i<MAX;i++)
		{
			infile << name[i];
			q.enqueue (name[i]);
		}
	}
		
	infile.close();	
}

Appreciate any of your help
Thank you

Recommended Answers

All 7 Replies

Hi..
I suppose to have a programe to read a data from a file and enqueu it to the Queue.
The programme works like a Priority queue whether the ladies queue need to be inserted to queue first before the gentleman...

But i think i need to try first how to actually read the data from the file and put it into Queue before i need to implement the priority ...

Very good choice

Here is what i try to do but it can't works

Why can't it?

Wouldn't it be easier if you told us what makes you think it can't work?

Very good choice

Why can't it?

Wouldn't it be easier if you told us what makes you think it can't work?

Oops sorry,i forgot to mention the error..
So the compiler stated this error
- no matching function for call to 'Queue::enqueue(char[20])
- candidates are: void Queue::enqueue(cosnt Person&)

Oops sorry,i forgot to mention the error..
So the compiler stated this error
- no matching function for call to 'Queue::enqueue(char[20])
- candidates are: void Queue::enqueue(cosnt Person&)

You passed a character array to enqueue() . But enqueue() is defined to take a Person

You passed a character array to enqueue() . But enqueue() is defined to take a Person

I try to use p.name
But name in Person class is private and i can't access it...

What's are you going to do with?

int Queue::size = 0;

Looks like no function matching when program compiled.

I suffed up thepost ... See below.
(Keyboard shortcuts. lol!)

Hi..
...SNIP...
Appreciate any of your help
Thank you

You don't want to be using insertion operators on infile.
infile << name; will output(insert) a character, name, to infile. You want to be reading(extracting) stuff in.
infile >> name;
I'm no whiz-genius but I think that'll grab a line of text from infile and stuff it into the 'name' variable. I noticed you included <string.h> but I didn't see any declaration of string variables.
Perhaps instead of, char name[MAX]; you could simply use
string name;

I have only used a text file with a single integer value on each line and the code I was using to add each integer to a stack looked like this:

while(in_file >> i)	//read file contents into Stack structure
{
        myStack.Push(i);
}

I didn't test for EOF. *shrugs*
I might be wrong, but I anticipate your text file might look like:

Bruce M
Trish F
Jack M
etc. etc.

So the first thing I'd try, and I haven't yet, sorry, would be:
1. Read from infile (with the >> operator)
2. Use the variables to create the Person object.
3. Enqueue.

Person *temp;

while(!in_file.eof())
{
       infile >> name >> sex;
       temp = new Person(name, sex);
       q.enqueue(*temp);
}

(Remember your Person constructor is shonky also. Set variable i to a value before comparing it to MAX, which you might not have to do if you use string variables.)
You might change also your text file to look like this:

<?xml version="1">
<Person>
<Name>Bruce</Name>
<Sex>M</Sex>
</Person>
<Person>
<Name>Trish</Name>
<Sex>F</Sex>
</Person>
<Person>
<Name>Jack</Name>
<Sex>M</Sex>
</Person>
</xml>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.