Just editing my calculator and looking at what functions math.h had and i came across pow,

My program compiles and things like that so no error but if I'm not mistaken the answer is wrong.

6 ^ 1 = 6X6? = 36 (IS THAT CORRECT??)

JUST IN MY PROGRAM IF I USE 6^1 IT GIVES ME 1

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std;

#define PI 3.14159265

void binary(int);

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
     
    double firstNumber, secondNumber;
    double resultSqrt, resultSin, resultCos, resultTan, resultBinary;
    double resultPower;
    char operation, boolSqrt; 
    
    cout << " *****************************************" << endl;;
    cout << "  Kizzop Productions - Simple Calculator!" << endl;;
    cout << " *****************************************\n\n"; 
    
    cout << "  OPERATORS : +, -, *, /, !, ^, S, C, T, B" << endl;
    cout << "\n";
    
    cout << "  + = ADD\n  - = SUBTRACT\n  * = Multiplication\n  ";
    cout << "/ = Division\n  ! = SQAURE ROOT\n  ^ = POWER\n  S = SIN\n  ";
    cout << "C = COS\n  T = TAN\n  B = BINARY\n";
    
    cout << " \n ************************************************" << endl;
    cout << "  NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!" << endl;
    cout << " ************************************************\n\n";    
       
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    resultSqrt = sqrt (firstNumber);
    
    resultPower = pow (firstNumber, secondNumber);
    
    //resultBinary = binary (firstNumber);
    
    resultSin = sin (firstNumber*PI/180);
    
    resultCos = cos (firstNumber*PI/180);
    
    resultTan = tan (firstNumber*PI/180);
    
         switch(operation)
         {
                          case '+':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber / secondNumber);
                          break;
                          
                          case '!':
                          cout << "\n ANSWER = " << resultSqrt;
                          break;
                          
                          case '^':
                          cout << "\n TO THE POWER OF : ";
                          cin  >> secondNumber;
                          cout << "\n ANSWER = " << resultPower;
                          break;
                          
                          case 'S':
                          case 's':
                          cout << "\n ANSWER = " << resultSin;
                          break;
                          
                          case 'C':
                          case 'c':
                          cout << "\n ANSWER = " << resultCos;
                          break;
                          
                          case 'T':
                          case 't':
                          cout << "\n ANSWER = " << resultTan;
                          break;
                          
                          case 'B':
                          case 'b':
                          cout << "\n ANSWER = " << resultBinary;
                          break;
                          
                          default:
                          cout << "\n WRONG MATHEMATICAL OPERATOR!";
                          break;
                          
         }
                          system("PAUSE > nul");
         }
            
/*
void binary(int number) 
{
     int remainder;
     
     if( number <= 1 )
     {
         cout << number;
         
         return;
     }
     
     remainder = number % 2;
     binary (number >> 1);
     cout << remainder;
     
}
*/

Dani AI

Generated

Two separate issues happened here: a math/notation mix-up and a C++ bug. In mathematics “6^2” means 6 squared, but in C/C++ the token ^ is not exponent — it’s bitwise XOR for integer types (and logical XOR for bool). Real exponentiation is done with pow() (from <cmath>). ’s correction about 6^1/6^2 was the math side; the program behavior came from the code side.

The runtime bug was that the program computed the power before reading the exponent. That left the exponent variable uninitialized, which is undefined behavior; in your run it happened to be zero, so pow(base, 0) produced 1. Moving the pow call so it runs only after you prompt for and read the second number (as suggested) fixes the immediate problem.

A few practical tips to make the calculator more robust:

  • Always initialize local variables or read input before use; undefined values cause strange results.
  • Use <cmath> and std::pow in C++ to avoid name-pollution and get proper overloads.
  • For integer exponents consider an integer-power routine (repeated squaring) if you need exact integer results — pow works in floating point and can introduce rounding.
  • Validate inputs (non-numeric, out-of-range, negative base with fractional exponent can yield NaN).
  • For your binary feature: decide whether the helper should print or return a value. Don’t assign the result of a void function to a double; either return a std::string or use std::bitset/integer logic and then output.

Also remove stray characters in the posted snippet (there’s an accidental 6 after the pow line). With the pow call moved after reading the exponent and the above checks, the power and binary features will behave reliably.

Recommended Answers

All 5 Replies

>>6 ^ 1 = 6X6? = 36 (IS THAT CORRECT??)
No, that is not corrtect. 6 ^ 2 is 6 x 6.
6 ^ 1 = 6

you can use a calculator to verify the math

And 6^0=1

Oops sorry.

I recompiled my program and went 6 ^ 2, and the answer was still one..

HELP?

Line 45

resultPower = pow (firstNumber, secondNumber);

Either initialize secondNumber or get its value using cin

I just ran your code for power and it runs fine. In most cases you are using cin after you input the operator and then perform the required operation. But in this case either input secondNumber before line 45 or use the pow() function in the 'case' for ^ after you have taken 'secondNumber'

Oh, I've said to use pow before the user has entered the value, thank you hammerhead!

#include <iostream>
#include <conio.h>
#include <math.h>
#include <stdio.h>

using namespace std;

#define PI 3.14159265

/*void binary(int);*/

int main()
{
    system("TITLE My Calculator Program.");
    system("COLOR C");
     
    double firstNumber, secondNumber;
    double resultSqrt, resultSin, resultCos, resultTan, resultBinary;
    double resultPower;
    char operation, boolSqrt; 
    
    cout << " *****************************************" << endl;;
    cout << "  Kizzop Productions - Simple Calculator!" << endl;;
    cout << " *****************************************\n\n"; 
    
    cout << "  OPERATORS : +, -, *, /, !, ^, S, C, T, B" << endl;
    cout << "\n";
    
    cout << "  + = ADD\n  - = SUBTRACT\n  * = Multiplication\n  ";
    cout << "/ = Division\n  ! = SQAURE ROOT\n  ^ = POWER\n  S = SIN\n  ";
    cout << "C = COS\n  T = TAN\n  B = BINARY\n";
    
    cout << " \n ************************************************" << endl;
    cout << "  NOTE : YOU CAN USE X AND x FOR MULTIPLICATION!" << endl;
    cout << " ************************************************\n\n";    
       
    cout << "\n ENTER FIRST NUMBER  : ";
    cin  >> firstNumber;

    cout << "\n ENTER OPERATOR SIGN : ";
    cin  >> operation;
    
    resultSqrt = sqrt (firstNumber);
        
    //resultBinary = binary (firstNumber);
    
    resultSin = sin (firstNumber*PI/180);
    
    resultCos = cos (firstNumber*PI/180);
    
    resultTan = tan (firstNumber*PI/180);
    
         switch(operation)
         {
                          case '+':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber + secondNumber);
                          break;
                          
                          case '-':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber - secondNumber);
                          break;
                          
                          case '*':
                          case 'x':
                          case 'X':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber * secondNumber);
                          break;
                          
                          case '/':
                          cout << "\n ENTER SECOND NUMBER : ";
                          cin  >> secondNumber;
                          cout << "\n Answer = " << (firstNumber / secondNumber);
                          break;
                          
                          case '!':
                          cout << "\n ANSWER = " << resultSqrt;
                          break;
                          
                          case '^':
                          cout << "\n TO THE POWER OF : ";
                          cin  >> secondNumber;
                          
                          resultPower = pow (firstNumber, secondNumber);6
                          
                          cout << "\n ANSWER = " << resultPower;
                          break;
                          
                          case 'S':
                          case 's':
                          cout << "\n ANSWER = " << resultSin;
                          break;
                          
                          case 'C':
                          case 'c':
                          cout << "\n ANSWER = " << resultCos;
                          break;
                          
                          case 'T':
                          case 't':
                          cout << "\n ANSWER = " << resultTan;
                          break;
                          
                          case 'B':
                          case 'b':
                          cout << "\n ANSWER = " << resultBinary;
                          break;
                          
                          default:
                          cout << "\n WRONG MATHEMATICAL OPERATOR!";
                          break;
                          
         }
                          system("PAUSE > nul");
         }
            
/*
void binary(int number) 
{
     int remainder;
     
     if( number <= 1 )
     {
         cout << number;
         
         return;
     }
     
     remainder = number % 2;
     binary (number >> 1);
     cout << remainder;
     
}
*/

WOO, POWER IS WORKING!

NOW JUST TO GET THE BINARY WORKING =/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.