Hi. Why am I getting different output when I call the calculate() function?

The output on line 184 gives 1.0001, while the output from line 433 gives 1.

Thanks.

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

using namespace std;

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

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


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

    }

}

/**
* 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' )
        {
            cout << "New Process created." << endl;
            process.incrementPid();
            cout << "Pid is " << process.getPid() << endl;
            readyQueue.push_back( process );
            int kk;
            cout << "please enter the number of cylinders: " << endl;
            cin >> kk;
            process.setCylin( kk );
            float k1;
            cout << "Please enter the history parameter. " << endl;
            cout << "Example: 0.9 " << endl;
            cin >> k1;
            process.setAlpha( k1 );
            float k2;
            cout << "Please enter the burst time in milliseconds. " << endl;
            cout << "Example: enter 5 for 5 milliseconds, " << endl;
            cin >> k2;
            process.setTau( k2 );
            cout << "Number: " << process.calculate() << endl;
        }
        if( k[0] == 't' )
        {
            if( !readyQueue.empty() )
            {
                cout << "Process terminating." << endl;
                readyQueue.clear();
                process.decrementPid();
            }
            else
            {
                cout << "Ready vector 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 );
                    diskQueue.push_back( process );
                    diskQueueCopy.push_back( process );
                    process.decrementPid();
                    readyQueue.pop_back();
                }
                else
                {
                    cout << "There are no processes in the ready vector." << 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 );  
                    printerQueue.push_back( process );
                    printerQueueCopy.push_back( process );
                    process.decrementPid();
                    readyQueue.pop_back();
                }
                else
                {
                    cout << "There are no processes in the ready vector." << 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 );
                    cdQueue.push_back( process );
                    cdQueueCopy.push_back( process );
                    process.decrementPid();
                    readyQueue.pop_back();
                }
                else
                {
                    cout << "There are no processes in the ready vector." << endl;
                }
            }
            else
            {
                cout << "Please enter a valid CD/RW number. " << endl;
            }
        }          
        if( k[0] == 'D' )
        {
            if( !diskQueue.empty() )
            {
                     readyQueue.push_back( process );
                     process.incrementPid();
                     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 );
                     process.incrementPid();
                     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 );
                     process.incrementPid();
                     cdQueue.pop_back();
                     cdQueueCopy.pop_back();
            }
            else
            {
               cout << "The CD/RW vector 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) << "" << i+1 << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << p1.calculate() << 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(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !printerQueueCopy.empty() )
        {
            for( int i = 0; i < printerQueue.size(); i++ )
            {
                cout << setw(0) << "--p" << i+1 << setw(5) <<  " "
                << setw(14) << printerQueueCopy.front().getFilename() << setw(14) <<  printerQueueCopy.front().getMemoryStart()
                << setw(14) << printerQueueCopy.front().getFileLength() << " - " << endl;
                printerQueueCopy.clear();
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
        printerQueueCopy = printerQueue;
    }
    if( rpdc[0] == 'd' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !diskQueueCopy.empty() )
        {
            for( int i = 0; i < diskQueue.size(); i++ )
            {
                cout << setw(0) << "--d" << i+1 << setw(6) <<  " "
                << setw(14) << diskQueueCopy.front().getFilename() << setw(14) <<  diskQueueCopy.front().getMemoryStart()
                << setw(14) << diskQueueCopy.front().getFileLength() << diskQueueCopy.front().getRW() << endl;
                diskQueueCopy.clear();
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
        diskQueueCopy = diskQueue;
    }
    if( rpdc[0] == 'c' )
    {
        cout << left << setw(9) << "PID" << left << setw(14) << "Filename"
        << left << setw(14) << "Memstart" << left << setw(14) << "FileLen" << " R/W" << endl;
        if( !cdQueueCopy.empty() )
        {
            for( int i = 0; i < cdQueue.size(); i++ )
            {
                cout << left << setw(0) << "--c" << i+1 << left << setw(5) <<  " "
                << left << setw(14) << cdQueueCopy.front().getFilename() << left << setw(14) <<  cdQueueCopy.front().getMemoryStart()
                << left << setw(14) << cdQueueCopy.front().getFileLength() << cdQueueCopy.front().getRW() << endl;
                cdQueueCopy.clear();
            }
        }
        else
        {
                cout << left << setw(0) << "" << "0" << left << setw(4) <<  " "
                << left << setw(14) << " " << left << setw(14) <<  " "
                << left << setw(14) << " " << " " << endl;
        }
        cdQueueCopy = cdQueue;
    }
}

/**
 * Member function definititions follow:
 */
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;
}

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

void PCB::setTime( int t )
{
     procTime = t;
}


int PCB::getTime() const
{
    return procTime;
}

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()
{
    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()
{
      return (getAlpha()*getTau())+((1-getAlpha()*getTau()));
}

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

    return 0;
}

Recommended Answers

All 12 Replies

Which lines again? We see the line numbers HERE, on the forum. Do yourself a favour by doing US a favour and make sure the line numbers you quote are the line numbers we see.

Have you investigated this? Perhaps your function calculate could output the value of each variable it is working with so you could see if they're different? Also, are you aware that floating point calculations are inherently imprecise? http://floating-point-gui.de/ Frankly, in terms of casual floating point use, 1.0001 and 1 ARE the same.

Thanks Moschops.

Sorry about that. It looks like it's now lines 175 and 424. See if that makes sense.

I think it's a problem I'm having trying to output using different objects of class PCB, but I'm not sure.

I did some testing and it looks like I am right. I have two classes here: PCB and Computer. In class PCB I make a calculation using an object of class PCB. It gives me the correct output.

If I create a new instance of class PCB within a member function of class computer and try to access the same information, I get another answer which is incorrect. This type to thing has happened to me before, so I know it's something I'm not understanding.

What input do you give the program?

3
3
3
A
6
73.897
42800

I then get an output of 1.01416
I then enter

S
r

I then get an output of 1 for the function.

Thanks.

So where's the 1.0001?

I got the 1.0001 when I entered different values.

Right. So what were THOSE values?

I'm not exactly sure, but this gives 1.0004:

3
3
3
A
6
0.9
1500
...

3
3
3
A
6
0.9
400
...

Will give 1.00001

Please take a look at this small program. It illustrates what I think the issue is:

#include <iostream>
    using namespace std;

    class IntCell
    {
          public:
            IntCell() : storedValue( 0 ) {}
            int read() const;
            void write( int x );


          private:
            int storedValue;
    };

    class second
    {
          public:
            void show();
    };

    int IntCell::read() const
    {
        return storedValue;
    }

    void IntCell::write( int x )
    {
         storedValue = x;
    }

    void second::show()
    {
         IntCell k;
         cout << "Here is the incorrect value of stored value: " << k.read() << endl;
    }

    int main()
    {
        IntCell t;
        t.write( 16 );
        cout << "Here is the value for t.read(): " << t.read() << endl;
        second s;
        s.show();
        return 0;
    }

I'm giving storedValue a value of 16 at line 41.
When I try to access that value with an instance of class IntCell in class second, it fails. This can be seen in the output from the program which is 0.

I'm pretty sure the same thing is happening in the original program I posted.

I am trying to access the private values in class PCB from an instance of class PCB in a member function of class computer.

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.