Home  |  Forums  |  914 Info  |  Blogs
 
914World.com - The fastest growing online 914 community!
 
Porsche, and the Porsche crest are registered trademarks of Dr. Ing. h.c. F. Porsche AG. This site is not affiliated with Porsche in any way.
Its only purpose is to provide an online forum for car enthusiasts. All other trademarks are property of their respective owners.
 

Welcome Guest ( Log In | Register )

> OT - C++ question.
Jeff Krieger
post May 13 2003, 06:05 PM
Post #1


Unregistered









Does anyone know how to modify this small program that I wrote in C++ so that for every cout statement there is a statement that writes the exact same information to the file accelfig.txt? I use this program to generate acceleration values and write them to accelfig.txt and then I read these acceleration values into another program that I wrote that numerically integrates these figures to calculate 0 to 60 mph and 1/4 mile times, speed at the end of the 1/4 mile, maximum hp, 60 mph to 0 braking distance etc. I use the free C++ compiler from http://www.bloodshed.net/dev/devcpp.html .


//CalcAccl1

#include < iostream.h >
#include < stdio.h >

// This program calculates 30 seconds worth of acceleration values, assumed
// to be in ft/s^2, and writes them to the file accelfig.txt.

main()
{ FILE *fp;

int counter;

double acceleration = 0, time = 0;

fp = fopen("accelfig.txt", "w");

for (counter = 0; counter <= 1600; counter++)
{
acceleration = time + 10;
fprintf(fp, "%20.10f\n", acceleration);
cout << "\nThe acceleration at " << time << " seconds is " << acceleration << '\n';
time = time + 0.0025;
}

for (counter = 1601; counter <= 4800; counter++)
{
acceleration = 14;
fprintf(fp, "%20.10f\n", acceleration);
cout << "\nThe acceleration at " << time << " seconds is " << acceleration << '\n';
time = time + 0.0025;
}

for (counter = 4801; counter <= 12000; counter++)
{
acceleration = 62 - 4*time;
fprintf(fp, "%20.10f\n", acceleration);
cout << "\nThe acceleration at " << time << " seconds is " << acceleration << '\n';
time = time + 0.0025;
}


fclose(fp);
}
Go to the top of the page
+Quote Post
 
Reply to this topicStart new topic
Replies(1 - 16)
SirAndy
post May 13 2003, 06:13 PM
Post #2


Resident German
*************************

Group: Admin
Posts: 41,649
Joined: 21-January 03
From: Oakland, Kalifornia
Member No.: 179
Region Association: Northern California



here you go:


Attached File(s)
Attached File  accelfig.c ( 1.29k ) Number of downloads: 7
User is online!Profile CardPM
Go to the top of the page
+Quote Post
Jeff Krieger
post May 13 2003, 06:16 PM
Post #3


Unregistered









I get an error message when I try to open it with my compiler.
Go to the top of the page
+Quote Post
john rogers
post May 13 2003, 06:21 PM
Post #4


Senior Member
***

Group: Members
Posts: 1,525
Joined: 4-March 03
From: Chula Vista CA
Member No.: 391



I tried the code and it works fine as the data goes into the file looks the same as what is on the screen. There is only the numeric data, no text, if that is what you want? I used Microsoft Devstudio 6.0 and it went fine?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
SirAndy
post May 13 2003, 06:22 PM
Post #5


Resident German
*************************

Group: Admin
Posts: 41,649
Joined: 21-January 03
From: Oakland, Kalifornia
Member No.: 179
Region Association: Northern California



all you need to do is to put this line in your code after each line of cout:

fprintf(fp, "\nThe acceleration at %f seconds is %f\n", time, acceleration);

back to work ...

edit: i did not try to compile it, i assumed you had it running BEFORE you posted here ...
User is online!Profile CardPM
Go to the top of the page
+Quote Post
Brad Roberts
post May 13 2003, 06:23 PM
Post #6


914 Freak!
***************

Group: Members
Posts: 19,148
Joined: 23-December 02
Member No.: 8
Region Association: None



Went fine for me also..

Alfred... he wouldnt post a virus infected on his own server for everyone on the BBS to dowload. Think past the anger ...


B
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
SirAndy
post May 13 2003, 06:29 PM
Post #7


Resident German
*************************

Group: Admin
Posts: 41,649
Joined: 21-January 03
From: Oakland, Kalifornia
Member No.: 179
Region Association: Northern California



QUOTE(Jeff Krieger @ May 13 2003, 05:16 PM)
I get an error message when I try to open it with my compiler.

did the initial version compile? i only added 3 lines and a few tabs ...
User is online!Profile CardPM
Go to the top of the page
+Quote Post
Jeff Krieger
post May 13 2003, 06:40 PM
Post #8


Unregistered









Ok, I copied and pasted that one line into my program and it seems to work perfectly. Thanks, Andy, I appreciate it.
Go to the top of the page
+Quote Post
krk
post May 13 2003, 07:23 PM
Post #9


Senior Member
***

Group: Members
Posts: 997
Joined: 27-December 02
From: San Jose
Member No.: 22



If you want to use ostream fiiles the same way you use cout, you have to do a few includes, and set the output stream up. Once set up, you use it just like cout, except it's heading to a file.

Or, if you want the mundane approach, you could always switch back to C. Oops, I see you have. :-)


#include
#include
#include

main()
{
// open a file for outpout
ofstream oFile("newoutput.txt", ios::out);

// check to make sure it worked
if ( !oFile ) { //open failed?
cerr << "open failed" << endl;
exit (-1);
}

// write something to it

oFile << "The horsepower at " << "etc" << endl;

return 0;
}

This worked on GCC last time I tried it. (does it look familar? :-)

love kim.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
krk
post May 13 2003, 07:26 PM
Post #10


Senior Member
***

Group: Members
Posts: 997
Joined: 27-December 02
From: San Jose
Member No.: 22



hmm.. tabs disappear on posting. Interesting.

kim.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
Qarl
post May 13 2003, 07:26 PM
Post #11


Shriveled member
*****

Group: Benefactors
Posts: 5,233
Joined: 8-February 03
From: Florida
Member No.: 271
Region Association: None



Buncha computer geeks! I use to know that crap. Not any longer.

Have fun!

(IMG:style_emoticons/default/tongue.gif)

Karl
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
krk
post May 13 2003, 07:42 PM
Post #12


Senior Member
***

Group: Members
Posts: 997
Joined: 27-December 02
From: San Jose
Member No.: 22



QUOTE(krk @ May 13 2003, 05:23 PM)
I


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>


So this is wierd. (I wonder if it's the browser I'm using) The include names don't appear for me (i.e. where "#include " is writtten, I see "#include") I've left them in the quoted section to see what I see.

kim.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
krk
post May 13 2003, 07:43 PM
Post #13


Senior Member
***

Group: Members
Posts: 997
Joined: 27-December 02
From: San Jose
Member No.: 22



QUOTE(krk @ May 13 2003, 05:42 PM)
QUOTE(krk @ May 13 2003, 05:23 PM)
I


#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>


So this is wierd. (I wonder if it's the browser I'm using) The include names don't appear for me (i.e. where "#include <crap>" is writtten, I see "#include") I've left them in the quoted section to see what I see.

kim.

test.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
krk
post May 13 2003, 07:44 PM
Post #14


Senior Member
***

Group: Members
Posts: 997
Joined: 27-December 02
From: San Jose
Member No.: 22



So the <>'s appear in quoted text to me, but not in "regular" text.

Anyone else seeing this?

kim.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post
SirAndy
post May 13 2003, 10:16 PM
Post #15


Resident German
*************************

Group: Admin
Posts: 41,649
Joined: 21-January 03
From: Oakland, Kalifornia
Member No.: 179
Region Association: Northern California



QUOTE(krk @ May 13 2003, 06:44 PM)
So the <>'s appear in quoted text to me, but not in "regular" text.

Anyone else seeing this?

kim.

yepp, cause in regular posts (not qoutes) your brackets indicate HTML keywords, which are stripped out by the browser for display (IMG:style_emoticons/default/wink.gif)
User is online!Profile CardPM
Go to the top of the page
+Quote Post
Jeff Krieger
post May 14 2003, 12:21 AM
Post #16


Unregistered









QUOTE(krk @ May 13 2003, 06:23 PM)
If you want to use ostream fiiles the same way you use cout, you have to do a few includes, and set the output stream up. Once set up, you use it just like cout, except it's heading to a file.

Or, if you want the mundane approach, you could always switch back to C. Oops, I see you have. :-)


#include
#include
#include

main()
{
   // open a file for outpout
   ofstream oFile("newoutput.txt", ios::out);

   // check to make sure it worked
   if ( !oFile ) { //open failed?
       cerr << "open failed" << endl;
       exit (-1);
   }

   // write something to it

   oFile << "The horsepower at " << "etc" << endl;

   return 0;
}

This worked on GCC last time I tried it. (does it look familar? :-)

love kim.




Andy's way is much easier - besides, I just wanted to be able to write the output to a text file so I could check for errors more easily.

QUOTE
This worked on GCC last time I tried it. (does it look familar? :-)

Are you Kim the lady mathematician? Hey, do you know why a negative ... ah forget it.
Go to the top of the page
+Quote Post
Jeff Krieger
post May 14 2003, 12:23 AM
Post #17


Unregistered









QUOTE(krk @ May 13 2003, 06:44 PM)
So the <>'s appear in quoted text to me, but not in "regular" text.

Anyone else seeing this?

kim.

Just leave an extra space on each end e.g., < iostream.h >.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 20th May 2024 - 12:40 AM