Pow Function

I am trying to make a calculator code using switch statements. There are many different arithmetic operators but when I use the pow function with integer numbers no matter what, it outputs "1". 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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
double num1;
double num2;
double answer;
answer = pow(num1, num2);
char oper;
char subraction = '-';
char adddtion = '+'; 
char divsion = '/';
char muliplaction = '*'; 
char pow = '^';
cout << "Put in a Number" << endl;
cin >> num1;

cout << "Put in another Number" << endl;
cin >> num2;

cout << "Type in one of these operators (* / - + ^)" << endl; 
cin >> oper;
switch (oper)
{
case '+': 
cout << num1 + num2  << endl;
break;
case '/':
cout << num1 / num2  << endl;
break;
case '*':
cout << num1 * num2  << endl;
break;

case '-':
cout << num1 - num2  << endl;
break;
case '^':
cout << answer << endl;
break;
}

0;
}

Hello FreshCoder,

You should run your program and try to fix the errors and warnings .

Lines 8, 9 and 10 define 3 variables that are uninitialized and contain garbage values.

Line 11 calls the "std::pow" function with the garbage values, so you may be lucky that you are getting the (1) that you do.

By the time you actually give "num1" and "num2" a proper value and then reach "case '^':" you do not use "num1" and "num2" with their new values you output "answer" which contains a garbage value because of garbage input.

To start with it is always a good idea to initialize your numeric variables when defined.

Lines 13 - 17 are never used. Did you have a reason for them?

Line 42 needs to look more like lines 29, 32, 35 and 39.

Andy
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
#include <iostream>
#include <cmath>

int main()
{
    std::cout << "enter an expression in the form: <nnnnn> <op> <nnnnn>\n"
              << "where <nnnnn> is a number and <op> is an operator( one of */-+^)\n"
              << "for example, enter 12.34 / 9.86: "  ;

    double num1 = 0;
    double num2 = 0 ;
    char oper = '*' ;

    // read in the input first,
    if( std::cin >> num1 >> oper >> num2 ) // if the attempt to read is successful
    {
        // *after* reading in the values, use them to perform the computation and display the result
        // optional: std::fixed : display the result using fixed notation
        std::cout /* << std::fixed */ << num1 << ' ' << oper << ' ' << num2 << " == " ;

        switch(oper)
        {
            case '*' :
                std::cout << num1*num2 << '\n' ;
                break ;

            case '/' :
                // could add a check for division by zero? zero/zero?
                std::cout << num1/num2 << '\n' ;
                break ;

            case '+' :
                std::cout << num1+num2 << '\n' ;
                break ;

            case '-' :
                std::cout << num1-num2 << '\n' ;
                break ;

            case '^' :
                // could add a check for validity: are the arguments to std::pow are well defined?
                std::cout << std::pow(num1,num2) << '\n' ;
                break ;

            default:
                {
                    std::cout << "***error: invalid operator '" << oper << "'\n" ;
                    return 1 ; // return non-zero from main to indicate failure
                }
        }
    }

    else
    {
        std::cout << "***error: invalid input\n" ;
        return 1 ; // return non-zero from main to indicate failure
    }
}
Topic archived. No new replies allowed.