d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > What Did I Do Wrong? > C++ Problem
Add Reply New Topic New Poll
Member
Posts: 2
Joined: Sep 19 2012
Gold: 0.00
Sep 19 2012 12:56am
I'm working on an assignment and I cant tell what I'm doing wrong. could someone help me out? I'm very new to C++ This is my second program

Code
Program #1

According to Einstien's Special Theory of Relativity, time passes slower with reference to a moving object. The faster you go, the slower time gets from your reference point. He also theorized that if you travel the speed of light, time stops and if you go faster, you can go back in time (he also proved you cannot go the speed of light). But, you can approach the speed of light.


Use the following algorithm to write a program that gives us the values of time passing on a moving clock vesus a stationary clock (declare everything as a double).
 
       insert your class header info
       include pre-processor directives (along with <iostream>, you will need to #include <cmath> for the sqrt())
       declare speed of light constant const double c= 299792458
       main()
         declare identifiers
  prompt for time of travel (tm)  
  read time on moving clock
         check for valid input (time of trip > 0)
  prompt for velocity (v)
         read velocity (a percent of light speed. a number between 0 and 100)
         check for valid input (velocity > 0 and less than 100)
  Make sure that after you enter the velocity (v), do the following equation
 
 v = (v / 100) * c;//this changes the velocity to a percent of light speed

  calculate time on stationary clock using the following

 ts = tm/(sqrt(1-(v*v)/(c*c)));

 where
 ts = time of stationary clock
 tm = time of moving clock
 v = velocity (remember, this is a % of light speed so make sure you multiply v * c)
 c = speed of light

         print results (see below)
         prompt the user to enter another set of data. If the user enters 'y' do it all over again. If he enters 'n', stop             processing
        loop for another set of data
        pause
        return
        end of main()


and this is what I have so far
#include <iostream>
#include <cmath>
using namespace std;
const double c= 299792458;


int main()
{

double ts, tm, v, vv, c;
c=299792458;
cout << "Please enter time of travel: ";
cin >> tm;

cout << "Please enter velocity: ";
cin >> v;


ts=(tm/(sqrt(1-(vv *vv)/(c*c))));

vv =(v/100)*c;


cout << "the time of the stationary clock is: << "ts " <<endl;

system("pause");
return 0;
}

What am I missing here?
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 19 2012 01:55am
cout << "the time of the stationary clock is: << "ts " <<endl;


"ts "

so you want to output the words "ts" instead of outputting the variable ts?


(tm/(sqrt(1-(vv *vv)/(c*c))));

edit: maybe its just me but shouldn't this look like


(tm/[sqrt(1-[(vv*vv)/(c*c)])]) (used square brackets for emphasis)

seems like you were missing one or something, at least to me (mathematically speaking)

This post was edited by Eep on Sep 19 2012 02:04am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Sep 19 2012 07:15am
your cout line is missing a ending quotation and your variable is inside quotations.
Member
Posts: 2
Joined: Sep 19 2012
Gold: 0.00
Sep 19 2012 02:48pm
So I've modified it a bit to this point. however whenever the ts and tm both come out the same. I also cant get the program to repeat. Ideas?

#include <iostream>
#include <cmath>
using namespace std;
const double c= 299792458;

int main()
{

double ts, tm, v, vv, c, x, yes, no;
if (x=1)
{
c=299792458;
cout << "Please enter time of travel: ";
cin >> tm;

cout << "Please enter velocity: ";
cin >> v;
if (v<0)
{if (v>100)
{ cout << "Error : Invalid input";}}

v =(v/100);
ts=((tm/(sqrt(1-((vv*vv)/(c*c))))));
vv=(v*c);


cout <<"the time of the stationary clock is" << ts;
cout << "Enter another Value?" ;
cin >> x;
}
else
cout << "Goodbye!";
system("pause");
return 0;}
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Sep 19 2012 04:15pm
You're declaring 2 variables called "c". One as a constant, and the other in the main body. To make a loop, you need to use: while (x=1), not if (x=1).

"ts" and "tm" have the same values because the variable "vv" does not have a value when you use it.
Code

ts=((tm/(sqrt(1-((vv*vv)/(c*c))))));

lets say the variables values are these:

TM = 5
c = 10
vv = 0 //because you did not assign any value to it before using, so im assuming it took 0, i dont know how the language behaves in this case

so, with this, we have:

ts = ((5/(sqrt(1-((0*0)/10*10))))));

zero divided by any number is zero, so 1-0 is 1, and square root of 1 is 1, so your TS variable will be TM divided by 1, wich is TM itself.

This post was edited by StormHasHe on Sep 19 2012 04:23pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll