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.
#include <iostream>
usingnamespace 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));
elsereturn 1;
}
#include <iostream>
usingnamespace 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));
elsereturn 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.
#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)
returnfalse;
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;
}