Exponent calculator

Hello! I am working on an exponent calculator and It is not working correctly. Please Help!
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <Windows.h>
#include <winuser.h>
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
    string retry;
    string symbol;
    string word;
    int a = 0;
    int b = 0;
    int x = 1;
    int Exponent=0;
	double number1=0;
	double number2=0;
	double answer=0;
    void num_input();
    void Answer();
    void exponent();
    bool Retry();
int main(){
do {
	cout << "What type of equation would you like to answer? \n";
	cout << "Please type the word for the equation you would like to complete \nNO CAPS!\n";
	cin >> word;

             if (word == "add"){
			cout << "You have chosen Addition" << endl;
			symbol="+";}
		else if(word == "subtract"){
			cout << "You have chosen Subtraction" << endl;
			symbol="-";}
		else if(word == "divide"){
			cout << "You have chosen Division" << endl;
			symbol="/";}
		else if (word == "multiply"){
			cout << "You have chosen Multiplication" << endl;
			symbol="*";}
		else if(word == "exponent"||word == "exponents"){
                        cout << "You have chosen Exponents"<< endl;
                        symbol="^";}
		else{
			cout << "You have not followed my instructions, the program will now Restart!" << endl;
			Sleep(2000);
			system("cls");
			continue; }
               num_input();
               Answer();
} while (x=1);
num_input();}
void num_input(){
	cout << "Please type your 1st number" << endl;
	cin >> number1;
	cout << "Please type your 2nd number or Exponent" << endl << endl;
	cin  >> number2;

	if (symbol == "+")
			answer = number1 + number2;
		else if(symbol == "-")
			answer = number1 - number2;
		else if(symbol == "/")
			answer = number1 / number2;
		else if (symbol == "*")
			answer = number1 * number2;
        else if (symbol == "^")
            Exponent = number2;
            void exponent();

	if (symbol == "/" && number2 == 0)
	{
			cout << "This is the only formula that is not yet supported, sorry about that! \nThe program will now end!";
			Sleep(2000);
			system("EXIT");
	}else
	Answer();




}
void exponent(){
switch(Exponent){
case 1:
    number1 = answer;
break;
default:
    break;
}
}
void Answer(){
	cout << "Your equation is:" << endl;
	cout << number1 << " " << symbol << " " << number2 << " = " << answer;
    cout << "\nyour answer is " << answer << endl << endl << endl;
	cout << "Thank you for using The 4 Equation Calculator, made by jasonwynn10!" << endl;
	x=0;
	Retry();}
bool Retry(){
    Sleep(5000);
    cout << "Press SpaceBar to restart or wait 5 seconds for the program to close.\n\n";
    while(b!=1){
    if(GetAsyncKeyState(VK_SPACE)){
    a=1;
    x=1;
    main();
    }else{
    Sleep(1000);
    if(b=5){system("exit");}
    b++;}}
    }
Last edited on
lolwut

What is your compiler/debugger telling you? I suggest you start from there.
Lines 63 & 64 can be similar to the other cases, you just need to use the pow() function:
1
2
        else if (symbol == "^")
            answer = pow(number1, number2);

Now get rid of exponent_answer(). You don't need it.

Consider restructuring your code. Right now main calls num_input(). Num_input gets the numbers and then calls Answer(). Answer() prints the result and then prompts for a retry. Retry calls num_input() again. It's better to have each function do one thing only instead of having it do one thing and then call the function to do the next. In other words, num_input() should JUST get the numbers and compute the answer. Answer() should JUST display the answer. retry() should JUST ask whether you want to retry. You can then string these all together in the main program with something like this:
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
do {
	cout << "What type of equation would you like to answer? \n";
	cout << "Please type the word for the equation you would like to complete \nNO CAPS!\n";
	cin >> word;

             if (word == "add"){
			cout << "You have chosen Addition" << endl;
			symbol="+";}
		else if(word == "subtract"){
			cout << "You have chosen Subtraction" << endl;
			symbol="-";}
		else if(word == "divide"){
			cout << "You have chosen Division" << endl;
			symbol="/";}
		else if (word == "multiply"){
			cout << "You have chosen Multiplication" << endl;
			symbol="*";}
		else if(word == "exponent"||word == "exponents"){
                        cout << "You have chosen Exponents"<< endl;
                        symbol="^";}
		else{
			cout << "You have not followed my instructions, the program will now Restart!" << endl;
			Sleep(2000);
			system("cls");
			continue; }
               num_input();
               Answer();
       } while (Retry());


Note that Retry() should contain this logic, which you will need to modify to make work right:

1
2
3
4
5
6
7
8
9
    Sleep(5000);
    cout << "Press SpaceBar to restart or wait 5 seconds for the program to close.\n\n";
    while(b!=1){
    if(GetAsyncKeyState(VK_SPACE)){
    a=1;
    Retry();}else{
    Sleep(1000);
    if(b=5){system("exit");}
    b++;}
which header does pow come from?
Topic archived. No new replies allowed.