CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jul 2012
    Posts
    9

    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;
    }

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?


  3. #3
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    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.

  4. #4
    Join Date
    Jul 2012
    Posts
    9

    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?

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    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

  6. #6
    Join Date
    Jul 2012
    Posts
    9

    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;
    }

  7. #7
    Join Date
    May 2007
    Posts
    811

    Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?

    Quote Originally Posted by tyam.adi@gmail.com View Post
    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?

  8. #8
    Join Date
    Jul 2012
    Posts
    9

    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;
    }

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?

    Quote Originally Posted by tyam.adi@gmail.com View Post
    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

  11. #11
    Join Date
    Jul 2012
    Posts
    9

    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;
    }

  12. #12
    Join Date
    Jul 2012
    Posts
    9

    Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?

    BUMP anyone?

  13. #13
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: PLEASE HELP A NOVICE WITH C++. How do i make this code output to a .txt file?

    Try to setup through the debugger and see how the flow of your program, along with the values of each variable. Most likely following check is not being satisfied.

    Code:
    while (z<n)
    Following links can help you with debugging.

    Mastering Debugging in Visual Studio 2010 - A Beginner's Guide
    Tutorial - Debugging C++ in Visual Studio 2010 part (1/3)
    Tutorial - Debugging C++ in Visual Studio 2010 (part 2/3)
    Tutorial - Debugging C++ in Visual Studio 2010 part (3/3)

  14. #14
    Join Date
    Jul 2012
    Posts
    9

    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...

  15. #15
    Join Date
    Jul 2012
    Posts
    9

    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured