So I'm programming another calculator, and this time I'm trying to do exponents (ex. 3^4=81).
Code:
#include <iostream>
using namespace std;
int main ()
{
int w=1;
do {
cout << "\nEnter an expression.\n"; //instructions for the user
cout << "Key: \n";
cout << "* = multiply\n";
cout << "/ = divide\n";
cout << "+ = add\n";
cout << "- = subtract\n";
cout << "^ = x to the yth power\n";
cout << "Enter your expression.\n";
int x,z;
char y;
cin >> x >> y >> z; //get the expression
switch (y) {
case '+': //calculate answer(s)
cout << "The answer is " << x+z << ".\n";
break;
case '-':
cout << "The answer is " << x-z << ".\n";
break;
case '*':
cout << "The answer is " << x*z << ".\n";
break;
case '/':
cout << "The answer is " << x/z << ".\n";
case '^':
for (z; z >= 2; z=z-1)
{
int Q;
Q=x*x;
}
cout << "The answer is " << x << ".\n";
break;
default :
cout << "The expression is invalid.\n";
break;
}
cout << "To run function again, press 1. Otherwise, press 0.\n"; //Optional Continuation
cin >> w;
} while (w==1);
cin.get();
}
This program runs fine, but when I try to do the exponent, I get the wrong number. Usually it happens like this:
3^4 = 3
6^2 = 6
etc.
So basically I just get my x value back. How do I fix this?
Thanks