C++ 将值传递给 fStream
C++ Pass Values To 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;
};
这会让你大吃一惊,但你的代码是正确的。
arrival
和 burst
是类变量,从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_bursts
和all_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);
而且,现在您的代码应该可以正确访问您之前输入的值。
PS:最好在C++中阅读更多有关类和类变量的信息。
相关文章:
- 为什么可以将左值传递给"std::async",即使它引用了右值
- 当值传递给C++中的运算符重载函数时会发生什么
- 如何将非可变参数值传递给 fmt::format?
- 将 qml 项目值传递给 cpp 后如何与它们进行交互?
- NDK:将文本字段值传递给 c++ 字符串
- 如何将值传递给 lambda 函数
- 如何在C++中将std::string值传递给RegSetValueEx()
- 如何将 c ++ 变量的值传递给 hadoop HDFS 的 bash 系统命令?
- C++ 通过参数将值传递给 char 数组
- 如何将值传递给MFC消息映射函数on_command
- 使用JNI将长值传递给Java
- 如何在不使用 Argv 和 Argc 的情况下将 Sum 值传递给 C++ 程序
- C++ 将值传递给 fStream
- 如何将函数中的多个值传递给C++中的不同函数
- 将多个解析值传递给 addPositionalArgument 函数
- C++为什么将左值传递给move构造函数对模板有效
- 一种自动检测对象的方法,该方法通过值传递给c++中的函数
- 将值传递给main(int,char**)
- 将大于long类型的最大值的值传递给C中的fseek
- 将值传递给atexit