0

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?

#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();
}
  • `functionThatCallsOtherFunctionsInOrderToPrintToAFile();` - But you should consider a shorter and more domain specific name. – StoryTeller - Unslander Monica Oct 09 '17 at 06:04
  • Please, if you use new, use it correctly. You're missing delete. – deviantfan Oct 09 '17 at 06:04
  • Well unless you ask about build errors, then please show us code that *builds*. I recommend you take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask), and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Oct 09 '17 at 06:05
  • I know it has some errors i tried to copy and paste the the format was off so i typed it out. How would i print the output of the functions to the file? – NotHungry2Phil Oct 09 '17 at 06:07
  • Formatting second, proper copy-pasted code first. There's always someone willing to help you fix formatting problems. If you rewrite code into the question there's always the chance you add some unrelated errors by mistake, and those distract from the real problem. Or worse, you accidentally *fix* the problem you had, making us all scratch our heads wondering what you're talking about. – Some programmer dude Oct 09 '17 at 06:08
  • As for your problem, output to an output stream works just the same no matter the stream. An output stream is an output stream is an output stream. And you manage to write to the *output stream* `cout` fine in `printOutArray`. Perhaps what you need is [a good beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to read? – Some programmer dude Oct 09 '17 at 06:11
  • It should be noted that your `writeToFile()` function should _not_ be obtaining scores from the user. That should be done _before_. – Dúthomhas Oct 09 '17 at 06:23

1 Answers1

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).

You have two options

  1. Return whatever it is that you want to be printed, as the result of buildArray.
  2. Instead of sending the results of buildArray to outfile, pass outfile as an argument to buildArray and send date to outfile inside the body of buildArray.

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:

  1. Include the ostream header.
  2. Add a parameter to the function, for the filestream you want to use.
  3. Use the stream inside the function, show on the last line of the body.
jacg
  • 2,040
  • 1
  • 14
  • 27