• Steam recently changed the default privacy settings for all users. This may impact tracking. Ensure your profile has the correct settings by following the guide on our forums.

Visual Studio, C help

tave

New Member
Hey guys, ive just taken up a programming course, and i cant figure out whats wrong with this code, could someone please shed some light on this? After I input the angle, the program closes....it doesnt give me the output(range).
I am using microsoft visual studio to debug/compile the code

thanks!

/*Author: ------------
Date: Sept 12, 2010
Purpose: calculate the range given velocity and angle of launch
*/

#include <stdio.h>
#include <math.h>
#define ACCEL_GRAV 9.81

int main( void )
{
double initialspeed;
double range;
double angle;

printf( "Input Initial speed m/s \n" );
scanf( "%lf", &initialspeed );

printf( "Input Angle in (radians)\n" );
scanf( "%lf", &angle );

range = ( initialspeed * initialspeed )
/ ACCEL_GRAV * sin( 2 * angle );

printf( "Final Range Is: %f ", range );

return 0;

}

btw: the forum kinda ruined my indentations
 

Moose

Meta Moose
I'm no C programmer, but have you tried making the range calculation happen over several lines? For example:

Code:
range = (initialspeed * initialspeed);
range = range / ACCEL_GRAV;
range = range * sin(2 * angle);
 

MenaceInc

Staff Member
You can keep your code formatting by using code tags. Just use them in [. And ] tags

You aren't giving the window any chance to show output I think. I'm on my phone right now so I can't say for sure but I think if you put system(PAUSE); before your return then it should show your output
 

MenaceInc

Staff Member
I'm no C programmer, but have you tried making the range calculation happen over several lines? For example:

Code:
range = (initialspeed * initialspeed);
range = range / ACCEL_GRAV;
range = range * sin(2 * angle);

That shouldn't matter as long as he follows BIDMAS. Even if it didn't work, it should show garbage
 

tave

New Member
system(PAUSE) gave me an error
so i tried getchar(); to allow me to press a key before the program closes...but it still closes after typing in a range
 

MenaceInc

Staff Member
Just to clarify, is

Code:
 range = ( initialspeed * initialspeed )
/ ACCEL_GRAV * sin( 2 * angle );
All on the one line?

I think you need the stdlib.h header to use system() as well. Nice try using getchar() though for the same thing. Must be something in the printf parameters. Honestly though, I'm being distracted by television right now so I'll have a try at it tomorrow when I get home if someone else hasn't fixed for you xD
 

ultimakillz

Teh Fett Mawn
the problem is not with your code, its with visual studio. your program closes fast because you are running it through debug. you need to actually execute the program and it should display the "Press any key to exit....." message. i believe the shortcut for running a program is F7, but i havent used visual studio in a while so i cant remember exactly.
 

tave

New Member
ultimakillz, thanks so much
to get it to work properly, i went to
Build --> Profile Guided Optimization --> Run Intrumented/Optimized Application
 

ultimakillz

Teh Fett Mawn
hm... you shouldnt have had to modify any options, just actually execute the program instead of run it in through debug. there should have been an option under one of the menus to simply execute the program. regardless, im glad you got it working :D
 

tave

New Member
what should i be scanning instead?
and what exactly is a long float? i dont think ive learnt that far yet, but it would be nice to know!
 

slicer4ever

Coding random shit
a long float is pretty much a double, since a float is generally 4 bytes, then a long float is 8 bytes, and a double is 8 bytes
 

SilverSpring

New Member
the problem is not with your code, its with visual studio. your program closes fast because you are running it through debug. you need to actually execute the program and it should display the "Press any key to exit....." message. i believe the shortcut for running a program is F7, but i havent used visual studio in a while so i cant remember exactly.

No F7 is to compile the code but not run it. The actual shortcut is Ctrl+F5 which will run without debugging (F5 by itself is run with debugging). Ctrl+F5 will run the program and then display the "Press any key to continue..." message so no need for any getchar() calls.

Note: this is with Visual Studio 2010 but I'm pretty sure all versions use the same shortcuts (IIRC anyway, I've been using VS since the first .net version which was 2002, then VS2003, VS2005, VS2008, and now VS2010)
 
Top