我目前正在编写一个程序,该程序读取文本文件,执行计算,然后将结果返回到新的文本文件。到目前为止,程序返回了文本文件的基本详细信息,但是,Im努力将执行计算的两个函数的结果解析为writetoFile函数(顾名思义,将所有输出写入新的文本文件) 。
这是2个计算函数以及writetoFile函数
//function 1
double robotComplexity(std::vector<std::string>& lines) //takes in the out.txt file
{
std::string output = lines[0]; //so far only reads the first line of the output file.
std::string input = output.substr(18,1);
double complexity = 20 * atoi(input.c_str());
if(complexity > 100) {
complexity = 100;
}
cout << "The Robot Complexity is: " << complexity << endl;
return complexity;
}
//function 2
double robotVariability(std::vector<std::string>& lines)
{
std::string output = lines[0]; //so far only reads the first line of the output file.
std::string input = output.substr(15,2);
double variability;
variability = 5 + atoi(input.c_str());
cout << "The Robot Variability is: " << variability << endl;
return variability;
}
//function to write to file.
void writeFile(std::string const& outputFile, std::vector<std::string> const& lines,
double unitComplexity, double unitVariability)
{
std::ofstream file(outputFile);
for(std::string const& line: lines)
{
file << line << std::endl;
}
}
我尝试使用类型为double的附魔for循环,但它不接受它,收到了错误,“此基于范围的'for'语句需要合适的“ begin”函数,但未找到”
重申一下,我试图将robotComplexity函数和robotValdity函数的结果添加到writefile函数所示的输出文件中。如果需要任何其他代码,请随时询问。谢谢。
已编辑以包含主要内容
int main() {
readFile(inputFile1, inputFile2, inputFile3, lines);
robotComplexity(lines);
robotVariability(lines);
writeFile(outputFile, lines, robotComplexity(lines), robotVariability(lines));
getch();
return 0;
}
尝试循环
for(double x: unitComplexity) {
file << x << std::endl;
}