Program to calculate Force btw 2 objects

February 18, 2011 at 11:19:03
Specs: Windows 7
I am also new to C++. This is my first class, second program. I'm a math major and this is a requirement. This assignment was to write a program that calculates the force between 2 objects. My program is to prompt the user for m1, m2, initial distance apart, end distance apart t. I need to utilize the Read Double function since we just went over that in the prior class. Additionally, I need to increment my program to provide an output table that displays the change in force as the distance changes. We've also recently discussed the boolean function, but I don't know if it needs to (or can be) implemented into this program.

Here's what I have: (By the way, if anyone knows how to copy text out of Nano that would be lovely to know).

# include <iostream>
# include <cmath>
# include <string> <-- do I need this?
using namespace std;
const double G=6.673 * pow(10, -11) <-- can I do that?
double readDouble (string prompt)
double Force (double m_1, double m_2, double r, G);

double Force (double m_1, double m_2, double r, G)
{
return m_1*m_2*G/(pow(r, 2.0));
}

double readDouble (string prompt)
{
bool done=False;
while (done=False)
{
cout<< "Enter mass of first object";
cin>>m_1;
cout<< "Enter mass of second object";
cin>> m_2;
cout<< "Enter initial distance between object";
cin>>R_start;
cout<< "Enter final distance between objects";
cin>>R_end;
if (cin.fail ()!=0
{
cout<<"Errot!"<<endl;
cin>>clear();
cin.ignore(INT_MAX, '\n');
}
else
done=true;
}
return 0;
}


I'm sure this is atrocious. Any guidance would be greatly appreciated.


See More: Program to calculate Force btw 2 objects

Report •


#1
February 18, 2011 at 13:11:58
# include <string> <-- do I need this?
As long as double readDouble (string prompt) calls for a std::string, yes. (std::string is better practice than cstrings, anyways.)

const double G=6.673 * pow(10, -11) <-- can I do that?
Gcc just let me do that, yeah.

if anyone knows how to copy text out of Nano that would be lovely to know
I don't think nano interfaces with clipboards. What you'll do depends on what you want to do. Use xclip if you want to use the Linux/X Windows clipboard, get a better terminal program if you want to use Window 7's clipboard. I like PuTTY.

while (done=False)
You're assigning done to False (whatever that is; I'd expect false or maybe FALSE), not comparing the two.

if (cin.fail ()!=0
You're missing a right parenthesis.

How To Ask Questions The Smart Way


Report •

#2
February 18, 2011 at 13:39:09
Thank you for such a quick response. Now I'm going to try to make sense of it all. Thank you again!

Report •
Related Solutions


Ask Question