Good evening! I'm struggling with getting my loop to increment properly. I know that it has to do with Seconds, but I'm stuck as to what I need to do to fix it. I am missing a variable (secondsFalling) from my flowchart (I incremented that on the flow chart), but not sure how it would fit in. The requirements of the assignment are: "Instead of using an output parameter, your C++ function FallingDistance should return a result of type double."
//This program will compute how many feet an object
//falls in 1 second, 2 seconds, etc., up to 12 seconds.
#include <iostream>
#include <iomanip>
usingnamespace std;
//Function Prototypes
void showintro();
int getSeconds();
double distanceFallen(int, double);
int main()
{
int Seconds, numSeconds, secondsfalling = 0; // Initialize Seconds variable to Zero
double FallingDistance = 0; // Initialize Distance variable to Zero
//Display an intro screen.
showintro();
//Get the number of seconds.
Seconds = getSeconds();
// Display the header for table of Seconds and Distance.
cout << "\nSeconds Distance\n";
cout << "-----------------\n";
// Calculate the distance the object has fallen for each second variable.
for (Seconds = 1; Seconds <= numSeconds; Seconds++);
{
FallingDistance=distanceFallen(Seconds,FallingDistance); //Convert seconds into distance.
cout << fixed << showpoint << setprecision(4); //Set the numeric output formatting.
cout << Seconds << setw(15) << FallingDistance << "\n"; // Populating the display table with values
}
return 0;
} //End of main function.
//***************************************************
//Use showintro function to display an intro screen *
//***************************************************
void showintro()
{
// Get the number of seconds needed to limit the loop.
cout << "This program will compute how many feet an object\n";
cout << "falls in 1 second, 2 seconds, etc., up to 12 seconds.\n";
}
//****************************************************
//The getSeconds function prmopts the user to enter *
//the number of seconds and then returns that value *
//as an integer. *
//****************************************************
int getSeconds()
{
int numSeconds;
cout << "Enter the number of seconds to calculate\n";
cout << "the feet an object will fall.\n";
cin >> numSeconds;
return numSeconds;
}
//**************************************************
//The distanceFallen function accepts a number of *
//seconds as an argument and returns the distance *
//needed to populate the table. *
//**************************************************
double distanceFallen(int numSeconds, double FallingDistance)
{
//Calculate the distance fallen.
return (32.2 * numSeconds * numSeconds * 0.5);
}
I've tried so many things, and I just don't understand how to get unstuck. Every example I have to reference either does not call the function within a for loop, or they don't call for the seconds entry. I know it doesn't match my Raptor, but I just don't know how to get numSeconds to increment. I also used secondsFalling in my Raptor, but I can't figure out how to integrate it beyond initializing it. I know my loop is wrong, but nothing I try fixes it. I only end up breaking it. How do I figure out the desired value?
I'm thinking that the value for numSeconds is being lost during the function call? Do I even need secondsFalling? Am I making this harder than it should be? I would really appreciate more detailed guidance. I've been trying to fix this for a day now and it's driving me crazy!
I just don't know what to do to get the desired value as noted by anup30, or conversely I don't know why my table will not increment. Help please!