Public Group

# C coding Help: dunno wuts wrong with this code!

This topic is 4373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I started writing a code and wanted to make sure i was doing it right so far so i made a random outpu after calling the flight_time() function, but i get a runtime error. I got no problem when debugging ( in VS 2003) but when i use VS 2005 i get some error: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ===this error is at two places according to VS 2005...one when i first declare flight_time() before the main function and other when i have "{" rite after the "flight_time" at the bottom of code...but in VS 2003 shows no error so im sure w/e VS 2005 is showing doesn't matter, but when i run it in, a runtime error shows up....can anyone help me figure out what's wrong??? HERE'S THE CODE(this is not a finished program, im just trying to see if this much works...):
#include "stdafx.h"
#include "math.h"
#define g 32.17
float angle_of_elevation, gun_height, estimated_velocity, actual_distance;
const flight_time();
//distance_to_impact();
//errors_in_hitting_the_target();

int _tmain(int argc, _TCHAR* argv[])
{
printf("** U.S. Military flight Trajectory Sofware **\nInstructions: Please follow the prompts belowand enter the appropriate\ninformation when prompted. Pay particular attention to the requested\nunits to ensure correct computations!\n\n");
printf("Enter the Angle of Elevation of the Gun (in radians): ");
scanf("%f", &angle_of_elevation);
printf("Enter the gun's height relative to the target (in ft): ");
scanf("%f", &gun_height);
printf("Enter the Estimated Velocity of the Shell (in ft/sec): ");
scanf("%f", &estimated_velocity);
printf("Enter the Actual Distance to the target (in ft): ");
scanf("%f", &actual_distance);

const flight_time();
}

flight_time()

{
printf("\nComputing...\n\n");
printf("Flight");
return(0);
}


Here is my output http://i6.photobucket.com/albums/y220/ap4234/Output.jpg Thanks!!

##### Share on other sites
flight_time()'s prototype does not have a return type (so "int" is assumed, and that's not legal C++). Furthermore, your braces are funky, so main does not return and you have code outside the body of any function (since your definition of flight_time is broken) and "const" is not required when calling the function.

Also, "_tmain" isn't standard C++, and you should not be using precompiled headers (#include "stdafx.h" -- remove that line and go to the project options and disable precompiled headers, they'll just cause you trouble at this stage).

In future when creating projects from Visual Studio it is best to create an "empty project" so that you don't end up with things like precompiled headers and "_tmain" generated for you (_tmain is designed to help with Unicode support; you don't need to worry about that either).

Additionally, don't use #define for compile-time constants in C++, especially not constants with identifiers like "g" (accelleration due to gravity in feet-per-second-squared, correct?).

For input, you should be using cin and for output, cout. printf() and scanf() are not safe.

// iostream contains the definitions for cout and cin, the "console out" and// "console in" objects, respectively.#include <iostream>// "const" is better for compile-time constants than #define, because it// allows for type-safety, among other things.// #define g 32.17const float gravitational_accel = 32.17f;// You don't need these as globals; you could just as easily pass them to// the flight_time() function. However, I will leave that as a challenge// for you.float angle_of_elevation, gun_height, estimated_velocity, actual_distance;// I assume you want to calculate the flight time with this function?// "const" alone does not specify a type, so we need to further clarify// with a type. Also, there is no reason for the return to be "const"// anyhow, as it will restrict you.//const flight_time()float flight_time();int main(int argc,char **argv){  // cout and cin use the << and >> operators to "put data into" and "pull  // data out of" the objects (called "streams") respectively.  // So here, we are "putting data into" the output stream, so it appears  // to the user.  std::cout << "Enter angle of elevation (radians): ";    // Note the use of "std::" in front of cout. cout (and cin) exist in the  // std (standard) namespace. A namespace is exactly what it sounds like --  // a localized space for names; it aides in preventing name conflicts  // between libraries.  // Now we "pull data out of" the input stream and put it into one of our  // variables. You can note that the direction of the "arrows" indicate the  // direction of data flow (from our input stream to our variable); this may  // help you remember.  std::cin >> angle_of_elevation;    // ...I'll leave the rest of the prompting and parsing to you...  // To call flight time, we don't require the "const" qualifier. We  // do, however, probably want to store the result of flight time.float  result_time = flight_time();  // main() should return a value. 0 indicates success.  return (0);}// Defining a function needs to include the return type, too:float flight_time(){  // You'll actually want to compute the_time, based on your globals.  // You'll eventually want to change those globals to parameters and just  // pass them to this function instead, but like I said, that's for you  // to figure out.float  the_time = 10.0f;  return (the_time);}

[Edited by - jpetrie on October 3, 2006 8:09:19 AM]

##### Share on other sites
The topic says C, and the code you posted is definately C, but the compiler is complaining about C++, so my tentative diagnosis is that you need to tell it to compile this code as C and not C++.

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 19
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007479
• ### Who's Online (See full list)

There are no registered users currently online

×