Queue

Last Edited By Krjb Donovan
Last Updated: Mar 12, 2014 02:46 PM GMT

Question

Hello Zlatko

I am writing a queue program which gives priority to female. The program will read infile.txt

[infile.txt] M Alan1 F Mary1 M Alan2 F Mary2 M Alan3

Qns: How do I read in the file & sort my queue as follows:

[OUTPUT to screen] P (Mary1, F) P (Mary2, F) P (Alan1, M) P (Alan2, M) P (Alan3, M)


[CODE]

  1. include <iostream>
  2. include <fstream>
  3. include <cstring>

using namespace std;

const int MAX = 30;

class Person {

      friend ostream& operator << (ostream&, Person&);
      public:
                      Person();       // Perfrom 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;


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();

}

Answer

Hello Steve.

I recommend you use this loop for your input

   while (!infile.eof())
   {
       string sex;
       infile >> sex;
       
       string name;
       infile >> name;
       // Create the person
       Person p(
              (char*)name.c_str(), 
              *(sex.c_str()) // Gets just the first character
       ); 
   }

The input loop you sent me makes no sense.

You should always try to use the C++ string and getline tools for reading from files instead of using character arrays.

Also, I recommend that you make the char[] in the Person constructor a const, like this: Person(const char[] name, char gender); So that we do not need the (char*) cast when passing the name from String::c_str to the constructor.

It would be even better to store the name as a std::string in Person, instead of a char array. Using string is almost always a better and safer idea. Then your Person constructor would look like this: Person(const string& inName, char gender) : name(inName) { sex = gender; }

Now that you can actually read the infile.txt and create a person, I'll leave it up to you to continue working and debugging the queue algorithms.

Advertisement

©2024 eLuminary LLC. All rights reserved.