2013-11-04 133 views
1

我试图将计算函数的值传递给文件函数。但是“到达”和“突发”值没有从“计算”功能中正确读取。它只返回通过计算输入的最后一个值。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; 
}; 
+0

你可以发布你的类定义吗?从目前的情况来看,这有点难以分辨。 – jrd1

+1

我已将类定义添加到原始文章。 – optimus203

+0

我想你可能需要移动文件(num_pr,arrival_in,burst_in,quantum,avg);在上面的for循环里面。 –

回答

1

这会令你感到惊讶,但你的代码是正确的。

arrivalburst是类变量,从calculated只更新:他们永远不会存储如下所示:

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

因此,当你在这里使用它们:

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

由于这些变量仍包含上次运行时的值,因此您的结果并不令人惊讶。

这是什么意思是你的实现只访问这些变量在它们中的最后一个值,而不是所有的(根据需要)。存储它们的一种方法是使用类vectors来存储的所有值。然后,在以后的file()中,您可以查阅这些向量并编写正确的值。

例子(注意,这只是一个例子 - 它是没有办法,因为有你的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); 
    } 
} 

all_burstsall_arrivals被定义如下:现在

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

,如果你这样做,你可以使用这些值(不用将它们作为函数参数的默认类的功能,可以在任何地方访问这些变量):

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

,你可以调用函数是这样的:

file (num_pr, quantum, avg); 

而且,现在您的代码应预先输入正确访问值你。

P.S .:在C++中阅读更多关于类和类变量的知识将是一个好主意。

+0

非常好的解决方案,额外2载体。像魅力一样工作。感谢您的时间和帮助! – optimus203

+0

@ optimus203:没问题。很高兴为您服务。而且,谢谢你! :) – jrd1