我試圖將計算函數的值傳遞給文件函數。但是「到達」和「突發」值沒有從「計算」功能中正確讀取。它只返回通過計算輸入的最後一個值。C++將值傳遞給fStream
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;
}
}
這裏是我的類定義:
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;
};
你可以發佈你的類定義嗎?從目前的情況來看,這有點難以分辨。 – jrd1
我已將類定義添加到原始文章。 – optimus203
我想你可能需要移動文件(num_pr,arrival_in,burst_in,quantum,avg);在上面的for循環裏面。 –