You are here:

- Home
- Computing/Technology
- C/C++
- C++
- Exponents

Advertisement

Hi, I'm trying to write a pretty basic calculator and I want to make it to where I can have exponents. The calculator has 3 functions: main, greeting, calculations. I have main that calls on greeting and then calculations. Calculations first asks which type of calculation would you like to do. Then it defines int a and then the code accepts a (cin >> a;). So then this runs:

if(a == 1)

{

//Adding

cout << "You've chosen adding!" << endl;

cout << "Type two numbers and they'll add together." << endl;

double b;

double c;

cin >> b;

cin >> c;

cout << b + c << endl;

}

else if(a == 2)

{

//Subtracting

cout << "You've chosen subtracting!" << endl;

cout << "Type two numbers and they'll subtract the first one from the second." << endl;

double b;

double c;

cin >> b;

cin >> c;

cout << b - c << endl;

}

else if(a == 3)

{

//Multiplying

cout << "You've chosen multipling." << endl;

cout << "Type two numbers and they'll multiply by each other." << endl;

double b;

double c;

cin >> b;

cin >> c;

cout << b * c << endl;

}

else if(a == 4)

{

//Dividing

cout << "You've chosen dividing." << endl;

cout << "Type two numbers and they'll divide by each other." << endl;

double b;

double c;

cin >> b;

cin >> c;

cout << b / c << endl;

}

else if(a == 5)

{

//Exponents

cout << "You've chosen exponents." << endl;

cout << "Type two numbers and your first one will be that first number, and the second will be the exponent." << endl;

double b;

double c;

cin >> b;

cin >> c;

cout << b ^ c << endl;

I need to know how to make the last else if statement to be able to run b(to the power of)c.

Hope you can help :)

Well although C and C++ have no exponentiation operator such as ^ in BASIC or ** in FORTRAN it does include a maths function to do it. [Note: in C and C++ the ^ symbol is used for the bitwise exclusive OR operator].

The function is called pow, is considered part of the maths section of the C/C++ standard library so you need to include <cmath> (Or <math.h> for C and really old C++ libraries). You may have to explicitly link to a math library (e.g. use option -lm for g++/gcc and some other Linux / Unix compilers) to get your program to complete building.

The pow function takes a floating point base and a floating point exponent or power and returns a floating point result:

// Replacement for your program line:

// cout << b ^ c << endl;

cout << pow( b, c ) << endl;

Hope this helps and moves you forward.

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines.
Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is).
When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD.
My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes.
I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.**Education/Credentials**