dbp
dbp

Reputation: 373

C++ Pass Values To fStream

I'm trying to pass values from the calculate function to the file function. But the "arrival" and "burst" values aren't being read properly from the "calculate" function. It is only returning the last values input through calculate.

float RR::calculate()
{
    cout << "Enter the number of processes: ";
    cin >> num_pr;
    vector<RR*> all_processes;
    for (int i=0; i<num_pr; i++)
    {
        cout << "What is the arrival time for process " << i << ": ";
        cin >> arrival_in;
        cout << "What is the burst time for process " << i << ": ";
        cin >> burst_in;
    }
    ...
    file (num_pr, arrival_in, burst_in, quantum, avg);
}
void RR::file(int processes, float arrival, float burst, float quantum, float avg)
{
    fstream newFile;
    newFile.open ("results.txt",ios::in | ios::out | ios::app);
    for (int i=0; i<processes; i++)
    {
        newFile << "Arrival time for process " << i << ": " << arrival << endl;
        newFile << "Burst time for process " << i << ": " << burst << endl;
    }
}

Here is my class definition:

class RR
{
public:
    RR();
    RR(float burst_set, float arrival_set);
    int num_pr, pos;
    float quantum, avg, burst_sum, time, burst_time, sleep_time, arrival_sum, total_avg, burst_in, arrival_in, calculate(), get_arrival(), get_burst(), get_avg(), get_initial_burst();
    void set_avg(float avg_set);
    void set_burst(float burst_time_set);
    void write_file(int processes, float arrival, float burst, float quantum, float avg);
private:
    float initial_burst, arrival_time, avg_time;
};

Upvotes: 1

Views: 91

Answers (1)

jrd1
jrd1

Reputation: 10716

This will surprise you, but your code is correct.

arrival and burst are class variables, which from calculated are only updated: They are never stored as shown here:

float RR::calculate()
{
    cout << "Enter the number of processes: ";
    cin >> num_pr;
    vector<RR*> all_processes;
    //your loop is correct, but it is only updating the values in `arrival_in` and `burst_in`
    //once this function finishes executing, those variables will be set at the
    //last value that was assigned to them.
    for (int i=0; i<num_pr; i++)
    {
        cout << "What is the arrival time for process " << i << ": ";
        cin >> arrival_in;
        cout << "What is the burst time for process " << i << ": ";
        cin >> burst_in;
    }
}

Hence, when you use them here:

file (num_pr, arrival_in, burst_in, quantum, avg);

Since those variables still contain the values the last time they were run, your results are unsurprising.

What this means is that your implementation only accesses the last value those variables had in them and not all of them (as desired). One way to store them is to use class vectors to store all the values. Then, later on in file() you can consult these vectors and write the correct values.

Example (note, this is just an example - it is in no way what you should use as there are additional problems with your understanding of C++):

float RR::calculate()
{
    cout << "Enter the number of processes: ";
    cin >> num_pr;
    vector<RR*> all_processes;
    //your loop is correct, but it is only updating the values in `arrival_in` and `burst_in`
    //once this function finishes executing, those variables will be set at the
    //last value that was assigned to them.
    for (int i=0; i<num_pr; i++)
    {
        cout << "What is the arrival time for process " << i << ": ";
        cin >> arrival_in;
        all_arrivals.push_back(arrival_in);
        cout << "What is the burst time for process " << i << ": ";
        cin >> burst_in;
        all_bursts.push_back(burst_in);
    }
}

Where all_bursts and all_arrivals are defined here:

class RR
{
public:
    RR();
    RR(float burst_set, float arrival_set);
    int num_pr, pos;
    float quantum, avg, burst_sum, time, burst_time, sleep_time, arrival_sum, total_avg, burst_in, arrival_in, calculate(), get_arrival(), get_burst(), get_avg(), get_initial_burst();
    void set_avg(float avg_set);
    void set_burst(float burst_time_set);
    void write_file(int processes, float arrival, float burst, float quantum, float avg);
private:
    float initial_burst, arrival_time, avg_time;
    std::vector<float> all_arrivals;//this should be private
    std::vector<float> all_bursts;//this should be private
};

Now, if you do so you can use these values (without passing them as a function parameter as the class functions by default, can access these variables anywhere):

void RR::file(int processes, float quantum, float avg)
{
    fstream newFile;
    newFile.open ("results.txt",ios::in | ios::out | ios::app);

    //since the number of `all_arrivals` and `all_bursts` are the same: i.e. `processes`, you can use `processes` here:
    //alternatively, you can use `all_arrivals.size()` or `all_bursts.size()` in lieu of `processes`
    for (int i=0; i<processes; i++)
    {
        newFile << "Arrival time for process " << i << ": " << all_arrivals[i] << endl;
        newFile << "Burst time for process " << i << ": " << all_bursts[i] << endl;
    }
}

Now, you can call the function like this:

file (num_pr, quantum, avg);

And, now your code should correctly access the values you previously entered.

P.S.: It would be a good idea to read up some more on classes and class variables in C++.

Upvotes: 1

Related Questions