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);
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.
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
constunsigned 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:
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
#include <iostream>
#include <iomanip> //removed this
usingnamespace 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.