Hi. I'm having a problem accessing a function of an object which I put into a vector.
When I try to read the vector at lines 503-506 , I get an output of whatever number I set my setTau() function, instead of the correct number.
The beginning value for the varible tau is 10 when I enter my first "A" which simulates a new process entering the ready queue of an operating system. When I enter my second "A," and I enter a "2" when I am prompted "How long?" and then try to test what value the vector is holding by entering "S, r" I get an output of:
2
2
Which is incorrect. I should get an output of:
10
2
I am entering the values into the vector at lines 222, and 230.
Can someone take a look please? Thanks!
If you need more information from me, please let me know.
I have shown the output below the .cpp code. Lines 766 & 767 show the "2's" which are incorrect.
#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 ) { }
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;
static 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;
void screen();
void prompt();
void runningPrompt();
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' );
}
}
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 );
}
}
if( k[0] == 'L' ) // this is a test construct
{
PCB j;
//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.pop_back();
diskQueueCopy.pop_back();
}
else
{
cout << "The disk queue is empty. " << endl;
}
}
if( k[0] == 'P' )
{
if( !printerQueue.empty() )
{
readyQueue.push_back( process );
printerQueue.pop_back();
printerQueueCopy.pop_back();
}
else
{
cout << "The printer queue is empty. " << endl;
}
}
if( k[0] == 'C' )
{
if( !cdQueue.empty() )
{
readyQueue.push_back( process );
cdQueue.pop_back();
cdQueueCopy.pop_back();
}
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;
}
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;
}
float PCB::calculate() // This function is incorrect
{
float t;
t = (getTau() - getTime());
return 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::tau = 0;
float PCB::alpha = 0;
float PCB::time = 0;
/**
* main() begins here--
*/
int main()
{
computer c;
c.prompt();
c.runningPrompt();
return 0;
}
Welcome to the Sys Gen Process
Please enter the number of printers in the system (between 1 and 9)
3
Thank you. Please enter the number of disks in the system (between 1 and 9)
3
Please enter the number of CD/RW's in the system (between 1 and 9)
3
Please enter an initial burst tau:
10
Please enter a value for alpha:
0.5
Please enter how many cylinders for each disk:
Disk[1]:
4
Disk[2]:
4
Disk[3]:
4
You may enter a command or type 'exit' to exit the system.
Please enter command: A
New Process created.
Please enter command: A
Interrupt detected
How long?
2
Please enter command: S
Taking Snapshot.
Please select r, d, p, or c.
r
PID Filename Memstart R/W
1
2
Please enter command: S
Taking Snapshot.
Please select r, d, p, or c.
p
2
2
PID Filename Memstart FileLen R/W
0
Please enter command: