Thread: Program Help Please :)

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    54

    Program Help Please :)

    Hey Guys,

    New to programming, but slowly getting the hang of it. I'm a little stuck though right now.

    I have to create a program that calculates Orbital Velocity (VC). The User must enter a the distance to the earth (in km/Height) and the program needs to calculate VC based on an equation I have. If the user enters a number less than 320, it must be seen as invalid as the satellite would burnup up with a VC that low. So if the user enters a number less than 320, I would like the program to ask for the Height again and recalculate.

    I believe I've got everything properly defined but im running into errors, any help would be greatly appreciated. I'm quite new to this but an slowely and surely coming along! Thanks in advance!

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <math.h>
    
    void main(void)
    {
    
    float G, RE, M, VC, R, H, REDO, H_REDO, R_TWO;
    /*G=Universal Gravitational Constant*/
    /*RE=Mean Earth Radius*/
    /*M=Mass of Planet*/
    /*VC=Oribal/Circular Velocity*/
    /*R=Distance from sattelite to centre of planet*/
    /*H=Height of Orbit in Kilometers - User MUST enter this to preform calculation*/
    
    printf("Please enter the height of the satellite from Earth, in KM :\n ");
    scanf("%f", &H);
    G = 6.67E-11.00;/*Define G Value Here, before semi-colon*/
    RE = 6378000.0;/*Define RE Value Here, before semi-colon*/
    M = 5.98E24;/*Define M Value Here, before semi-colon*/
    R = RE + H;/*Define R value Here - Remeber the value of H is already stored from the user input, before semi-colon*/
    VC = sqrt(G * M / R) / 1000.00;/*Define the calculation needed to define Orbital/circular velocity, before semi-colon*/
    REDO = sqrt(G * M / R_TWO);
    R_TWO = RE + H_REDO;
    
    if (H < 320);
    {
    printf("\nThe entered value is too low causeing the satellite to slow down and burn up");
    printf("\n\nRe-enter a value greater than 320:");
    scanf("%f", &H_REDO);
    printf("\nVC: %.2fkm/s",REDO);
    }
    else
    printf("\nVC: %.2fkm/s",VC);
    
    _getch();
    
    return;
    }

    Error Messages:
    Code:
    1>c:\users\matthew\documents\visual studio 2008\projects\velocity2\velocity2\velocity2.cpp(18) : warning C4305: '=' : truncation from 'double' to 'float'
    1>c:\users\matthew\documents\visual studio 2008\projects\velocity2\velocity2\velocity2.cpp(18) : error C2143: syntax error : missing ';' before 'constant'
    1>c:\users\matthew\documents\visual studio 2008\projects\velocity2\velocity2\velocity2.cpp(20) : warning C4305: '=' : truncation from 'double' to 'float'
    1>c:\users\matthew\documents\visual studio 2008\projects\velocity2\velocity2\velocity2.cpp(22) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data
    1>c:\users\matthew\documents\visual studio 2008\projects\velocity2\velocity2\velocity2.cpp(27) : warning C4390: ';' : empty controlled statement found; is this the intent?
    1>c:\users\matthew\documents\visual studio 2008\projects\velocity2\velocity2\velocity2.cpp(33) : error C2181: illegal else without matching if

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So floats end with the letter "f", as in "6378000.0f". You're not allowed decimal points in powers-of-10, so stick with "e-11". And as they mention on line 27, you have an empty if-statement -- if statements go to the next semicolon or end of a block, and there's a semicolon right there on that same line, so that's the end of the if statement.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> void main(void)

    main returns an int, *always*.

    >> scanf("%f", &H);

    Always check the return value of scanf - it returns the number of variables successfully read, and if it returns less then you know the user entered something wrong.

    >> G = 6.67E-11.00;/*Define G Value Here, before semi-colon*/

    The trailing '.00' is not valid for constants using scientific notation.

    >> if (H < 320);

    Your compiler was telling you the problem, you just need to look more closely. Do you see a problem with that 'if' statement?

    >> scanf("%f", &H_REDO);

    And what if the user enters a bad value again? The best way to go about this is to set up a loop to get the user input. If all is well, break out of the loop, otherwise, issue an error and let the loop restart the process.

    >> _getch();

    There's no need for that. When working with console programs, run them from the command line.

    Finally, please learn to indent your code, eg:

    Code:
    int main( void )
    {
    	while( true )
    	{
    		if( false )
    		{
    			for( ;; )
    			{
    				puts( "foo" );
    			}
    		}
    		else
    		{
    			if( true )
    			{
    				puts( "bar" );
    			}
    		}
    	}
    	return 0;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    float G, RE, M, VC, R, H, REDO, H_REDO, R_TWO;
    
    ...
    
    REDO = sqrt(G * M / R_TWO);
    R_TWO is declared but never initialized prior to its first use.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Quote Originally Posted by rags_to_riches View Post
    is that frowned upon?

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    From here:
    Don't shotgun-blast all the available help channels at once, that's like yelling and irritates people. Step through them softly.
    It's called consideration for the time of those willing to provide help for free. In reality you should be asking questions of your instructor; he or she is being well-compensated to provide that. Now, if someone is already answering -- or has answered -- your question on one forum, and another goes to answer it on this forum, you have wasted the second person's time altogether! Not very considerate.

    Too many people think they are the only ones who exist...don't be one of those.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    54
    Quote Originally Posted by rags_to_riches View Post
    From here:


    It's called consideration for the time of those willing to provide help for free. In reality you should be asking questions of your instructor; he or she is being well-compensated to provide that. Now, if someone is already answering -- or has answered -- your question on one forum, and another goes to answer it on this forum, you have wasted the second person's time altogether! Not very considerate.

    Too many people think they are the only ones who exist...don't be one of those.
    Fair enough.
    I consider this forum a great learning learning tool and do not want to come off as ignorant. In the future I will keep that in mind. Thanks everyone for the feedback on the code, I'm starting to understand much better.

    Cheers,
    Matt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM