SUPER NEW

Okay, I literally just began C++ programming and thus far, I'm loving it. BUT I do need some guidance. I'm attempting to write a program so solve an equation. The equation is F = k q1 q2 / r 2.

Here is my program:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// Declare Variables
double Charge_1, Charge_2, Meters, Force;
const double C_Constant = 8.9988 * 10e9;

// Input Section
cout << "What is the charge of particle 1 in Coulombs ?" ;
cin >> Charge_1;
cout << "What is the charge of particle 2 in Coulombs ?" ;
cin >> Charge_2 ;
cout << "What is the distance between particle 1 and particle 2 in meters ?" ;

//Processing Section
Force = C_Constant * Charge_1 * Charge_2 / Meters^2;

//Output Section
cout << " Charge of Particle 1: " << Charge_1 << endl;
cout << " Charge of Particle 2: " << Charge_2 << endl;
cout << " Force: " << Force << endl;
return 0;
}

I keep getting this error: "invlaid operands of types 'double' and 'int' to binary 'operator^'.
In cpp ^ does not get the power of two numbers. To square two numbers I'd suggest explicitly multiplying it by itself.
So I would literally have to write out nine 10s? Are you sure there is not another way? And what is my error telling me? Thanks in advance.
or input cmath library --- #include<cmath> and write force = c_constant*charge1*charge2 / pow(meters,2);
pow is standart function which returns double ....your error said that your language haven't " ^ " operand
Last edited on
Don't use pow to square something. It's overkill -- like driving a nail with a sledgehammer.

To square something, just say x*x:

 
Force = (C_Constant * Charge_1 * Charge_2) / (Meters * Meters);


pow() is for more complex exponent operations.


Also this little bit is more verbose than it needs to be (and actually I suspect it's wrong):

1
2
3
const double C_Constant = 8.9988 * 10e9; // <- this
// can be reduced to this:
const double C_Constant = 8.998e9;


That's what the 'e' suffix is for. It multiplies the preceeding numerical constant by 10x

Also note that the above produces 2 different constants:

1
2
8.9988 * 10e9 = 89988000000
8.9988e9      = 8998800000


I assume you wanted the latter.

Remember, in C, 10e1 is 100, not 10
Last edited on
The error you getting is telling you that the operator "^" can not take an int and double. "^" is the XOR operator.
Oh, okay I see now. When is it appropriate to use " ^ " ?
When you want to conduct the bitwise XOR logical operation.

http://www.cplusplus.com/doc/tutorial/operators/

For squaring, you could do
1
2
3
4
5
template<typename T>
inline auto square(T number) ->decltype(number*number)
{
    return number*number;
}


(or, the C++ 2003 version):

1
2
3
4
5
template <typename T>
inline T square(T number)
{
    return number*number;
}


It doesn't actually save you any writing work, but I find that it makes things easier to read sometimes (especially when using larger expressions).
Last edited on
How would I go about changing the output to not be in scientific notation? For example, making the answer "Force" be printed to a fixed format using 5 digit precision.
1
2
3
4
5
#include <iomanip>

//...

cout << fixed << setprecision(5) << mynumber;


Other items of interest:

http://cplusplus.com/reference/iostream/manipulators/
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
// Declare Variables
double Charge_1, Charge_2, Meters, Force;
const double C_Constant = 8.9988e9;

// Input Section
cout << "What is the charge of particle 1 in Coulombs? " ;
cin >> Charge_1;
cout << "What is the charge of particle 2 in Coulombs? " ;
cin >> Charge_2 ;
cout << "What is the distance between particle 1 and particle 2 in meters? " << endl;
cin >> Meters;

//Processing Section
Force = (C_Constant * Charge_1 * Charge_2) / Meters * Meters ;

//Output Section
cout << " Charge of Particle 1: " << Charge_1 << " Coulombs" << endl;
cout << " Charge of Particle 2: " << Charge_2 << endl;
cout << " Distance between both particles: " << Meters << endl;
cout << " Force: " << Force << endl;
cout << " Force: " << setprecision (5) << Force << endl;
cout << " This is how we doooooo it!\n";
return 0;

Yeah, I did that but it still comes out as scientific notation. I'm trying to achieve two forms of the answer. One in scientific and one in a 5 fixed digit precision form. Do I need to alter something else?
You forgot <<fixed

Also, use [ code ] [ /code ] tags around your code, it makes it a lot easier to read.
Topic archived. No new replies allowed.