-
July 4th, 2012, 09:37 PM
#1
PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
How do I make this code output/debug to a .txt file?
Here is the code:
#include <iostream>
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;
int n;
cout << "Enter the Number of Iterations "<<"\n";
cin >> n;
z=1;
cout<< "Enter Initial Postion:"<<"\n";
cin >> x;
cout<< "Enter Initial Velocity:"<<"\n";
cin >> v;
cout<< "Enter Time Step: "<<"\n";
cin>>dt;
cout<<"t,x"<<"\n";
while (z<n)
{
a= -k*x/m;
x= x + v*dt + .5* a*dt*dt;
v=v+a*dt;
t=t+dt;
cout<<t;
cout<<" , ";
cout<<x<<"\n";
cout<<" ";
z=z+1;
}
system("pause");
return 0;
}
-
July 4th, 2012, 11:48 PM
#2
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
-
July 4th, 2012, 11:52 PM
#3
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
And using your email id as your user name will certainly pull a lot of spam to your mail box.
-
July 5th, 2012, 12:03 AM
#4
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
Ya haha, but i am kinda confused on how to have the loop's results written to the txt file? I have already read that page, it is not really helping. Can you be more tell me where to place the codings?
-
July 5th, 2012, 12:07 AM
#5
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
a. Please use code tags, while posting the code, its very hard to read unformulated code (also a bit rude to the people who are willing to you help you, but you are not willing to help them).
b. Open file stream before your while loop and close it after the loop. In the body of the loop, everywhere you have used cout, use the file stream along side (as explained in the link). In the end, the output file would be same as the output that you'll get at console.
Last edited by Ejaz; July 5th, 2012 at 12:09 AM.
Reason: typo
-
July 5th, 2012, 02:39 AM
#6
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
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;
}
-
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 tyam.adi@gmail.com
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?
-
July 5th, 2012, 12:30 PM
#8
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
This is the best commenting/code tagging I can do. 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; //position
float v; //velocity
float a; //acceleration
float t; //time
int z;
float dt; //time step
int main () //main program
{
t=0; //time starts
ofstream myfile; //stream class to write on files
myfile.open ("euler.txt"); //open file euler.txt (WHERE IS euler.txt SUPPOSED TO BE LOCATED?
int n; //initiate number of iterations (loops)
cout << "Enter the Number of Iterations "<<"\n"; // number of iterations
cin >> n; //input number of iterations
z=1;
cout<< "Enter Initial Postion:"<<"\n"; //initial position
cin >> x; //input initial position
cout<< "Enter Initial Velocity:"<<"\n"; //initial velocity
cin >> v; //Input initial velocity
cout<< "Enter Time Step: "<<"\n"; //time step
cin>>dt; //input time step
cout<<"t,x"<<"\n"; // identify data as time, poition
while (z<n) //loop initiate
{
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 in file
myfile<<" , "; //Spacing
myfile<<x<<"\n"; //Display New Position in file
myfile<<" ";
z=z+1;
}
system("pause");
myfile.close(); //close file
return 0;
}
-
July 5th, 2012, 12:36 PM
#9
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
[code]code here[/code] will preserve your code indentation making it easier to read.
-
July 5th, 2012, 12:37 PM
#10
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
Originally Posted by tyam.adi@gmail.com
This is the best commenting/code tagging I can do.
If you don't know what a certain term means, there's little point in just guessing the meaning. Either search or just ask.
What you added to your code are called comments.
Code tags is something you add in your post on this forum that will help to preserve the indentation of the code to make it readable. See http://forums.codeguru.com/announcement.php?f=7.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
July 5th, 2012, 12:57 PM
#11
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
How is this?? Thanks for the tips. But,
I made the adjustments but the program is stopping before the Loop. AKA at: cout<<"t,x"<<"\n"; What am I doing wrong in the code
//Euler Code for Simple Harmonic Motion
Code:
#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; //position
float v; //velocity
float a; //acceleration
float t; //time
int z;
float dt;
int main ()
{
t=0; //start time
ofstream myfile; //stream class to write on files
myfile.open ("euler.txt"); //open file euler.txt (WHERE IS euler.txt 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;
}
-
July 5th, 2012, 08:32 PM
#12
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
-
July 6th, 2012, 03:20 AM
#13
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
-
July 6th, 2012, 03:25 AM
#14
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
But how does the code know where euler.txt is? im confused where/if i identify the location of the file...
-
July 6th, 2012, 03:26 AM
#15
Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?
my debugger says:
Warning 1 warning C4244: '=' : conversion from 'double' to 'float', possible loss of data Line 39
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
|