Outputting table to text file. While loop problem!

Hi there.
The problem that I'm experiencing here is quite a specific one, and after a few searches, I concluded that I ought to just ask.

I have calculated earlier in my program various values in various arrays, angle[], t[], y[], x[] etc and now I want to output these values in table form to a text file. Because these arrays contain different values I have introduced a while loop which I thought would write one row of the table "num" times, changing the value of x[1] to x[2] etc every time it loops.

This isn't happening however, and the only thing being written to file is the table header shown on lines 5-7 below. It seems as if the while loop and everything inside it is being ignored completely!
Am I missing something really obvious here?

What is bugging me even more is the fact that I used the same while loop method to cout the same table to be viewed inside my program by the user and this works fine. (This code is shown further down the page).

This is a problem that is really holding back the progress with my project and any help would be greatly appreciated!
Many thanks, Robin.

Writing to file code.

ofstream myfile;

myfile.open("projectiles.txt");

myfile << setw(20) << "number" << setw(20) << "Angle (rads)" << setw(20) << "Time (s)" << setw(20) << "Max height (m)" << setw(20) << "Max distance (m)" << endl;

while (n < num)
{
n++;

myfile << setw(20) << n << setw(20) << angle[n] << setw(20) << t[n] << setw(20) << y[n] << setw(20) << x[n] << endl;

}

myfile.close();

cout << "End the program and view the output file or try new \n"
<< "values of u and theta \n"
<< "...." << endl;


cout code.

cout << setw(10) << "number" << setw(15) << "Angle (rads)" << setw(15) << "Time (s)" << setw(15) << "Height (m)" << setw(15) << "Distance (m)" << endl;

while (m < num)
{
m++;

cout << setw(10) << m << setw(15) << angle[m] << setw(15) << t[m] << setw(15) << y[m] << setw(15) << x[m] << endl;
}
I'm not sure but I think you have to add the base class to the open() method.

myfile.open("projectiles.txt", ios_base::out)

maybe you have to pass the filename character by character too.

myfile.open("projectiles.txt".c_str(), ios_base::out)
Last edited on
You might want to put your code into code tags:
http://www.cplusplus.com/forum/articles/16853/

Do you give n an initial value?

Its more usual for a basic numeric loop to use for() rather than while. Its harder to forget setting the initial value of the iterator:
1
2
3
4
for (n = 0; n < num; ++n)
{
    myfile << setw(20) << n << setw(20) << angle[n] << setw(20) << t[n] << setw(20) << y[n] << setw(20) << x[n] << endl;
}
Thanks very much for the suggestion but the same thing is still happening. The output file still just takes the form;

number Angle (rads) Time (s) Max height (m) Max distance (m)

Cheers, Rob
To Galik.

Yes I stated n = 0 when initially declaring it :¬)

I will try what you suggested, (with the iterator and the cade tags!)

Thanks
Galik and Takiro, thankyou very much!
The problem is solved.
I really appreciate the help you've given. I could have been here for a while scratching my head over that :¬)
Robin
Topic archived. No new replies allowed.