Hi, if I run the following code and press L on the screen, I should be getting a leapfrog.txt textfile. But I only see my previous euler.txt. What is wrong with the code.

Thanks in advance for any help.

#include <iostream>
#include <fstream>
#include <cmath>

#include "H:\Visual Studio 2008\Projects\section2\threevector.h"

using namespace std;

void errors(double, threevector, ofstream&);                          // Function that calculates the errors 
                                                                      //           and outputs    the results

int main()
{
	char determinant;
	cout << "Press E to use Euler's method or L to use leapfrog method." << endl;
	cin >> determinant;
	
	if (determinant = 'E')                                            // Euler method
	{
		ofstream outfile("euler.txt");
		outfile << "Time \t Vx \t Vy \t Vz \t Vxerror \t Vyerror \t Vzerror \t Vmagerror" << endl;
		
		threevector vn(0.0, 0.0, 2.0);                                    // Initial vector velocity
		threevector  b(1.0, 0.0, 0.0);                                    // Magnetic field
		double delta_t = 0.01;
		
		for (int i = 0; i < 2001; i++)                                    // Calculates the velocity from t = 0 to t = 20  
		{                                                                 //                         in steps of delta_t = 0.01
			double t = delta_t*i;                                         // Time
			if (t == 0.0)
			{
				errors(t, vn, outfile);
			}
			else
			{
				vn += ((vn^b)*delta_t);                                    // New velocity
				errors(t, vn, outfile);
			}
		}
	}

	else if (determinant = 'L')                                        // Leapfrog method
	{
		ofstream outfile("leapfrog.txt");
		outfile << "Time \t Vx \t Vy \t Vz \t Vxerror \t Vyerror \t Vzerror \t Vmagerror" << endl;
		
		threevector vnminus1;                                               // where vnminus1 will be stored
		threevector vreserve;                                               // where vn       will be stored
		
		threevector vn(0.0, 0.0, 2.0);                                      // Initial vector velocity
		threevector b(1.0, 0.0, 0.0);                                       // Magnetic Field
		double delta_t = 0.01;
		
		for (int i = 0; i < 2; i++)                                        // Euler used to calculate vnminus1 and vn
		{
			double t = delta_t*i;                                          // Time
			if (t == 0.0)
			{
				vnminus1 = vn;                                             // The first  velocity: vnminus1
				errors(t, vn, outfile);
			}
			else
			{
				vn += ((vn^b)*0.01);                                        // The second velocity: vn
				errors(t, vn, outfile);

			}
		}
		for (int i = 2; i < 2001; i++)                                      // Leapfrog used for all subsequent iterations
		{
			double t = delta_t*i;                                           // Time
			vreserve = vn;                                                  // vn       stored for subsequent calculation                          
			vn = vnminus1 + ((vn^b)*(2.0*delta_t));
			vnminus1 = vreserve;                                            // vnminus1 stored for subsequent calculation   
			errors(t, vn, outfile);

		}
	}
	else
	{}
	
	return 0;
}

void errors(double t, threevector vn, ofstream& outfile)
{
	threevector vtrue (0.0, 2.0*sin(t), 2.0*cos(t));                    // True velocity
	threevector verror = vn - vtrue;                                    // Error in velocity components
	double vmagerror = vn.mag3() - 2.0;                                 // Error in veocity magnitude
	outfile << t << '\t' 
	             << vn.getx() << '\t' << vn.gety() << '\t' << vn.getz() << '\t' 
	 		     << verror.getx() << '\t' << verror.gety() << '\t' << verror.getz() << '\t'
	    		 << vmagerror 
			     << endl;

}

Use comparison ( == ) instead of assignment ( = ) in your if-statements.

if (determinant = 'E')

This will _always_ be true, note that in C/C++ if you want to compare two values you should use '==', not '='.

I'd suggest to write the if statements like this:

if( 'E' == determinant )

That way, if you accidentally write = instead of == you will get a compiler error.

Thanks!

How does the compiler interpret

if (determinant = 'E')

anyway?

It assigns 'E' to determinant, the assignment succeeds so the overall statement is interpreted as true.

I've written the above piece of code as part of a task. Would you please mind having a look at the code and checking for any possible errors or ways to improve the code?

Please! I'll upload the document if you wish.

I can give you some hints for improvement, but you have to try and fix them yourself first ;):

* What happens if a user does not enter 'E' or 'L', but for example: "Hello I am trying to crash your program by entering text that you do not expect!" ?
Similarly, what happens if they enter 'A', or 'e' ?

* What happens if your program can not open the outfile? Maybe you don't have permissions to write the file, etc.

* It looks like delta_t is used as a constant value. It is good to be explicit when it comes to constness, so declare delta_t as const:

const double delta_t = 0.01;

* vn += ((vn^b)*delta_t);
Its hard to judge if this is correct, as these operators (+= and ^) are implemented the threevector.

* if (t == 0.0)
Comparing doubles is dangerous, you can not really do it precisely.

* It may be more clear to whoever is correcting your code to split the work up into functions.

That's all I have time for right now, hope it helps.

Please mark the thread as solved when it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.