If I prompt the user for n number of printers, is there a way of creating n number of vectors on the fly as opposed to creating vectors and putting them in your class structure at the start?

I have to create as many printer queues as a user specifys. I am using vectors for this. I am planning to put them into a class structure.

Thanks.

Edited 2 Years Ago by smitsky

Thanks! Let me see if I can figure out how to implement that.

I tried a few things and they're not working.

Let's say I have this: vector< vector<PCB> > printers;

How do I create n vectors out of this?

int input;
cin >> input;
Where would I use input to create the vectors?

Thanks.

Thank you. I tried it in my code and it didn't cause an error.

How to I write to and read from each vector now? What is the syntax I'll need?

The above code could be more succinctly written as vector<vector<PCB> > printers(input);

Edited 2 Years Ago by nullptr

I see, thanks! I'll try that.

So, let's say I want to push back a value to printer 1 as opposed to printer 2 if input is 2 and 2 vectors are created? What is the syntax to differentiate between each vector?

Edited 2 Years Ago by smitsky

Here's a simple example:

#include <iostream>
#include <vector>
#include <string>

class MyClass {
public:
    MyClass(std::string arg = "default") : m_arg(arg) {}
    void display() const {
        std::cout << m_arg << std::endl;
    }

private:
    std::string m_arg;
};

int main()
{
    size_t n = 0;
    // assuming valid input for brevity
    std::cout << "Enter the number of vectors (at least 3): ";
    std::cin >> n;
    std::vector<std::vector<MyClass> > classes_vector(n);

    MyClass mc("This is first");
    MyClass mca("This is also first");
    MyClass mc1("This is second");
    MyClass mc2("This is third");

    classes_vector[0].push_back(mc);
    classes_vector[0].push_back(mca);
    classes_vector[1].push_back(mc1);
    classes_vector[2].push_back(mc2);

    for (auto cl : classes_vector[0])
    {
        cl.display();
    }

    for (auto cl : classes_vector[2])
    {
        cl.display();
    }
    // simple flush
    std::cin.ignore(90, '\n');
    std::cin.get();

    return 0;
}

Thanks.

I tried it but am having the following issue:

When I try to access private member tau in the PCB class with the getTau() member function through readyQueues[0].getTau() at line 241, I get an error message stating:

[Error] 'class std::vector<PCB>' has no member named 'getTau'

Here is the code:

#include <iostream>
#include <string>
#include <limits>
#include <vector>
#include <iomanip>
#include <cstdlib>

using namespace std;

/**
 * Global Variables
 */
string k;
int pmem, plen;
int dmem, dlen;
int cmem, clen;
string dname, pname, cname, drw, prw, crw;
int g_pid = 0;
float tempTau;

/**
 * Process Control Block Class
 */
class PCB
{
public:
    PCB() : pid (0), filename ( "InitialString" ), memoryLocation ( 0 ), rw ( "InitialString" ), fileLen ( 0 ), procTime( 0 ), tau( 0 ) { }
    void setPid( int p );
    int getPid() const;
    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 setTime( float t );
    float getTime() const;
    void setTau( float t );
    float getTau() const;
    void setAlpha( float a );
    float getAlpha() const;
    void setCylin( int cc );
    int getCylin() const;
    //float calculate();
    float calculateTau();
    void setCpuTime( float c );
    float getCpuTime() const;
private:
    int pid;
    string filename;
    int memoryLocation;
    string rw;
    int fileLen;
    int procTime;
    int cylinder;
    float tau;
    static float alpha;
    float cpuTime;
    static float time;
};

/*
 * Computer structure
 */
struct computer
{
        int printer;
        int disk;
        int cd;
        int num;
        vector<PCB> readyQueue;
        vector<PCB> diskQueue;
        vector<PCB> diskQueueCopy;
        vector<PCB> printerQueue;
        vector<PCB> printerQueueCopy;
        vector<PCB> cdQueue;
        vector<PCB> cdQueueCopy;
        vector<int> time;
        vector<PCB> vCylinder;
        vector< vector<PCB> > printerQueues;
        void screen();
        void prompt();
        void runningPrompt();
        void calculate();
        int & findMin( vector<int> & v );
        int findPos( vector<int> & vv );
        void removeElem();
};

/**
 * Sys Gen Process begins here. Prompt the user for input.
 */
void computer::prompt()
{
    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' );
        }

    }
    vector<vector<PCB> > printerQueues( printer ); // 3 vectors
    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' );
        }
    }
    cout << "Please enter an initial burst tau: " << endl;
    PCB t;
    string inputTau;
    cin >> inputTau;
    tempTau = atof(inputTau.c_str());
    while( tempTau <= 0 )
    {
        cout << "Please enter a value such as 0.9" << endl;
        cin >> inputTau;
        tempTau = atof(inputTau.c_str());
    }
    t.setTau( tempTau );
    cout << "Please enter a value for alpha: " << endl;
    string inputAlpha;
    float tempAlpha;
    cin >> inputAlpha;
    tempAlpha = atof(inputAlpha.c_str());
    while( tempAlpha <= 0 )
    {
        cout << "Please enter a value such as 0.9" << endl;
        cin >> inputAlpha;
        tempAlpha = atof(inputAlpha.c_str());
    }
    t.setAlpha( tempAlpha );
    cout << "Please enter how many cylinders for each disk: " << endl;
    string inputCylinder;
    int tempCylinder;
    for( int i = 1; i < disk + 1; i++ )
    {
        cout << "Disk[" << i << "]:" << endl;
        cin >> inputCylinder;
        tempCylinder = atof(inputCylinder.c_str());
        while( tempCylinder <= 0 )
        {
            cout << "Please enter an integer value: " << endl;
            cin >> inputCylinder;
            tempCylinder = atof(inputCylinder.c_str());
        }
    t.setCylin( tempCylinder );
    vCylinder.push_back( t );
    }
}

/**
* This runningPrompt Function receives input from the keyboard regarding processes
*/
void computer::runningPrompt()
{
    cout << "You may enter a command or type 'exit' to exit the system." << endl;
    PCB process;
    while (k != "exit")
    {
        cout << "Please enter command: ";
        cin >> k;
        if( k[0] == 'A' )
        {
            if( !readyQueue.empty() )
            {
                cout << "Interrupt detected" << endl;
                cout << "How long? " << endl;
                string inputTime;
                cin >> inputTime;
                int timeTemp;
                timeTemp = atof( inputTime.c_str());
                while( timeTemp <= 0 )
                {
                    cout << "Please enter an integer value: " << endl;
                    cin >> inputTime;
                    timeTemp = atof(inputTime.c_str());
                }
                g_pid++;
                process.setPid( g_pid );
                process.setTau( timeTemp );
                readyQueue.push_back( process );
            }
            else
            {
                cout << "New Process created." << endl;
                g_pid++;
                process.setPid( g_pid );
                process.setTau( tempTau );
                readyQueue.push_back( process );
            }
            calculate();
        }
        if( k[0] == 'L' ) // this is a test construct
        {
            PCB j;
            printerQueues[0].push_back( j );
            cout << printerQueues[0].getTau() << endl;
            //cout << j.getTau() << endl;
            //cout << j.getAlpha() << endl;
            //j.calculate();
            //cout << j.getTau() << endl;
            //cout << readyQueue[0].calculate() << endl;
            //cout << readyQueue[1].calculate() << endl;
        }
        if( k[0] == 't' )
        {
            if( !readyQueue.empty() )
            {
                cout << "Process terminating." << endl;
                readyQueue.erase( readyQueue.begin() );
            }
            else
            {
                cout << "Ready queue is empty." << endl;
            }
        }
        if( k[0] == 'S' )
        {
            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( disk >= num )
            {
                if( !readyQueue.empty() )
                {
                    cout << "This is a system call requesting Disk " << num << endl;
                    cout << "What is the filename? Please enter it: " << endl;
                    cin >> dname;
                    process.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' );
                    }
                    process.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' );
                    }
                    process.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;
                    }
                    process.setRW( drw );
                    process.setPid( readyQueue.front().getPid() );
                    diskQueue.push_back( process );
                    readyQueue.erase( readyQueue.begin());
                }
                else
                {
                    cout << "There are no processes in the ready queue." << endl;
                }
            }
            else
            {
                cout << "Please enter a valid disk number" << endl;
            }
        }
        if ( k[0] == 'p' )
        {
            if( printer >= num )
            {
                if( !readyQueue.empty() )
                {
                    cout << "This is a system call requesting printer: " << num << endl;
                    cout << "What is the filename? Please enter it: " << endl;
                    cin >> pname;
                    process.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' );
                    }
                    process.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' );
                    }
                    process.setFileLength( plen );
                    process.setPid( readyQueue.front().getPid() );
                    printerQueue.push_back( process );
                    readyQueue.erase( readyQueue.begin());
                }
                else
                {
                    cout << "There are no processes in the ready queue." << endl;
                }

            }
            else
            {
                cout << "Please enter a valid printer number" << endl;
            }
        }
        if ( k[0] == 'c' )
        {
            if( cd >= num )
            {
                if( !readyQueue.empty() )
                {
                    cout << "This is a system call requesting CD/RW: " << num << endl;
                    cout << "What is the filename? Please enter it: " << endl;
                    cin >> cname;
                    process.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' );
                    }
                    process.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' );
                    }
                    process.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;
                    }
                    process.setRW( crw );
                    process.setPid( readyQueue.front().getPid() );
                    cdQueue.push_back( process );
                     readyQueue.erase( readyQueue.begin() );
                }
                else
                {
                    cout << "There are no processes in the ready queue." << endl;
                }
            }
            else
            {
                cout << "Please enter a valid CD/RW number. " << endl;
            }
        }
        if( k[0] == 'D' )
        {
            if( !diskQueue.empty() )
            {
                     readyQueue.push_back( process );
                      diskQueue.erase( diskQueue.begin() );

            }
            else
            {
               cout << "The disk queue is empty. " << endl;
            }
        }
        if( k[0] == 'P' )
        {
            if( !printerQueue.empty() )
            {
                     readyQueue.push_back( process );
                      printerQueue.erase( printerQueue.begin() );
            }
            else
            {
               cout << "The printer queue is empty. " << endl;
            }
        }
        if( k[0] == 'C' )
        {
            if( !cdQueue.empty() )
            {
                     readyQueue.push_back( process );
                      cdQueue.erase( cdQueue.begin() );
            }
            else
            {
               cout << "The CD/RW queue is empty. " << endl;
            }
        }
    }
}

/**
 * User can get a "Screen Shot" but typing "S" at command line.
 */
void computer::screen()
{
    PCB p1;
    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) << "" << readyQueue[i].getPid() << 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' )
    {
        PCB l;
        //cout << l.calculate() << endl;
        //cout << l.calculateTau() << endl;
        /*for( int i = 0; i < vCylinder.size(); i++ )
        {
            cout << vCylinder[i].getCylin() << endl;
        } // This works <--------------------------------------------------*/
        for( int i = 0; i < readyQueue.size(); i++ )
        {
            cout << readyQueue[i].getTau() << endl;
        }
        calculate();
        cout << readyQueue[0].getTau() << endl;
        cout << left << setw(8) << "PID" << setw(14) << "Filename"
        << setw(14) << "Memstart" << setw(14) << "FileLen" << " R/W" << endl;
        if( !printerQueue.empty() )
        {
            for( int i = 0; i < printerQueue.size(); i++ )
            {
                cout << setw(0) << "p" << printerQueue[i].getPid() << setw(6) <<  " "
                << setw(14) << printerQueue[i].getFilename() << setw(14) << printerQueue[i].getMemoryStart()
                << setw(14) << printerQueue[i].getFileLength() << " - " << 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(8) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !diskQueue.empty() )
        {
            for( int i = 0; i < diskQueue.size(); i++ )
            {
                cout << setw(0) << "d" << diskQueue[i].getPid() << setw(6) <<  " "
                << setw(14) << diskQueue[i].getFilename() << setw(14) <<  diskQueue[i].getMemoryStart()
                << setw(14) << diskQueue[i].getFileLength() << diskQueue[i].getRW() << endl;
            }
        }
        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(8) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !cdQueue.empty() )
        {
            for( int i = 0; i < cdQueue.size(); i++ )
            {
                cout << left << setw(0) << "c" << cdQueue[i].getPid() << left << setw(5) <<  " "
                << left << setw(14) << cdQueue[i].getFilename() << left << setw(14) <<  cdQueue[i].getMemoryStart()
                << left << setw(14) << cdQueue[i].getFileLength() << cdQueue[i].getRW() << endl;
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
    }
}

/**
 * Member function definititions follows:
 */
void PCB::setPid(int p)
{
    pid = p;
}

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

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

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

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;
}

void PCB::setTime( float t )
{
     time = t;
}


float PCB::getTime() const
{
    return time;
}

int & computer::findMin( vector<int> & v )
{
    int minIndex = 0;

    for( int i = 1; i < v.size( ); i++ )
        if( v[ minIndex ] > v[ i ] )
            minIndex = i;
    return v[ minIndex ];
}

int computer::findPos( vector<int> & v )
{
    int minIndex = 0;

    for( int i = 1; i < v.size( ); i++ )
        if( v[ minIndex ] > v[ i ] )
            minIndex = i;
    return minIndex;
}

void computer::removeElem()
{
    time.erase( time.begin() + ( findPos( time ) ) );
}

void PCB::setTau( float t )
{
     tau = t;
}

float PCB::getTau() const
{
    return tau;
}


void PCB::setAlpha( float a )
{
     alpha = a;
}

float PCB::getAlpha() const
{
       return alpha;
}

void PCB::setCylin( int cc )
{
     cylinder = cc;
}

int PCB::getCylin() const
{
    return cylinder;
}

void computer::calculate()
{
      float t;
      t = ( readyQueue[0].getTau() - readyQueue[1].getTau());
      readyQueue[0].setTau( t );
}

float PCB::calculateTau()
{
    float f;
    f = ( (getAlpha() * getTau()) + (getAlpha() * getTime()) );
    return f;
}

void PCB::setCpuTime( float c )
{
     cpuTime = c;
}

float PCB::getCpuTime() const
{
      return cpuTime;
}

float PCB::alpha = 0;
float PCB::time = 0;

/**
* main() begins here--
*/
int main()
{
    computer c;
    c.prompt();
    c.runningPrompt();

    return 0;
}

Edited 2 Years Ago by smitsky

readyQueues[0] is the vector, so you need to specify which element (PCB instance) of the vector you wish to access.
e.g.

printerQueues[0][0].getTau();
printerQueues[0].at(0).getTau();
printerQueues[0].front().getTau();
printerQueues[0].back().getTau();

As an aside, assuming you are using ASCII character encoding (or something else with digits in order, such as UTF-8), you can simplify the section in lines 265-282 as:

if (isdigit(k[1]) && k[1] != '0')
    num = k[1] - '1';

It isn't a huge improvement, but it should make it more readable. You'll need to #include <cctype> at the start of the source file. If you actually do want it to set it when zero, simply remove the second part of the conditional cluase.

Thanks nullptr and Schol-R-LEA.

nullptr, I have tried all 4 of you suggestions above and they all cause a segmentation fault. They all compile however.

Thanks.

Is the issue the way I am trying to fill the vectors at line 115?

This question has already been answered. Start a new discussion instead.