Help with ofstream within a function

Hello all,

I have this code and I can't figure out why my ofstream isn't working as it should within it's folder. When I run the current code, my 'black box' in codeblocks shows all my information, while the .txt file has nothing, it's empty.

Thank you for the assistance!


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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

float fallingDistance(int time)
{
    return(0.5*9.8*time*time);
}

void printHeader()
{
    cout<<setw(5)<<" Time"<<setw(20)<<" Distance\n"
        <<setw(5)<<" (seconds)"<<setw(15)<<" (meters)\n"
        <<"________________________\n";
}

void print(int time, float distance)
{
    cout<<"    "<<setw(16)<<left<<time<<distance<<endl;
}

int main()
{
    ofstream Descent;
    Descent.open("Distance.out");

    printHeader();

    for(int seconds=1; seconds<=15; seconds++)
    {
        print(seconds, fallingDistance(seconds));
    }

    Descent.close();
    return 0;
}
Last edited on
cout writes to the console (black box, though the color scheme can be set).

pass an ofstream into the functions and use it:

void printHeader(ofstream & ofs)
{
ofs<<setw(5)<<" Time"<<setw(20)<<" Distance\n"
<<setw(5)<<" (seconds)"<<setw(15)<<" (meters)\n"
<<"________________________\n";
}

You need to actually output things to the file to see things. Just opening a file doesn't cause all subsequent std::cout's to go to both the terminal and the file. That would just be annoying.

If you want your output to go to the file then pass Descent to your print function (as a std::ofstream&) and output to it instead of std::cout.
effin duh!!!!! Christ Dutch lol. THank you for the insight, I can't believe I did that... I know better!
Now I have added ofstream the function, can you help me within the int main function? the for statment is having issues with the print.

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

float fallingDistance(int time)
{
    return(0.5*9.8*time*time);
}

void printHeader(ofstream Descent)
{
    Descent<<setw(5)<<" Time"<<setw(20)<<" Distance\n"
        <<setw(5)<<" (seconds)"<<setw(15)<<" (meters)\n"
        <<"________________________\n";
}

void print(int time, float distance, ofstream Descent)
{
    Descent<<"    "<<setw(16)<<left<<time<<distance<<endl;
}

int main()
{
    ofstream Descent;
    Descent.open("Distance.out");

void  printHeader(ofstream Descent);

    for(int seconds=1; seconds<=15; seconds++)
    {
      print(seconds, fallingDistance(seconds),Descent)
    }

    Descent.close();
    return 0;
}
You need to pass it as a reference ostream&
And note that it just needs to be an ostream, not an ofstream
An ostream is higher up the class hierarchy and therefore can accept, for instance, std::cout as well as an ofstream (or even an ostringstream).
Last edited on
Why is it whenever I have (ofstream Decent) my code blocks opens an ios_base.h?
I simply cannot parse that. :-(
Also, on the file, it isn't printing out the printHeader function.
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

float fallingDistance(int time)
{
    return(0.5*9.8*time*time);
}

void printHeader(ostream & Descent)
{
    Descent<<setw(5)<<" Time"<<setw(20)<<" Distance\n"
           <<setw(5)<<" (seconds)"<<setw(15)<<" (meters)\n"
           <<"________________________\n";
}

void print(int time, float distance, ostream & Descent)
{
    Descent<<"    "<<setw(16)<<left<<time<<distance<<endl;
}

int main()
{
    ofstream Descent;
    Descent.open("Distance.out");

void  printHeader(ostream & Descent);

    for(int seconds=1; seconds<=15; seconds++)
    {
      print(seconds, fallingDistance(seconds),Descent);
    }

    Descent.close();
    return 0;
}
When I said I couldn't "parse" that I meant I couldn't understand your question. If your current IDE is randomly opening files that you didn't request then there's obvioudly some kind of serious problem.

As for printing the header, you need to make an actual function call. All you've done is put a declaration of the printHeader function in the middle of main. Get rid of that and make it a function call:

 
    printHeader(Descent);

Alrighty, SO, now i have the header printing to file. But it is repeating the header on each line. I just need the header to print once and insert the numbers into a list format.

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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

float fallingDistance(int time)
{
    return(0.5*9.8*time*time);
}

void printHeader(ofstream & Descent)
{
    Descent<<"  "<<setw(20)<<" Time"<<" Distance\n"
           <<"  "<<setw(15)<<" (seconds)"<<" (meters)\n"
           <<"________________________\n"<<endl;
}

void print(int time, float distance, ostream & Descent)
{
    Descent<<"    "<<setw(16)<<left<<time<<distance<<endl;
}

int main()
{
    ofstream Descent;
    Descent.open("Distance.out");

    for(int seconds=1; seconds<=15; seconds++)
    {
      printHeader(Descent);
      print(seconds, fallingDistance(seconds),Descent);

    }

    Descent.close();
    return 0;
}
Think about why the header is printing over and over again, almost as if it was in a loop. You just might be able to crack that one.

To line up the numbers properly under the header you'll need to use setw. I'm not sure if you really want your numbers left-justified, so you might want to get rid of left.
Thanks, I had it inside the call for the printing of the numbers.
Quick question, can you help me understand further why you place ostream& and not ofstream&??

I placed both in the argument and it didn't appeaer to make a difference.
Although it doesn't make a difference in your specific case to use ofstream, it needlessly limits the use of your print functions. Suppose you wanted to print the information to the terminal first, and then ask the user if he wants a copy printed to a file? In that case it would be nice to be able to pass std::cout to the print functions and have them output to the terminal. If you use ostream it can accept either std::cout (which is an ostream) or an ofstream. This is because ofstream "inherits" from ostream and therefore can act like one. You probably have yet to learn about class hierarchies. Suffice it to say that you should use ostream (or istream for input) unless the function uses methods specific to files (like open, close, is_open), in which case you would need to use ofstream.
Topic archived. No new replies allowed.