Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

c++ simple Physics


ghost's Avatar
0 0

I am working on a phycis cal. i am have some trouble with it this my first program way form the safety of the tuts. i have been planning it and trying to think ahead and i got

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int calculate(int v, int d, int t) {
   int null = 0;
   cout << "" << endl; 
   cout << "" << endl;
   cout << "" << endl;
   if(v == null) // checks if the varible "v" has a value
   { 
     v=d/t;
     cout << v  << endl;
   }  
   if(d == null) // checks if the varible "d" has a value
   {
     d = v*t;
     cout << d << endl;
   } 
   if(t == null) // checks if the varible "t" has a value
   {
     t = d/v;
     cout << t << endl;    
   }       
   if(v == d/t) // checks for errors in the varibles if they are not equal
   {
   cout << "All varibles sloved for!" << endl;  
   }
   else if(v != d/t);
   {
    cout << "Error in varibles please Recheck!" << endl;
   }
   return 0;     
}


int main(int argc, char *argv[])
{
    cout << "Welcome to the Motions Physics calculator  Ver. 1.0" << endl;
    cout << "INSTRUTIONS: Input each varible in specfied spot if u are solving for a varible put '0'" << endl;
    cout << "By ::zer0pain::" << endl;
    
    cout << "" << endl;
    /* This is a list of the varibles in the motion equations */ 
    int avgspeed;
    int distance;
    int time;
        
    cout << "Enter the Avg. Speed in meters/second" << endl;
    cin >> avgspeed; 
    
    cout << "Enter the Distance in meters" << endl;
    cin >> distance;
    
    cout << "Enter the Time in seconds" << endl;
    cin >> time;
    
    calculate(avgspeed, distance, time);
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

when u run this code ur self u will notice to thing it will not solve for time (or t) and when u have valus that v=d/t it still echos the error i have madeā€¦ ty for your time zer0pain


ghost's Avatar
0 0
#include <iostream>
#include <string>
using namespace std;

int calculate(double v, double d, double t) {
static int null = 0;

cout << "\n\n\n" << endl;

if(v == null) // checks if the varible "v" has a value
{
v=d/t;
cout << v << endl;
}

if(d == null) // checks if the varible "d" has a value
{
d = v*t;
cout << d << endl;
}

if(t == null) // checks if the varible "t" has a value
{
t = d/v;
cout << t << endl;
}

/********************************************************************************
 * Can't say if this is really nescessary. Only causing problems at this point. *
 ********************************************************************************
 
if (v == (d/t)) // checks for errors in the varibles if they are not equal
{
cout << "All variables solved for!" << endl;
}

else if(v != (d/t))
{
cout << "Error in variables please Recheck!" << endl;
}
*/

return 0;
}


int main(int argc, char *argv[])
{
cout << "Welcome to the Motions Physics calculator Ver. 1.0" << endl;
cout << "INSTRUCTIONS: Input each variable in specfied spot if you are solving for a varible put '0'" << endl;
cout << "By ::zer0pain::" << endl;

cout << "" << endl;
/* This is a list of the varibles in the motion equations */
double avgspeed, distance, time;

cout << "Enter the Avg. Speed in meters/second" << endl;
cin >> avgspeed;

cout << "Enter the Distance in meters" << endl;
cin >> distance;

cout << "Enter the Time in seconds" << endl;
cin >> time;

calculate(avgspeed, distance, time);

system("PAUSE");
return EXIT_SUCCESS;
}

I changed the int to double type variables, in order to provide more accurate answers, and solve several problems with your program crashing on my machine.

I've commented out the part that does your error checking on the calculations. The reason behind my logic for doing so is because with floats, you won't always have a perfectly correct answer because of rounding. If your manipulations of data are correct, you shouldn't have to recheck them.

I've also done a few things (Such as making int null, static int null in order to keep it as one definition) to optimize your code a little bit. Good luck, and reply if you need any other help/explanations.