Help with Program

I'm new to C++ and I am having trouble getting my program to compile. I need HELP!!! Here is the assignment: Write a C++ program that computes how many meters an object falls in 1 second, 2 seconds, etc., up to 10 seconds.
Sample Output (This program has no input)
Seconds Distance
==============
1 4.9
2 19.6
3 44.1
4 78.4
5 122.5
6 176.4
7 240.1
8 313.6
9 396.9
10 490


This is what I have done so far, but my Professor is telling me that This assignment requires a loop in the main program and the table output should be done in the main program as well. The function should only compute the distance fallen.

#include <iostream>
#include <iomanip>
using namespace std;


double FallingDistance(double);



int main()
{
double Count = seconds;
double Seconds = 1;
double Distance = 0;
{
for (double Seconds = 1; Count < seconds; Seconds = Seconds + 1);}
cout << "This table computes how many meters an object falls in seconds." << endl;
cout << "Seconds Distance\n";
cout << "\n=================\n";
{
FallingDistance=FallingDistance(Seconds);

cout << fixed << showpoint << setprecision(2);
cout << Seconds << " " << FallingDistance << endl;
}

}

double FallingDistance (double Seconds);
{
double Distance = .5 * 9.8 * Seconds * Seconds;
return Distance;
}
This probably has problems compiling because you haven't defined the variable FallingDistance.. I wouldn't have to guess if you wouldn have posted your compiler's output.
This is the output from the compiler.

1>------ Build started: Project: Lab6 C++, Configuration: Debug Win32 ------
1>Compiling...
1>Lab6 C++.cpp
error C2065: 'seconds' : undeclared identifier
error C2065: 'seconds' : undeclared identifier
error C2659: '=' : function as left operand
error C2447: '{' : missing function header (old-style formal list?)

1>Lab6 C++ - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
aim1984

Please use the code tags - the <> button on the right.

1
2
{
for (double Seconds = 1; Count < seconds; Seconds = Seconds + 1);}


With this part the braces are not needed, and neither is the semicolon. Do not use doubles in for loop conditions, use ints and cast them to doubles inside the loop. Here is what it could look like:

1
2
3
4
5
6
7
const unsigned SIZE = 10

for (int Count = 0;  Count < LIMIT; Count++ ) { //do the code in braces 10 times
//your code here


}


Your function definition should look like this - no semicolon:

1
2
3
4
double FallingDistance (double Seconds) {
double Distance = .5 * 9.8 * Seconds * Seconds;
return Distance;
} 


There we go - some clues but not all of them.

Hope all goes well.



@aim1984

Here's some of your errors..
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
34
35
36
37
#include <iostream>
#include <iomanip>
using namespace std;


double FallingDistance(double);



int main()
{
double Count = seconds;  // Not needed plus you are using a variable that wasn't initialed or defined, yet
double Seconds = 1;// Seconds should be an int
double Distance = 0;
{
for (double Seconds = 1; Count < seconds; Seconds = Seconds + 1);}// Remove the right curly brace
                                                                                         //and semi-colon
   // For loop called wrong also. Should be 'for(Seconds=1;Seconds<11;Seconds++)
cout << "This table computes how many meters an object falls in seconds." << endl;// These 3 lines
                              // should be put BEFORE the for loop, otherwise they get printed each time
cout << "Seconds Distance\n";
cout << "\n=================\n";
{
FallingDistance=FallingDistance(Seconds);// Cant have a variable name the same as a function name
                                    // You probably meant 'Distance = FallingDistance(seconds); 

cout << fixed << showpoint << setprecision(2);
cout << Seconds << " " << FallingDistance << endl; // And rename 'FallingDistance' to 'Distance'
}

}

double FallingDistance (double Seconds); // remove semi-colon
{
double Distance = .5 * 9.8 * Seconds * Seconds;
return Distance;
} 
Last edited on
I made all the changes and I still got 4 errors when I tried to compile. The out put was:

1>------ Build started: Project: Lab6 C++, Configuration: Debug Win32 ------
1>Compiling...
1>Lab6 C++.cpp

syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
lab6 c++\lab6 c++\lab6 c++.cpp(12) : error C2059: syntax error : '}'
lab6 c++.cpp(16) : error C2447: '{' : missing function header (old-style formal list?)

1>Lab6 C++ - 4 error(s), 0 warning(s)"
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Post your new code and the compiler output whenever you make changes - then we can see what's happening.
Here is the new code and I still received 4 error when I tried to compile as stated in my previous post.:

#include <iostream>
#include <iomanip>
using namespace std;


double FallingDistance(double);



int main()
}

int Seconds = 1;
double Distance = 0;
{

cout << "This table computes how many meters an object falls in seconds." << endl;
cout << "Seconds Distance\n";
cout << "\n=================\n";
{

for(Seconds=1;Seconds<10;Seconds++)

Distance=FallingDistance(Seconds);

cout << fixed << showpoint << setprecision(2);
cout << Seconds << " " << Distance << endl;
}

}

double FallingDistance (double Seconds) {
double Distance = .5 * 9.8 * Seconds * Seconds;
return Distance;
}
Please use Code Tags - the <> button on the right. So it looks like this:

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
34
35
36
37
38
39
#include <iostream>
#include <iomanip>   //removed this
using namespace std;  //removed this

using std::cin;   //these are better than using namespace std
using std::cout;
using std::endl; //put std:: before any other std thing

double FallingDistance(double); //function declaration

int main() { //fixed the brace
}

    int Seconds = 1;
    double Distance = 0.0;


    cout << "This table computes how many meters an object falls in seconds." << endl;
    cout << "Seconds Distance\n";
    cout << "\n=================\n";
{  //removed brace

    for(Seconds=1;Seconds<10;Seconds++) { //added this brace
         Distance=FallingDistance(Seconds);

         cout << fixed << showpoint << setprecision(2); //did some indenting
         cout << Seconds << " " << Distance << endl;
    }       //now this runs through each iteration of the for loop

return 0; 

}//end of main function

double FallingDistance (double Seconds) { //function definition

     double Distance = 0.5 * 9.8 * Seconds * Seconds;
     return Distance;
} 



There were a few problems with the braces, IF you are using an IDE (Integrated Development Environment) It should automatically do braces for you. It should also do indenting.
Topic archived. No new replies allowed.