Trouble with printing to a file

Thread Solved

Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Trouble with printing to a file

 
0
  #1
Aug 17th, 2005
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.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <fstream>
  6. #define MINMASS (0.0000515053396)
  7. #define pi (3.14159265)
  8. #define JUPVOL (6.98509E9 * 6.98509E9 * 6.98509E9 * (4.00/3.00) * pi)
  9. #define SOLM2G (1.98892E33)
  10. using namespace std;
  11.  
  12. int main ()
  13. {
  14. double j1, j2, j3, j4;
  15. ofstream file;
  16. file.open("big.in"); //open a file
  17.  
  18. srand ( time(NULL) );
  19. srand48(time(NULL));
  20.  
  21.  
  22. j1 = drand48() * 0.013365; //mass needs to be between 5.15053396E-5 and 0.013365
  23.  
  24. while(j1 < MINMASS)
  25. {
  26. srand48(time(NULL));
  27. j1 = drand48() * 0.013365;
  28. }
  29. file<<" JOVIAN1 m="<<j1<<" r=20.D0 d="<<(j1/JUPVOL)*SOLM2G ;
  30.  
  31.  
  32. file.close(); //close it
  33. return 0;
  34.  
  35. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,574
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1612
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Trouble with printing to a file

 
0
  #2
Aug 17th, 2005
functions in <ioman>. Here are other values that can be used for setioflags function.

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. float n = 123.456F;
  8. // output float with 5 decimal places
  9. cout << setprecision(5) << setiosflags(ios_base::fixed) << n << endl;
  10. return 0;
  11. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Re: Trouble with printing to a file

 
0
  #3
Aug 17th, 2005
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:
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Re: Trouble with printing to a file

 
0
  #4
Aug 17th, 2005
I figured out the E problem, I'm still working on the decimal problem.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 80
Reputation: Daishi is an unknown quantity at this point 
Solved Threads: 2
Daishi Daishi is offline Offline
Junior Poster in Training

Re: Trouble with printing to a file

 
0
  #5
Aug 17th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Re: Trouble with printing to a file

 
0
  #6
Aug 17th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,574
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1612
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Trouble with printing to a file

 
0
  #7
Aug 17th, 2005
for floats look again at the cout line in my original post.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Re: Trouble with printing to a file

 
0
  #8
Aug 17th, 2005
Originally Posted by Ancient Dragon
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.

  1. file<<" JOVIAN1 m="<< setprecision(17) << setiosflags(ios_base::uppercase) << setiosflags(ios_base::scientific)
  2. << 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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,574
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1612
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Trouble with printing to a file

 
0
  #9
Aug 18th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 24
Reputation: altheastronut is an unknown quantity at this point 
Solved Threads: 0
altheastronut altheastronut is offline Offline
Newbie Poster

Re: Trouble with printing to a file

 
0
  #10
Aug 18th, 2005
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2180 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC