|
-
July 5th, 2012, 10:00 AM
#7
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
 Originally Posted by [email protected]
Oh sorry about that, I completely forgot. I will do my best to use code tags. I made the adjustments but the program is stopping before the Loop. AKA at: cout<<"t,x"<<"\n"; What am I missing?
//Euler Code for Simple Harmonic Motion
#include <iostream>
#include <fstream> //Stream class to both read and write from/to files.
using namespace std;
#define k 100 //force constant= 100 kcal/(mol*A^2)
#define m 16 //mass= 16 Daltons
float x;
float v;
float a; //acceleration
float t; //time
int z;
float dt;
int main ()
{
t=0;
ofstream myfile; //stream class to write on files
myfile.open ("euler.txt"); //open file euler.txt (WHERE IS THIS SUPPOSED TO BE LOCATED?
int n;
cout << "Enter the Number of Iterations "<<"\n"; //input number of iterations
cin >> n;
z=1;
cout<< "Enter Initial Postion:"<<"\n"; //input initial position
cin >> x;
cout<< "Enter Initial Velocity:"<<"\n"; //input initial velocity
cin >> v;
cout<< "Enter Time Step: "<<"\n"; //input time step
cin>>dt;
cout<<"t,x"<<"\n"; // identify data as time, poition
while (z<n) //loop
{
a= -k*x/m; //formula for acceleration
x= x + v*dt + .5* a*dt*dt; //formula for position
v=v+a*dt; //formula for velocity
t=t+dt; //formula for time
myfile << t; //Display New Time
myfile<<" , "; //Spacing
myfile<<x<<"\n"; //Display New Position
myfile<<" ";
z=z+1;
}
system("pause");
myfile.close(); //close file
return 0;
}
What happened to do your best using code tags?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|