While loop won't end

I've tried 3 different ways to end the loop and none have worked.
It just scrolls.

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

bool compute(int base, int exponent, int * result)
{
	if (exponent < 0)
	{
		return(false);
	}
	while(exponent != 0)
	{
		*result *= base;
		exponent -= 1;
	}
	return (true);
}

int main(void)
{	
	bool loop = true;

   while (loop == true)
   {
	int base = 0;
	int exponent = 0;
	int result = 1;
	
	cout << "enter base:     ";
	cin >> base;
	cout << "enter exponent: ";
	cin >> exponent;
   	
	
	//if while loop is here it'll save value that was previously input
	
	
	if (compute(base, exponent, &result) == false)
	{
   	cerr << "exponent must be positive\n";
	}
	else if (loop == true)
	{
		cout << "base    : " << base << endl;
		cout << "exponent: " << exponent << endl;
	    cout << "result  : " << result << endl;
	}
	else
	{
	//	break;
	//	loop = false;
	continue;
	}
	}
	return(0);
}

Last edited on
while (loop == true)
(which can, of course, be simplified to
while (loop)



So, to stop the loop ... set loop = false somewhere!
You do nothing anywhere in your loop to end it.

continue doesn't break out of a loop, it just ends the current iteration and immediately begins the next one. Anyway, it should be obvious that that statement will never be executed.

Your commented-out lines would, for the same reasons, never be executed.

And your return(0) statement is after the end of your loop.

To exit your loop, you could:

1) Use break in a place where it would actually be executed
2) Set loop = false in a place where it would actually be executed
3) Add a return statement to your loop, in a place where it would be executed

EDIT: Also, you would do yourself a huge favour if you adopted a consistent, sensible indentation style.
Last edited on
I tried using loop = false, break, and continue under the else statement, I was just showing all three while not actually executing all 3. If I don't put it in a statement it won't loop. Else should mean if anything else is typed end the loop, but instead it just scrolls.
I tried using loop = false, break, and continue under the else statement

And as I've already said:

Anyway, it should be obvious that that statement will never be executed.

Your commented-out lines would, for the same reasons, never be executed.


Take a careful, considered look at the logic of your if ... else if ... else block, and I'm sure you'll figure out why the final else block will never actually be executed.
Last edited on
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
#include <iostream>
using namespace std;

bool compute(int base, int exponent, int * result)
{
	if (exponent < 0)
	{
		return(false);
	}
	while(exponent != 0)
	{
		*result *= base;
		exponent -= 1;
	}
	return (true);
}

int main(void)
{	
	bool loop = true;

   while (loop == true)
   {
	int base = 0;
	int exponent = 0;
	int result = 1;
	
	cout << "enter base:     ";
	cin >> base;
	
	
	
	cout << "enter exponent: ";
	cin >> exponent;
   	
   	//add this to break loop
   	if(cin.fail()) {
	   loop = false; 
	}
	
	//if while loop is here it'll save value that was previously input
	
	
	if (compute(base, exponent, &result) == false)
	{
   	cerr << "exponent must be positive\n";
	}
	else if (loop == true)
	{
		cout << "base    : " << base << endl;
		cout << "exponent: " << exponent << endl;
	    cout << "result  : " << result << endl;
	}

   }
	return(0);
}
Last edited on
Thank you so much
Topic archived. No new replies allowed.