 |

08-23-2009, 01:16 PM
|
|
Forum Addict
BANNED
|
|
Join Date: Jul 2009
Posts: 251
|
|
Need Quick Help
This is for an equation for the time it takes a pendlum to swing
#include <iostream>
#include <cmath>
#define PI 3.14159
#define G 9.8 //Gravity
using namespace std;
int main()
{
double l;
double e;
cout << "Enter Lengh of Rod\n";
cin >> l;
cout << "The time it takes from one swing is ";
cout << 2*PI*((l/G)^(1/2));
cin >> e;
return 0;
}
i get error; invalid operands of types `double' and `int' to binary `operator^'
HELP!!
thanks
|

08-23-2009, 10:48 PM
|
 |
When They Cry...
|
|
Join Date: Jan 2007
Location: Japan
Posts: 4,404
|
|
Re: Need Quick Help
^ is a bitwise operator. It doesn't mean powers in C++.
I'd fix it for you, but I need to go to school.
You want to be using the pow() function which I believe is part of the math.h.
http://www.cplusplus.com/reference/clibrary/cmath/pow/
__________________
Quote:
[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT
|
|

08-24-2009, 08:04 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 91
|
|
Re: Need Quick Help
Code:
/*
* pendulum_time.c
*
* Calculates the time for a pendulum to complete one swing.
*
* 8/24/09 - super_
*/
#include <stdio.h>
#include <math.h>
#define PI 3.14159
#define G 9.8
main() {
double rod_length;
double swing_time;
printf("Enter length of rod: ");
scanf("%lf", &rod_length);
swing_time = 2 * PI * sqrt(rod_length / G);
printf("Time it takes for one swing: %f\n", swing_time);
return 0;
}
Improved.
|

08-24-2009, 10:22 PM
|
 |
When They Cry...
|
|
Join Date: Jan 2007
Location: Japan
Posts: 4,404
|
|
Re: Need Quick Help
Quote:
Originally Posted by super_
Code:
/*
* pendulum_time.c
*
* Calculates the time for a pendulum to complete one swing.
*
* 8/24/09 - super_
*/
#include <stdio.h>
#include <math.h>
#define PI 3.14159
#define G 9.81
int main(int argc, char* argv[]) {
double rod_length;
double swing_time;
printf("Enter length of rod: ");
scanf("%lf", &rod_length);
swing_time = 2 * PI * sqrt(rod_length / G);
printf("Time it takes for one swing: %f\n", swing_time);
return 0;
}
Improved.
|
Improved again.
While that may be legitimate C code for some compilers, others won't like the fact that you haven't specified a data type for the main method. As for the command line arguments, it's simply good practice to place them there.
Also, I always thought that gravity was 9.81 rather than 9.8, but I suppose that won't matter too much.
__________________
Quote:
[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT
|
|

08-24-2009, 11:45 PM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 91
|
|
Re: Need Quick Help
Quote:
Originally Posted by Swan
Improved again.
While that may be legitimate C code for some compilers, others won't like the fact that you haven't specified a data type for the main method. As for the command line arguments, it's simply good practice to place them there.
Also, I always thought that gravity was 9.81 rather than 9.8, but I suppose that won't matter too much.
|
It's legit for all compilers; implicit data type is int (k&r). Command line arguments aren't to be placed there; main() means 'function main with unknown arguments', not 'no arguments'. I leave it as 'unknown arguments' so that the real entry point can still push argc, argv, and envp on the stack but I don't need to access them. The stack is cleaned after main() returns anyway 
|

08-25-2009, 07:19 AM
|
 |
When They Cry...
|
|
Join Date: Jan 2007
Location: Japan
Posts: 4,404
|
|
Re: Need Quick Help
Quote:
Originally Posted by super_
It's legit for all compilers; implicit data type is int (k&r). Command line arguments aren't to be placed there; main() means 'function main with unknown arguments', not 'no arguments'. I leave it as 'unknown arguments' so that the real entry point can still push argc, argv, and envp on the stack but I don't need to access them. The stack is cleaned after main() returns anyway 
|
What I'm saying is that you aren't exactly complying to the standard most people use, which can confuse people.
Also, many compilers will throw errors if you don't explicitly state the return type.
__________________
Quote:
[14:54:13] <&Filefragg> x9 is made of 5 glow in the dark gorilla dildos vibrating in unison.
[14:54:19] <%TDD> hot
[14:54:22] <+Aoi> Win
[14:54:23] <x9> I TOLD YOU NOT TO TELL ANYONE, DAMNIT
|
|

08-25-2009, 05:02 PM
|
|
Forum Addict
|
|
Join Date: Jul 2008
Location: Sweden
Posts: 328
|
|
Re: Need Quick Help
Quote:
Originally Posted by Swan
Also, I always thought that gravity was 9.81 rather than 9.8, but I suppose that won't matter too much.
|
pretty much of topic but, what gravity is equal to differs depending on where on the earth you are (e.g. in Sweden we use g ≈ 9.82)...
__________________
My system: Mac Pro, 2 x 2.8 GHz Quad-Core Intel Xeon, 10GB 800MHz DDR2 RAM, ATI Radeon HD 4870 512mb
Some people see the glass as half empty. Some see it as half full. I see it as a glass that's twice as big as it needs to be.
|

09-06-2009, 08:18 AM
|
|
Forum Addict
BANNED
|
|
Join Date: Jul 2009
Posts: 251
|
|
Re: Need Quick Help
Thanks Tons Guys =)
|

10-21-2009, 11:10 AM
|
|
Active Member
|
|
Join Date: Jun 2008
Posts: 117
|
|
It's legit for all compilers; implicit data type is int (k&r). Command line arguments aren't to be placed there; main() means 'function main with unknown arguments', not 'no arguments'. I leave it as 'unknown arguments' so that the real entry point can still push argc, argv, and envp on the stack but I don't need to access them. The stack is cleaned after main() returns anyway
|

10-22-2009, 12:57 AM
|
|
Member
|
|
Join Date: Dec 2008
Posts: 91
|
|
Re: Need Quick Help
Quote:
Originally Posted by clint999
It's legit for all compilers; implicit data type is int (k&r). Command line arguments aren't to be placed there; main() means 'function main with unknown arguments', not 'no arguments'. I leave it as 'unknown arguments' so that the real entry point can still push argc, argv, and envp on the stack but I don't need to access them. The stack is cleaned after main() returns anyway
|
what is your purpose?
|
 |
|