program loop doesn't work correctly

I'm trying to compile a looped program that calculates how far a ball bounces off the ground, but I want it to break out of the loop once the distance becomes less than 0.5 inches. For some reason the program is compiling fine, but when I run it the lowest distance shown is 0.3125... Here is my code:

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
#include <iostream> 
using namespace std; 
  
int main() 
{ 
  double height = 40; 
  
  int i; 
  i = 0; 
  while (i < 10) 
  { 



    if ( height > 0.5)
    height = height / 2; 
    cout << "location is now: " << height << " inches" << endl; 
    i = i + 1; 

    if ( height < 0.5) break;



  } // while 
  
  return 0; 
} // main 


Any advice would be appreciated. Thanks!
It's because you're not using scope resolution operators, or SRO, and your indentation doesn't help either.
These lines:
1
2
3
4
    if ( height > 0.5)
    height = height / 2; 
    cout << "location is now: " << height << " inches" << endl; 
    i = i + 1; 

Aren't indented to show the correct code. Technically your code works exactly how it's supposed to, but now, let me show you how your code is perceived by the compiler:
1
2
3
4
    if ( height > 0.5)
        height = height / 2; 
    cout << "location is now: " << height << " inches" << endl; 
    i = i + 1; 

Or:
1
2
3
4
5
6
    if ( height > 0.5)
    {
        height = height / 2; 
    }
    cout << "location is now: " << height << " inches" << endl; 
    i = i + 1; 


Everything after the { and }, or SRO, of the if statement is run everytime, regardless of the condition.

Hope this helps.
Taking account the post above you still have Math to blame. If you really want it to only display number above 0.5 you can try this. It should work.

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
int main() 
{ 
  double tmp;
  bool isValid = true;
  double height = 40; 
  
  int i; 
  i = 0; 
  while (i < 10 && isValid) 
  { 
  if (height > 0.5)
	{
		tmp = height / 2;
		if (tmp < 0.5)
			isValid = false;
		else
    		height = height / 2; 
		
		if (isValid)
			cout << "location is now: " << height << " inches" << endl; 
    	
		i = i + 1; 
	}
  } // while 
  


or this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() 
{ 
  double height = 40; 
  
  int i; 
  i = 0; 
  while (i < 10) 
  { 
  if (height > 0.5)
	{
		height = height / 2; 
		if (height > 0.5)
			cout << "location is now: " << height << " inches" << endl; 

		i = i + 1; 
	}
  } // while 
  
  return 0; 
} // main  
Last edited on
Why not just a for loop then? Your original code:
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
#include <iostream> 
using namespace std; 
  
int main() 
{ 
  double height = 40; 
  
  int i; 
  i = 0; 
  while (i < 10) 
  { 



    if ( height > 0.5)
    height = height / 2; 
    cout << "location is now: " << height << " inches" << endl; 
    i = i + 1; 

    if ( height < 0.5) break;



  } // while 
  
  return 0; 
} // main  


For loop:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream> 
using namespace std; 
  
int main() 
{ 
  double height = 40; 
  for (int i = 0; i < 10 && height > 0.5; i ++)
    cout << "location is now: " << (height /= 2)<< " inches" << endl;
  } // for
  return 0; 
} // main  


A little bit smaller. Note, the second part of a for loop is the conditional, which is the same thing as a while loop. I can't remember if you need the && there, or a comma, but I believe the && since it's conditional. However, height is divided by two and assigned to the new value before printing out.

Note: I didn't test this before posting, it might need a few tweaks.
Last edited on
I'm actually supposed to make it an event-controlled loop! And is there away I can truncate the decimal so that it only prints whole numbers? Thanks!
You can cast it to an int, or you can create a temporary int variable, int temp; works for this case, and assign it to height and display the height, or the last option is using the iomanip header and turning the decimal off.

And an event controlled loop? How is what I showed you any different than yours? The "event" is the same, there just happens to be two events in mine, while yours uses a break to exit the loop.
1
2
3
4
5
6
if ( height > 0.5)
    height = height / 2; 
    cout << "location is now: " << height << " inches" << endl; 
    i = i + 1; 

    if ( height < 0.5) break;


To be honest, I find this style is one of the worst practice
What's wrong with putting a pair of {} around the statements?
Topic archived. No new replies allowed.