14
C++ looping and exponents?
Filed Under (Programming & Design) by jepes on 14-02-2012
asking this at February 14, 2012 – 18:55 pm
I am 2 days into C++, and have a question about this program:
#include
#include
#include
int main()
{
int number;
int number2;
int exponent = 1;
string choice;
cout < < "Would you like to add, div, mult, sub or exponent? " << endl;
cin >> choice;
if (choice == “add”)
{
cout < < "Enter the first digit: "<< endl;
cin >> number;
cout < < "Enter the second digit: " << endl << endl;
cin >> number2;
cout < < "The answer is " << number + number2 << endl;
}
else if (choice == "div")
{
cout << "Enter the first digit: " << endl;
cin >> number;
cout < < "Enter the second digit " << endl << endl;
cin >> number2;
cout < < "The answer is " << number /number2 << " with a remainder of " << number % number2 << endl;
}
else if (choice == "mult")
{
cout << "Enter the first digit: " << endl;
cin >> number;
cout < < "Enter the second digit: " << endl << endl;
cin >> number2;
cout < < "The answer is " << number * number2 << endl;
}
else if (choice == "sub")
{
cout << "Enter the first digit: " << endl;
cin >> number;
cout < < "Enter the second digit: " << endl << endl;
cin >> number2;
cout < < "The answer is " << number + number2 << endl;
}
else if (choice == "exponent")
{
cout << "Enter the base number: " << endl;
cin >> number;
cout < < "Enter the exponent: " << endl << endl;
cin >> number2;
for (int i = 1; i < = number2; i++)
{
exponent = (i * number2);
}
cout << "The answer is " << exponent << endl;
}
else
{
cout << "That answer is not permitted by the program " << endl;
}
return 0;
}
Okay, so it all runs how I want it, except the exponent formula. Can someone explain how to make the exponents work as well as how to loop back to the first question after each little calculation, so they can keep using it? If you need something answered I will be glad to supply in additional info so thanks in advance.
but the only thing the string represents is the first question, then after that the int variables represent all for the numbers?
Chosen Answer:
oops, February 14, 2012 – 19:10 pm (5 Stars)