issue with a loop and maybe sentinel?

My instructions were to make the program run until a sentinel was entered, in this case my sentinel was any negative number, however I want the program to imdeiatly stop if a negative is entered, and right now it keeps going; Eg. if I type -2 for base and 2 for power, it will still run and give me 4 but I want it to stop right after -2 is entered if that makes any sense.

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
#include <iostream>
using namespace std;
int exponent(int, int);
int main()
{
	int base, power, final;
	do
	{
	    cout << "This Program will continue until you enter a negative number" << endl;
        cout << "Please enter a number that you would like to be raised as an exponenet: ";
        cin >> base;
        cout << "Please enter power you would like this number to be raised to: ";
        cin >> power;
        
        final = exponent(base,power);
        
        cout << base << " to the " << power << " power = " << final << " \n " << endl;
	}while(base > 0 && power >= 0);
}

int exponent (int base, int power)
{
    if (power != 0)
        return (base*exponent(base,power-1));
    else
        return 1;
}
Change the ordering so that the condition is checked before exponent is called.

e.g.
1
2
3
4
5
6
7
8
9
10
cin >> base
cin >> power;
while (base > 0 && power >= 0)
{
     exponent(base, power);
     // ...

    cin >> base;
    cin >> power;
}
ok ill try it, I didnt know I could use a while loop for variables that didnt even have a value yet!
so I just tried it and it still runs the whole function :/
show what you did...
EDIT

I just realized I didnt see exactly what he said... I will try again lol
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
#include <iostream>
using namespace std;
int exponent(int, int);
int main()
{
	int base, power, final;
	
	cout << "This Program will continue until you enter a negative number" << endl;
	cout << "Please enter a number that you would like to be raised as an exponenet: ";
    cin >> base;
    cout << "Please enter power you would like this number to be raised to: ";
    cin >> power;
	while(base > 0 && power >= 0);
	{
	    
	    final = exponent(base,power);
	    
	    cout << base << " to the " << power << " power = " << final << " \n " << endl;
	    
        cout << "Please enter a number that you would like to be raised as an exponenet: ";
        cin >> base;
        cout << "Please enter power you would like this number to be raised to: ";
        cin >> power;
        
	}
    return 0;
}

int exponent (int base, int power)
{
    if (power != 0)
        return (base*exponent(base,power-1));
    else
        return 1;
}


ok so this is what I have now but its even more of a mess than before. even if I got this to work, if I entered a negative base It would still ask me for a power value and I need it to stop right there
right, follow the code top to bottom ...
cin base
//nothing in here to prevent going forward.
cin power

so you can do something roughly like

cin base
if(base >= 0)
{
cout enter power
cin power
}
while(... etc

its still a little messy. You have the get base and power code twice.
a little cleaner is to move back toward your original approach:

1
2
3
4
5
6
7
8
9
10
do
{
cout enter base //read this regardless, first time, and each time after 
cin base  
if(base >= 0)  //but if they entered a negative, don't do anything else at all
  {get power
  exponent(..)
  cout etc
  }
} while(base >= 0) //stops when it is negative 

and the only repeated thing in that idea is testing base >= twice, which is about as clean as it gets without being too weird.
you can extend the same ideas on power positive ... inside the loop after you get it, if its negative right after you get it, wrap it in a condition so nothing more will happen.
Last edited on
a true lifesaver thanks again.
The issue was that you shouldn't have a semi-colon on line 13 of your previous post.
Last edited on
smh thats true but even if I got rid of it and I entered a negative base, i would still get prompted for power.

thanks anyways!
It's easier if you make input as a function:

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
#include <iostream>

int exponent(int, int);

bool getinput(int& base, int& power) {
	std::cout << "Please enter a number that you would like to be raised as an exponenet: ";
	std::cin >> base;

	if (base < 0)
		return false;

	std::cout << "Please enter power you would like this number to be raised to: ";
	std::cin >> power;

	return power >= 0;
}

int main() {
	int base {}, power {};

	std::cout << "This Program will continue until you enter a negative number\n";

	while (getinput(base, power))
		std::cout << base << " to the " << power << " power = " << exponent(base, power) << "\n\n";
}

int exponent(int base, int power) {
	return power != 0 ? base * exponent(base, power - 1) : 1;
}

Topic archived. No new replies allowed.