Need Help with Looping Error for Seconds and Distance

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."

Thank you for in advance for helping!

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//This program will compute how many feet an object
//falls in 1 second, 2 seconds, etc., up to 12 seconds.
#include <iostream>
#include <iomanip>
using namespace 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);
	}
line 28for (Seconds = 1; Seconds <= numSeconds; Seconds++);

numSeconds doesn't have the desired value.
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!
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
40
41
42
#include <iostream>
#include <iomanip>
using namespace std;

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;
}
	
double distanceFallen(int numSeconds, double FallingDistance) {
	return (32.2 * numSeconds * numSeconds * 0.5);
}

int main()
{
	int Seconds, numSeconds, secondsfalling = 0;
	double FallingDistance = 0;

	cout << "This program will compute how many feet an object\n";
	cout << "falls in 1 second, 2 seconds, etc., up to 12 seconds.\n";


	Seconds = getSeconds();


	cout << "\nSeconds  Distance\n";
	cout << "-----------------\n";
		

	for (Seconds = 1; Seconds <= numSeconds; Seconds++); //here numSeconds == 0 !!!
	{
		FallingDistance = distanceFallen(Seconds,FallingDistance); 

		cout << fixed << showpoint << setprecision(4); 
		cout << Seconds << setw(15) << FallingDistance << "\n"; 
	}

return 0;
}
On line 28 for (Seconds = 1; Seconds <= numSeconds; Seconds++);
you have ; at the end. Remove it and your loop should start increment properly.

On line 21 Seconds = getSeconds();
I think you are accidentally getting seconds to Seconds instead of numSeconds.

Fixing those made the loop working.

Also, your int secondsfalling = 0; is not used anywhere in your code.
Last edited on
Topic archived. No new replies allowed.