Hello, I'm almost done with my program however I'm having trouble getting what I want printed to a file. I'm familiar with the printf function and what I want is to print out to the file in scientific notation, which would be %E if I were using printf. Is there something similar when writing out to a file. Also when using printf you can choose how many decimal places you need using %.2f or something like that, I also need to use something similar to that when writing out to a file. Here is how I have been writing out to a file. Can someone please help.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <fstream>
#define MINMASS  (0.0000515053396)
#define pi (3.14159265)
#define JUPVOL (6.98509E9 * 6.98509E9 * 6.98509E9 * (4.00/3.00) * pi)
#define SOLM2G (1.98892E33)
using namespace std;

int main ()
{
   double j1, j2, j3, j4; 
   ofstream file;
   file.open("big.in");          //open a file

  srand ( time(NULL) );
  srand48(time(NULL));
                         

  j1 = drand48() * 0.013365;    //mass needs to be between 5.15053396E-5 and 0.013365 

while(j1 <  MINMASS)
    {
      srand48(time(NULL));
      j1 = drand48() * 0.013365;
    }
 file<<" JOVIAN1     m="<<j1<<" r=20.D0 d="<<(j1/JUPVOL)*SOLM2G  ;


  file.close();                  //close it
   return 0;

}

functions in <ioman>. Here are other values that can be used for setioflags function.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	float n = 123.456F;
	// output float with 5 decimal places
	cout << setprecision(5)  << setiosflags(ios_base::fixed) << n <<  endl;
	return 0;
}

Thanks for your reply, I changed my code with your suggestion and at first I thought it was working the way I wanted, but it didn't. The first value that was written out seems fine, the second value that gets printed out seems to be modified by the specification of the first. What I am expecting to get is:
JOVIAN1 m=9.26272836047433573E-03 r=20.D0 d=13.72

using:

file<<" JOVIAN1     m="<< setprecision(17) << setiosflags(ios_base::scientific) <<j1<<" r=20.D0 d=" << setprecision(2)  << setiosflags(ios_base::fixed) <<(j1/JUPVOL)*SOLM2G;

what I get is: JOVIAN1 m=9.26272836047433573e-03 r=20.D0 d=13

The second value doesn't have two decimal places. The smaller problem, and it may be a little picky is when using printf %E gives you an upper case E for the exponential, is there any way of doing this? I'm going to run this file into a simulation program and the format for the simulation shows that it uses an upper case E. Thanks again.

Try setting the precision to 4, instead of 2.

And I'm really curious as to what this program does...? Does it have to do with Jovian orbits...?

-Fredric

I tried changing the precision, but what happens is if the random number is greater than nine, it alters the number of decimal places. So if it's set for 2 and the number is 10.XXXX it only prints out 10 If the number is 9.XXXXX it only prints out 9.X. I don't know what's going on here. I thought set precision was supposed to only effect the numbers after the decimal place.

Yeah, it's a program that randomly generates the mass of a jovian planet from Neptunes mass up to 14 times Jupiters mass, and from the mass it calculates the density of the planet. Then I'll run the file in a simulation and see which orbits are stable. It's actually pretty cool, I just have to have to get everything up and running correctly.

for floats look again at the cout line in my original post.

The only differences I see between what you have and I have are the F in your defining your float, and the endl; at the end. From what I read endl; ends the line and flushes the stream. I flushed the stream between the two outputs (since I don't end the line after the first) with flush, and I still have a problem with the decimal output.

file<<" JOVIAN1     m="<< setprecision(17) << setiosflags(ios_base::uppercase) << setiosflags(ios_base::scientific) 
     << j1 <<" r=20.D0 d=" << flush << setprecision(2) << setiosflags(ios_base::fixed) << ((j1/JUPVOL) * SOLM2G) << endl;

Even when I do use endl at the end of each output separating them by line it doesn't give me a decimal with two places. If I don't have the first input at all it works just fine printing out two decimal places.

what is the desired output?

JOVIAN1 m=8.08181550000000040E+001 r=20.D0 d=112595.65

if that last number is what you want, then you have to remove the scientific flag. You can combine some of those setflags by using | operator.

file<<" JOVIAN1     m="<< setprecision(17) << setiosflags(ios_base::uppercase | ios_base::scientific) 
     << j1 <<" r=20.D0 d=" << resetiosflags(ios_base::scientific) << setprecision(2) << setiosflags(ios_base::fixed) << ((j1/JUPVOL) * SOLM2G) << endl;

Also -- srand() (and its variants) should only be called once at the very betinning of the program. you should remove it from that loop.

Awesome, I want to thank you again for all your help, I learned a whole lot from it. I have never used anything we had gone through in this post. Have a great day!

This question has already been answered. Start a new discussion instead.