simple if question

Started by
28 comments, last by pbivens67 9 months, 3 weeks ago

how do I get my variable y to stay at 49.975 when it reaches y≥49,975.

	y = parabola[i];
	i++;
	cout << y << endl;
	if (y >= 49.975f)
	{
		y = 49.975f;
	}
Advertisement

Wrap all of it in an while loop and only execute it all if y is less than or equal to your amount. Then set it to that amount just in case it goes over.

i = 0;
y = parabola[i]

while (y <= 49.975f) {
   cout << y << endl;
   i++;
}
y = 49.975f;

Make sure to set the Y value at the end to your desired amount unless you are adding 0.001 to Y.

well I coded your code but it goes into an infinite loop

	i = 0;
	y = parabola[i];
	while (y <= 49.975f)
	{
		cout << y << endl;
		i++;
	}
	y = 49.975f;

I apologize, running off hardly any sleep right now. Put the second line within the while loop.

i = 0;

while (y <= 49.975f)
{

	y = parabola[i];
	cout << y << endl;
	i++;
}

y = 49.975f;

If you get another error, it might also help if you could send all the code or at least the code pertaining to this in specific. The problem with my original “solution” was that it was only setting “y” equal to “parabola[i]” once, and so therefore it would always be equal to that amount, causing an infinite loop.

It looks like behavior of the variables changes when y reaches that special value.

One way to do this is by keeping track what behavior the variables should do.

In practice that means you add a new variable, say “state”, and if it's 0 y can change how it wants. When y reaches your special value, you toggle your state to 1 which means y must stay the same. Thus

  • Create a new variable “state” and initialize it to 0 (which means y can do what it wants)
  • At every point where y changes you first check “state” and if it's 0, y may change otherwise it may not.
  • When y has changed to at or above your special value you set state to 1 (which means y should not change anymore). That should happen then because of the additional “state == 0” check at the previous point around every y change.

I am getting a vector subscript out of range error.

void count()
{
	for (float x = 22.5f; x >= -22.5f; x--)
	{
		y = (-0.1f * (x * x)) + 50;
		parabola.push_back(y);
	}
}

void jump()
{
	i = 0;
	while (y <= 49.975f)
	{
		y = parabola[i];
		cout << y << endl;
		i++;
	}
	y = 49.975f;

	if (direction_x == -1)
	{
		x--;
	}
	if (direction_x == 1)
	{
		x++;
	}
	glutPostRedisplay();
}

pbivens67 said:
I am getting a vector subscript out of range error.

Phil, what have you tried to find the error? Have you tried to find the error?

-- Tom Sloper -- sloperama.com

I am going to stub out my code to find the error

Well I have coded all of Alberths advice, Let me know what you think of my stubbed out code.

#include <iostream>
#include <vector>

using namespace std;

vector <float> parabola;
float i = 0.0f, y = 0.0f;
int state = 0;

void count()
{
	for (float x = 22.5f; x >= -22.5f; x--)
	{
		y = (-0.1f * (x * x)) + 50;
		parabola.push_back(y);
	}
}

int main()
{
	for (int j = 0; j < 46; j++)
	{
		count();
		if (state == 0)
		{
			y = parabola[i];
			i++;
		}
		if (y >= 49.975f)
		{
			state = 1;
		}
		if (state == 1)
		{
			y = 49.975f;
		}
		cout << y << endl;
	}
	return 0;
}

This topic is closed to new replies.

Advertisement