While loop won't end

Jun 11, 2020 at 1:53pm
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 Jun 11, 2020 at 1:55pm
Jun 11, 2020 at 2:03pm
while (loop == true)
(which can, of course, be simplified to
while (loop)



So, to stop the loop ... set loop = false somewhere!
Jun 11, 2020 at 2:51pm
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 Jun 11, 2020 at 2:52pm
Jun 14, 2020 at 3:54pm
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.
Jun 15, 2020 at 11:08am
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 Jun 15, 2020 at 11:11am
Jun 15, 2020 at 7:55pm
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 Jun 15, 2020 at 7:56pm
Jun 16, 2020 at 2:49am
Thank you so much
Topic archived. No new replies allowed.