Why does this program close when only one of the parameters is met?

I wrote this program for an assignment last week. I did a little extra for my own amusement and understanding and had the program calculate negative powers as well. For some reason I don't understand, if you enter -1 as a power the program exits the while loop even if the base is not a -1??? Why is that?

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
#include <cstdio> 
#include <iostream>
using namespace std;


float daPower(float rootBeer,int heMan) {    // Masters of the Universe fan?    http://www.youtube.com/watch?v=-dJolYw8tnk

	if (heMan == 0) return 1;			// zero power
	if (rootBeer == 0)return 0;			// 0 float case
	if (heMan == -1) return 1/rootBeer;
	if (heMan < 0) return 1/(rootBeer * daPower(rootBeer ,(heMan * -1)-1));  //  negative power for my amusement
	if (heMan > 1 )return rootBeer * daPower(rootBeer , heMan - 1);
	if (heMan == 1) return rootBeer;
	return 0;
}

int main()
{
	float rootBeer = 0;
	int heMan = 0;

	cout << "Welcome to the da Power program.";
	
	cout << endl << "Enter a float value x and integer power value y (-1 -1 to exit):";

	cin >> rootBeer >> heMan;

	while (rootBeer != -1 && heMan != -1) {		// makes the program loop 

	//	if (rootBeer == -1 && heMan == -1) break;

		cout << endl << "value is:" << daPower(rootBeer,heMan);
	
		cout << endl << "Enter a float value x and integer power value y (-1 -1 to exit):";

		cin >> rootBeer >> heMan;
	}// while while (rootBeer != -1 && heMan != -1)


	cout << endl << "Thank you for using the da Power program.";

	getchar();

}// main 
while (rootBeer != -1 && heMan != -1)

The while loop will keep going as long as rootBeer is not -1 AND heMan is not -1.

You say that if you set one of them to -1, the while loop ends? Seems correct to me.

I don't understand. That seems like an OR condition to me. Why doesn't it have to meet both conditions before exiting?
Last edited on
&& gives you true if both sides are true. otherwise it gives you false. So in order for "x && y" to be true, both x AND y must be true.

Loops continue looping as long as their loop condition is true.

Therefore... while (rootBeer != -1 && heMan != -1) tells the compiler "keep looping as long as rootBeer isn't -1 AND heMan isn't -1". As soon as either one of them becomes -1, then the AND condition becomes false, and the loop exits.



EDIT: to emphasize / clarify...

loop conditions are the condition for LOOPING, not for exiting. If you want to think of it as an exit condition instead, put the condition inside a ! operator:

1
2
3
while( condition_to_keep_looping )
// or...
while( !(  condition_to_exit ) )


So if you want it to EXIT when both rootBeer and heMan are -1, then that's an exit condition:

while( !( rootBeer == -1 && heMan == -1 ) )
Which of course is the same as the following:
while( rootBeer != -1 || heMan != -1 )
Last edited on
That made it click. Thank you.
Topic archived. No new replies allowed.