How to call a function that will write to a file and within that function call other functions

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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();
}


http://wyleczsiezdrowiem.pl/
Last edited on
outfile << buildArray(arrayScores,numOfScores);
buildArray returns void. Nothing. So what is supposed to be output here?


outfile << printOutArray(arrayScores,numOfScores);
printOutArray returns void. Nothing. So what is supposed to be output here?
Last edited on
You have three distinct functions, build, print, write. Each function should do one specific thing.
It's not a good idea to call build and print inside your write function. A better design would be to call build, print and write individually from main().

Topic archived. No new replies allowed.