I'm new at programing and I gave myself a challange by making a calculator, but when i want to use the exponential mode more than once, it adds up the exponents instead of restarting the whole thing, anyone can help me please? Also if you have some tips for future projects for beginners or you have some tips please share it with me, thanks a lot! :) Here is my code:
int main()
{
long double num1,num2,result;
char op,redo;
result = 1;
do
{
cout << "Give the operation! (e.g.: 2+3) aviable: + - * / ^" << endl;
cin >> num1 >> op >> num2;
if (op == '+' || '-' || '*' || '/' || '^')
{
switch (op)
{
case '+':
cout << endl << "Your solution is:" << num1 + num2;
break;
case '-':
cout << endl << "Your solution is:" << num1 - num2;
break;
case '*':
cout << endl << "Your solution is:" << num1 * num2;
break;
case '/':
if (num2 != 0)
{
cout << endl << "Your solution is:" << num1 / num2;
}
else
{
cout << endl << "We cannot devide by 0!";
}
break;
case '^':
while (num2 != 0) {
result *= num1;
--num2;
}
cout << "Your solution is:" << result;
break;
default:
cout << "This operation cannot be processed!";
break;
}
}
else
{
cout << "Wrong operation!";
}
cout << endl << "Would you like to restart? [Y/N]" << endl;
cin >> redo;
} while (redo == 'y' || redo == 'Y');
return 0;
}
You need to reset the result
every time before entering the loop:
case '^':
result = 1; // <--- add this
while (num2 != 0) {
result *= num1;
--num2;
}