I'm having trouble displying the contents of a queue. When I pop the queue here:

            for( int i = 0; i < diskQueue.size() + 1; i++ )
            {
                cout << left << setw(0) << "--d" << i+1 << left << setw(4) <<  " "
                << left << setw(14) << diskQueue.front().getFilename() << left << setw(14) <<      diskQueue.front().getMemoryStart()
                << left << setw(14) << diskQueue.front().getFileLength() << diskQueue.front().getRW() << endl;
                diskQueue.pop();
            }

I have to empty the queue, which is not what I want to do. Is there a way to print the contents of this queue without emptying it?

Here is the complete code:

#include <iostream>
#include <string>
#include <limits>
#include <queue>
#include <iomanip>

using namespace std;

/**
 * Global Variables
 */
int printer;
int disk;
int cd;
int num;
string k;
int pmem, plen;
int dmem, dlen;
int cmem, clen;
string dname, pname, cname, drw, prw, crw;



/**
 * Sys Gen class prompts for system information
 */
class sysGen
{
public:
    void prompt() const;
};


class PCB
{
public:
    PCB() : pid( 0 ), filename ( "InitialString" ), memoryLocation ( 0 ), rw ( "InitialString" ) { }
    void setPid( int p );
    int getPid() const;
    int incrementPid();
    int decrementPid();
    void setFilename( string f );
    string getFilename() const;
    void setMemoryStart( int m );
    int getMemoryStart() const;
    void setFileLength( int l );
    int getFileLength() const;
    void setRW( string r );
    string getRW() const;
    void screen();
    void runningPrompt() const;
    queue<PCB> readyQueue;
    queue<PCB> diskQueue;
    queue<PCB> printerQueue;
    queue<PCB> cdQueue;
private:
    int pid;
    string filename;
    int memoryLocation;
    string rw;
    int fileLen;
};


void sysGen::prompt() const
{
    cout << "Welcome to the Sys Gen Process"<<endl;
    cout<<"Please enter the number of printers in the system (between 1 and 9)"<<endl;
    while( ! ( cin >> printer ) )
    {
        cout << "That was not an integer...\nTry again: ";
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }
    while(printer<1 || printer>9)
    {
        cout<<"Sorry, that is an invalid value, please re-enter amount of printers:"<<endl;
        while( ! ( cin >> printer ) )
        {
            cout << "That was not an integer...\nTry again: ";
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        }

    }
    cout<<"Thank you. Please enter the number of disks in the system (between 1 and 9)"<<endl;
    while( ! ( cin >> disk ) )
    {
        cout << "That was not an integer...\nTry again: ";
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }
    while(disk<1 || disk>9)
    {
        cout<<"Sorry, that is an invalid value, please re-enter amount of disks:"<<endl;
        while( ! ( cin >> disk ) )
        {
            cout << "That was not an integer...\nTry again: ";
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        }

    }
    cout<<"Please enter the number of CD/RW's in the system (between 1 and 9)"<<endl;
    while( ! ( cin >> cd ) )
    {
        cout << "That was not an integer...\nTry again: ";
        cin.clear();
        cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    }
    while(cd<1 || cd>9)
    {
        cout<<"Sorry, that is an invalid value, please re-enter amount of CD/RW's:"<<endl;
        while( ! ( cin >> cd ) )
        {
            cout << "That was not an integer...\nTry again: ";
            cin.clear();
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
        }

    }

}

/**
* This runningPrompt Function receives input from the keyboard regarding processes
*/
void PCB::runningPrompt() const
{
    cout << "You may enter a command or type 'exit' to exit the system." << endl;
    PCB initialProcess;
    while (k != "exit")
    {
        cout << "Please enter command: ";
        cin >> k;
        if ( k[0] == 'A' )
        {
            cout << "New Process created." << endl;
            initialProcess.incrementPid();
            cout << "Pid is " << initialProcess.getPid() << endl;
            initialProcess.readyQueue.push( initialProcess );
        }
        if( k[0] == 't' )
        {
            cout << "Process terminating." << endl;
            cout << "Process has entered CPU." << endl;
        }
        if( k[0] == 'S' )
        {
            initialProcess.screen();
        }
        if( k[1] == '1' )
            num = 1;
        if( k[1] == '2' )
            num = 2;
        if( k[1] == '3' )
            num = 3;
        if( k[1] == '4' )
            num = 4;
        if( k[1] == '5' )
            num = 5;
        if( k[1] == '6' )
            num = 6;
        if( k[1] == '7' )
            num = 7;
        if( k[1] == '8' )
            num = 8;
        if( k[1] == '9' )
            num = 9;
        if ( k[0] == 'd' )
        {
            if( !initialProcess.readyQueue.empty() )
            {
                cout << "This is a system call requesting Disk " << num << endl;
                cout << "What is the filename? Please enter it: " << endl;
                cin >> dname;
                initialProcess.setFilename( dname );
                cout << "What is the starting location in memory? Please enter it:" << endl;
                while( ( ! ( cin >> dmem ) ) || dmem < 0  )
                {
                    cout << "That was not a positive integer...\nTry again: ";
                    cin.clear();
                    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                }
                initialProcess.setMemoryStart( dmem );
                cout << "Please enter the length of your file in an integer value:" << endl;
                while( ( ! ( cin >> dlen ) ) || dlen < 0 )
                {
                    cout << "That was not positive integer...\nTry again: ";
                    cin.clear();
                    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
                }
                initialProcess.setFileLength( dlen );
                cout << "Please enter r for read or w for write" << endl;
                cin >> drw;
                while( ! ( drw == "r" || drw == "w" ) )
                {
                    cout << "Please enter a r or a w" << endl;
                    cin >> drw;
                }
                initialProcess.setRW( drw );
                initialProcess.diskQueue.push( initialProcess );
                initialProcess.decrementPid();
                initialProcess.readyQueue.pop();
            }
            else
            {
                cout << "There are no processes in the ready queue." << endl;
            }
        }
        if ( k[0] == 'p' )
        {
            cout << "This is a system call requesting printer: " << num << endl;
            cout << "What is the filename? Please enter it: " << endl;
            cin >> pname;
            initialProcess.setFilename( pname );
            cout << "What is the starting location in memory? Please enter it:" << endl;
            while( ( ! ( cin >> pmem ) ) || pmem < 0  )
            {
                cout << "That was not a positive integer...\nTry again: ";
                cin.clear();
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            }
            initialProcess.setMemoryStart( pmem );
            cout << "Please enter the length of your file in an integer value:" << endl;
            while( ( ! ( cin >> plen ) ) || plen < 0 )
            {
                cout << "That was not positive integer...\nTry again: ";
                cin.clear();
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            }
            initialProcess.setFileLength( plen );
            cout << "Please enter r for read or w for write" << endl;
            cin >> prw;
            while( ! ( prw == "r" || prw == "w" ) )
            {
                cout << "Please enter a r or a w" << endl;
                cin >> prw;
            }
            initialProcess.setRW( prw );
            initialProcess.printerQueue.push( initialProcess );
        }
        if ( k[0] == 'c' )
        {
            cout << "This is a system call requesting CD/RW: " << num << endl;
            cout << "What is the filename? Please enter it: " << endl;
            cin >> cname;
            initialProcess.setFilename( cname );
            cout << "What is the starting location in memory? Please enter it:" << endl;
            while( ( ! ( cin >> cmem ) ) || cmem < 0  )
            {
                cout << "That was not a positive integer...\nTry again: ";
                cin.clear();
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            }
            initialProcess.setMemoryStart( cmem );
            cout << "Please enter the length of your file in an integer value:" << endl;
            while( ( ! ( cin >> clen ) ) || clen < 0 )
            {
                cout << "That was not positive integer...\nTry again: ";
                cin.clear();
                cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            }
            initialProcess.setFileLength( clen );
            cout << "Please enter r for read or w for write" << endl;
            cin >> crw;
            while( ! ( crw == "r" || crw == "w" ) )
            {
                cout << "Please enter a r or a w" << endl;
                cin >> crw;
            }
            initialProcess.setRW( crw );
            initialProcess.cdQueue.push( initialProcess );
        }
        if( k[0] == 'D' )
        {
            if( !initialProcess.diskQueue.empty() )
            {
                for( int i = 0; i < initialProcess.diskQueue.size(); i++ )
                {
                     initialProcess.diskQueue.pop();
                }
            }
            else
            {
               cout << "The disk queue is empty. " << endl; 
            }
        }
    }
}

void PCB::setPid(int p)
{
    pid = p;
}

int PCB::getPid() const
{
    return pid;
}

int PCB::incrementPid()
{
    pid++;
}

void PCB::setFilename( string f )
{
    filename = f;
}

string PCB::getFilename() const
{
    return filename;
}

void PCB::screen()
{
    cout << "Taking Snapshot." << endl;
    cout << "Please select r, d, p, or c." << endl;
    string rpdc;
    cin >> rpdc;
    while ( ! ( rpdc[0] == 'r' || rpdc[0] == 'p' || rpdc[0] == 'd' || rpdc[0] == 'c' ) )
    {
        cout << "Please enter r, p, d, or c. Thank you." << endl;
        cout << "Enter selection" << endl;
        cin >> rpdc;
    }
    if( rpdc[0] == 'r' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << " R/W" << endl; 
        if( !readyQueue.empty() )
        {
            for( int i = 0; i < readyQueue.size(); i++ )
            {
                cout << left << setw(0) << "" << i+1 << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
    if( rpdc[0] == 'p' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(12) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !printerQueue.empty() )
        {
            for( int i = 0; i < printerQueue.size(); i++ )
            {
                cout << left << setw(0) << "--p" << i+1 << left << setw(4) <<  " "
                << left << setw(14) << printerQueue.front().getFilename() << left << setw(14) <<  printerQueue.front().getMemoryStart()
                << left << setw(14) << printerQueue.front().getFileLength() << printerQueue.front().getRW() << endl;
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
    if( rpdc[0] == 'd' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(12) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !diskQueue.empty() )
        {
            for( int i = 0; i < diskQueue.size() + 1; i++ )
            {
                cout << left << setw(0) << "--d" << i+1 << left << setw(4) <<  " "
                << left << setw(14) << diskQueue.front().getFilename() << left << setw(14) <<      diskQueue.front().getMemoryStart()
                << left << setw(14) << diskQueue.front().getFileLength() << diskQueue.front().getRW() << endl;
                diskQueue.pop();
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
    if( rpdc[0] == 'c' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(12) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !cdQueue.empty() )
        {
            for( int i = 0; i < cdQueue.size(); i++ )
            {
                cout << left << setw(0) << "--c" << i+1 << left << setw(4) <<  " "
                << left << setw(14) << cdQueue.front().getFilename() << left << setw(14) <<  cdQueue.front().getMemoryStart()
                << left << setw(14) << cdQueue.front().getFileLength() << cdQueue.front().getRW() << endl;
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
}

int PCB::decrementPid()
{
    pid--;
}


void PCB::setMemoryStart( int m )
{
    memoryLocation = m;
}

int PCB::getMemoryStart() const
{
    return memoryLocation;
}


void PCB::setFileLength( int l )
{
    fileLen = l;
}

int PCB::getFileLength() const
{
    return fileLen;
}


void PCB::setRW( string r )
{
    rw = r;
}

string PCB::getRW() const
{
    return rw;
}


int main()
{
    sysGen s;
    PCB p;
    s.prompt();
    p.runningPrompt();

    return 0;
}

Thank you.

Recommended Answers

All 2 Replies

The short answer is no. If you need to examine all elements of the container without removing them, a queue is not what you need. Consider a deque instead.

Thank you Moschops.

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.