exponents
Apr 19, 2013 at 7:06pm UTC
basically im quite new to programming but i wanted to know how to do exponents. where someone can put in a first number and a second number. the answer would be the first number ^ second number.
i tried writing it as <<firstNumber<<"^"<<secondNumber<<"="<<(firstNumber^secondNumber)<< endl;
but it didnt seem to work... any suggesstions on how to do it???
It will be where:
if ( Op == 7){
cout << "the answer is..."<<
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
#include <iostream>
using namespace std;
int main()
{
{
double firstNumber = 0.0;
double secondNumber = 0.0;
char operation =' ' ;
char doAgain=' ' ;
do
{
cout << "welcome to Calculator++." << endl;
cout << "Select Operation" << endl;
cout << "1. Basic calculations" << endl;
cout << "2. Trigonometry" << endl;
cout << "3. Statistics" << endl;
int M;
cin >> M;
if ( M == 1 ){
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "4. Division" << endl;
cout << "5. Square a number" << endl;
cout << "6. Cube a number" <<endl;
cout << "7. Exponent's ( X^n)" <<endl;
cout << "8. Squareroot" <<endl;
{
cout << "Enter first number" <<endl;
cin>>firstNumber;
cout << "Enter operation number (1 to 8)" <<endl;
cin>>operation;
cout << "Enter second number (do not enter number for operation 5, 6 and 8)" <<endl;
cin>>secondNumber;
int Op;
cin >> Op;
if ( Op == 1 ) {
cout << "the answer is..." <<firstNumber<<"+" <<secondNumber<<"=" <<(firstNumber+secondNumber)<<endl;
break ;
}
if ( Op == 2){
cout << "the answer is..." <<firstNumber<<"-" <<secondNumber<<"=" <<(firstNumber-secondNumber)<<endl;
break ;
}
if ( Op == 3){
cout << "the answer is..." <<firstNumber<<"*" <<secondNumber<<"=" <<(firstNumber*secondNumber)<<endl;
break ;
}
if ( Op == 4){
cout << "the answer is..." <<firstNumber<<"/" <<secondNumber<<"=" <<(firstNumber/secondNumber)<<endl;
break ;
}
if ( Op == 5){
cout << "the answer is..." <<firstNumber<<"*" <<firstNumber<<"=" <<(firstNumber*firstNumber)<<endl;
break ;
}
if ( Op == 6){
cout << "the answer is..." <<firstNumber<<"*" <<firstNumber<<"*" <<firstNumber<<"=" <<(firstNumber*firstNumber*firstNumber)<<endl;
break ;
}
if ( Op == 7){
cout << "the answer is..." <<
Apr 19, 2013 at 7:23pm UTC
Apr 19, 2013 at 7:26pm UTC
thank you so much!! :)
Apr 19, 2013 at 7:50pm UTC
im getting an error saying that 'cbrt' is not a member of 'std'
Apr 19, 2013 at 7:59pm UTC
std::cbrt()
is C++11.
Which compiler (and version) are you using?
Apr 19, 2013 at 8:06pm UTC
im using code blocks 12.11 with GNU GCC compiler but im not sure what version sorry
Apr 19, 2013 at 8:24pm UTC
dont worry i found a tick box that allowed the compiler to notice c++11 language. it works fine now
Apr 19, 2013 at 8:25pm UTC
Turn on the compiler option -std=C++11
And while you are at it, turn on these three too: -Wall -Wextra -pedantic-errors
On the menu, Settings => Compiler... (IIRC)
Apr 19, 2013 at 8:26pm UTC
You may be able to tell the compiler to use the C++11 standard.
From the code::blocks menu, select
settings->compiler then click the checkbox for C++11.
Or, instead of using cbrt(), use raise the number to the power 1/3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double num = 125;
double a = pow(num,1.0/3.0);
double b = cbrt(num);
cout << "num: " << num << " a: " << a << " b: " << b << endl;
return 0;
}
Topic archived. No new replies allowed.