Hello im learning c++ and i was wondering how i can call a function that will write to a file. within that function it will call other functions and will print the output. How would i do that?
您好我正在学习c ++,我想知道如何调用将写入文件的函数。在该函数中,它将调用其他函数并将打印输出。我该怎么做?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void buildArray(float arrayScores[], int numOfScores);
void printOutArray(float arrayScores[], int numOfScores);
void writeToFile(float arrayScores[], int numOfScores);
int main(){
int numOfScores;
cout << "Enter the number of scores: "
cin >> numOfScores;
float *arrayScores = nullptr;
arrayScores = new float [numOfScores];
writeToFile(arrayScores, numOfScores);
delete [] arrayScores;
}
void buildArray(float arrayScores[], int numOfScores){
float score = 0;
for (int i=0; i<numOfScores; i++){
cout << "Enter the score: ";
cin >> score;
arrayScores[i] = score;
}
void printOutArray(float arrayScores[], int numOfScores){
int Items = numOfScores;
for (int i = 0; i<numOfScores; i++){
float grade = arrayScores[i];
cout << "Score number " << i+1 << ": " << arrayScores[i] << endl;
}
}
void writeToFile(arrayScores[], int numOfScores){
ofstream outfile;
outfile.open("Scores.txt");
outfile << buildArray(arrayScores,numOfScores);
outfile << printOutArray(arrayScores,numOfScores);
outfile.close();
}
1 个解决方案
#1
0
outfile << buildArray(arrayScore, numOfScores);
attempts to send the value returned by buildArray
to outfile
. But you have declared that buildArray
does not return anything! You have done this by writing void
before its name in its declaration (and definition).
尝试将buildArray返回的值发送到outfile。但是你已经声明buildArray没有返回任何东西!您已通过在其声明(和定义)中的名称前写入void来完成此操作。
You have two options
你有两个选择
- Return whatever it is that you want to be printed, as the result of
buildArray
. - Instead of sending the results of
buildArray
tooutfile
, passoutfile
as an argument tobuildArray
and send date tooutfile
inside the body ofbuildArray
.
作为buildArray的结果,返回您想要打印的内容。
不是将buildArray的结果发送到outfile,而是将outfile作为参数传递给buildArray,并将日期发送到buildArray体内的outfile。
Here is some code to get you going with the second idea.
这里有一些代码可以帮助您了解第二个想法。
#include <ostream>
void buildArray(std::ostream& outfile, float arrayScores[], int numOfScores){
float score = 0;
for (int i=0; i<numOfScores; i++){
cout << "Enter the score: ";
cin >> score;
arrayScores[i] = score;
outfile << /* whatever you want to write goes here */
}
Points to note:
注意事项:
- Include the
ostream
header. - Add a parameter to the function, for the filestream you want to use.
- Use the stream inside the function, show on the last line of the body.
包含ostream标头。
为函数添加一个参数,用于要使用的文件流。
使用函数内部的流,显示在正文的最后一行。
#1
0
outfile << buildArray(arrayScore, numOfScores);
attempts to send the value returned by buildArray
to outfile
. But you have declared that buildArray
does not return anything! You have done this by writing void
before its name in its declaration (and definition).
尝试将buildArray返回的值发送到outfile。但是你已经声明buildArray没有返回任何东西!您已通过在其声明(和定义)中的名称前写入void来完成此操作。
You have two options
你有两个选择
- Return whatever it is that you want to be printed, as the result of
buildArray
. - Instead of sending the results of
buildArray
tooutfile
, passoutfile
as an argument tobuildArray
and send date tooutfile
inside the body ofbuildArray
.
作为buildArray的结果,返回您想要打印的内容。
不是将buildArray的结果发送到outfile,而是将outfile作为参数传递给buildArray,并将日期发送到buildArray体内的outfile。
Here is some code to get you going with the second idea.
这里有一些代码可以帮助您了解第二个想法。
#include <ostream>
void buildArray(std::ostream& outfile, float arrayScores[], int numOfScores){
float score = 0;
for (int i=0; i<numOfScores; i++){
cout << "Enter the score: ";
cin >> score;
arrayScores[i] = score;
outfile << /* whatever you want to write goes here */
}
Points to note:
注意事项:
- Include the
ostream
header. - Add a parameter to the function, for the filestream you want to use.
- Use the stream inside the function, show on the last line of the body.
包含ostream标头。
为函数添加一个参数,用于要使用的文件流。
使用函数内部的流,显示在正文的最后一行。