help with program

I have tried multiple times to get this program to work. I have researched the web and I am coming up with nothing.

#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
//function prototype
double fallingDistance(int t = 0);

int _tmain(int argc, _TCHAR* argv[])
{
int t = 0; //time
double d = 0; //distance


std::cout << "I will calculate the distance an object falls in 12 seconds\n";

for (int i = 1; i <= 12; i++)
{
d = fallingDistance(i);
std::cout << i << "seconds " << d << "feet \n";
}
return 0;

double fallingDistance(int t);
{
double d = 0;
const double g = 32.2;

d = (0.5*g)*(pow(t, 2));
return d;
}
}


I am getting the following errors:

Warning 1 warning C4244: 'return' : conversion from 'double' to 'int', possible loss of data c:\users\13891\desktop\cosc\lab 06 c++\lab 06 c++\lab 06 c++.cpp 32 1 Lab 06 C++
Error 2 error LNK2019: unresolved external symbol "double __cdecl fallingDistance(int)" (?fallingDistance@@YANH@Z) referenced in function _wmain C:\Users\13891\Desktop\COSC\Lab 06 C++\Lab 06 C++\Lab 06 C++.obj Lab 06 C++
Error 3 error LNK1120: 1 unresolved externals C:\Users\13891\Desktop\COSC\Lab 06 C++\Debug\Lab 06 C++.exe 1 1 Lab 06 C++

Pull the function definition for fallingDistance outside of the main function. Also, in the definition, you don't want the semicolon at the end of double fallingDistance(int t);
If you're using a decent indentation style you should easily see that you're trying to define a function inside another function which is not allowed in C++.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
//function prototype
double fallingDistance(int t = 0);

int _tmain(int argc, _TCHAR* argv[])
{
    int t = 0; //time
    double d = 0; //distance


    std::cout << "I will calculate the distance an object falls in 12 seconds\n";

    for(int i = 1; i <= 12; i++)
    {
        d = fallingDistance(i);
        std::cout << i << "seconds " << d << "feet \n";
    }

    return 0;
    // The following code must be moved outside of the closing brace of main().
    double fallingDistance(int t);
    {
        double d = 0;
        const double g = 32.2;

        d = (0.5 * g) * (pow(t, 2));
        return d;
    }
}



Like so:

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

//Function Prototype
double fallingDistance(int t = 0)

int _tmain(int argc, _TCHAR* argv[])
{
int t = 0; //time
double d = 0; //distance


std::cout << "I will calculate the distance an object falls in 12 seconds\n";

for (int i = 1; i <= 12; i++)
{
d = fallingDistance(i);
std::cout << i << "seconds " << d << "feet \n";
}
return 0;
}
double fallingDistance(int t)
{
double d = 0;
const double g = 32.2;

d = (0.5*g)*(pow(t, 2));
return d;
}

I am getting these two errors:

Error 1 error C2144: syntax error : 'int' should be preceded by ';' c:\users\13891\desktop\cosc\lab 06 c++\lab 06 c++\lab 06 c++.cpp 12 1 Lab 06 c++
2 IntelliSense: expected a '{' c:\Users\13891\Desktop\COSC\Lab 06 c++\Lab 06 c++\Lab 06 c++.cpp 12 1 Lab 06 c++
Please edit your post and add code tags (highlight the code and then press the <> icon to the right of the edit window.

From the error messages it looks like your missing a semicolon somewhere.
Last edited on
Topic archived. No new replies allowed.