Hey, So I got this program all working except some of the output is weird pictures instead of letters. The first time it outputs it works fine, but after that it only partly works.

Here's the code:

#include "queue.cpp"
#include <iostream>
#include <fstream>
#include <iomanip>

/*******************************************
* class Priority                           *
* Takes input from file "queue1.dat"       *
* If that input is '*' calls service()     *
* If that input is '=' exits program       *
* Otherwise, Sends that input to queue.cpp *
* Author: Kimberlie Davis                  *
* Version: 2/5/09                          *
********************************************/

class Priority:public Queue
{
     protected:
              int priority;
              char character; 
     public:
            void service(Queue&, Queue&);
                 
};

/*
* method service
* While queue is not empty,
* Output contents of queue
*/
void
Priority::service(Queue &low, Queue &high)
{
    char lowOutput;
    char highOutput;
    cout << "Service: ";
    while(high.count != 0)
    {
          highOutput = high.takeAway();  
          cout << highOutput << " ";     
    }
    
    while(low.count != 0)
    {
          lowOutput = low.takeAway();
          cout << lowOutput << " ";
    }             
    cout << endl;   
}

/*
* main method
* Takes input from file
* Tests if file exists
* Tests if file is open
* While not end of file, tests files contents
* Calls correct method based on file contents
*/
int main()
{
    Priority priority;
    Queue highQueue;
    Queue lowQueue;
    ifstream partin;
    char charInput;
    int numInput;
    bool highQ;
    partin.open("queue1.dat");
    
    lowQueue.count = 0;
    
    if(!partin)
       cout << "Error: Unable to find file" << endl;
    if(partin.fail())  
        cout << "Error: Unable to open file!" << endl; 

    partin >> charInput >> numInput;
    while(partin)
    {
       if(charInput == '=')
           system("exit");
       else if(charInput == '*')
       {
            priority.service(lowQueue, highQueue);
       }
       else
       {
           if(numInput == 1)
           {
               highQueue.addToQueue(charInput); 
               highQ = true;
           }
           if(numInput != 1)
           {
                lowQueue.addToQueue(charInput);
                highQ = false;
           }
       }
           
        partin >> charInput >> numInput; 
    }
    
    partin.close(); 
    system("pause");
}
/****************************************
* A program that stores data to a queue *
* Tests if queue is full or empty       *
* Cant take data out of queue           *
* Author: Kimberlie Davis               *
* Version: 2/5/09                       *
*****************************************/
#include <iostream>

using namespace std;

class Queue
{
 private:
        int fill, remove;
 public:
        Queue(void);
        void initialize(void);
        char takeAway();
        bool full(void);
        bool empty(void);
        void addToQueue(char);
        int count;
        char element[8]; 
};

/*
* Constructor
* Initializes fill, remove and count
*/
Queue::Queue(void)
{
      fill = -1;
      remove = 0;
      count = 0;      
}

/*
* method initialize
* Reinitializes the variables
*/
void
Queue::initialize()
{ 
      fill = -1;
      remove = 0;
      count = 0;                     
}
/*
* Method takeAway
* Removes items from the queue
*/
char
Queue::takeAway()
{
      char value;  

      while(count != 0) 
      {
           value = element[remove];
           remove = (remove+1)%7;
           count--;
           fill--;
           return value;
      }       
         
}

/*
* method full
* Returns true if queue is full
*/
bool
Queue::full()
{
      if(count == 10)
      {
         cout << "Queue Overflow" << endl;
         return true;
      }
      else
         return false; 
}

/*
* method empty
* returns true if queue is empty
*/
bool
Queue::empty()
{
      if(count == 0)
      {
         cout << "Queue Empty" << endl;
         return true;
      }
      else
         return false;           
}

/*
* method addToQueue
* Tests if value is of high or low priority
* Adds the value to the correct queue
*/
void 
Queue::addToQueue(char character)
{
       if(count == 10)
         cout << "Queue overflow" << endl;
      else
      {
         fill = (fill+1)%7;
         element[fill] = character;
         count++;
      }                 
}

Recommended Answers

All 6 Replies

You are printing the ASCII value stored in the char lowOutput and highOutput!

void
Priority::service(Queue &low, Queue &high)
{
    char lowOutput;
    char highOutput;
    cout << "Service: ";
    while(high.count != 0)
    {
          highOutput = high.takeAway();  
          cout << highOutput << " ";     
    }
    
    while(low.count != 0)
    {
          lowOutput = low.takeAway();
          cout << lowOutput << " ";
    }             
    cout << endl;   
}

cast them to an int before printing them out or print their int value.
For example:
cout << (int)lowOutput;

It is suposed to print the char, not the int.

can you show what your output looks like?

Service: W R T
Service: E F J C H
Service:
Service: ╡ ▼ X R M
Press any key to continue . . .

That is with a little change to the code....

You are getting the pictures because you are printing out the ASCII value. Add some cout statements everytime you add something to the Queue to see what is getting added.

Nevermind, I went to my profs office and he helped me figure it out. It was just some errors with the fill variable in the queue class. Thanks!

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.