Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,982 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,378 people online right now. Registration is fast and FREE... Join Now!




Exponents in calculations

 
Reply to this topicStart new topic

Exponents in calculations, I'm in a c++ rpgramming class and this is what we have to do. We h

edub2015
3 Dec, 2006 - 11:45 AM
Post #1

New D.I.C Head
*

Joined: 3 Dec, 2006
Posts: 1


My Contributions
Is there anyway to code exponentiation in a program without using ^ ?

CODE
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//    The pupose of this program is to prompt user's to enter two
//    numeric values and an operation to be preformed on those two
//    values. The program will then accept the user's input and perform
//    the requested operation and display the original two values, as
//    either sum, difference, product, quotient, exponentiation, or remainder.
//    It is assumed that the program could operate as a more advanced
//    four function calculator.

//    Function prototypes
    char mainMenu();
    double getUserValue(string);
    double addition(double, double);
    double subtraction(double, double);
    double multiplication(double, double);
    double division(double, double);
    void pauseForEnter();
    void exponetiation(double, double, double);
    void modulus(double, double, double);

    int main()
    {
        //    Declare variables
        char operation = ' ';
        double value1  = 0.0;
        double value2  = 0.0;
        double result  = 0.0;
        string keypress = "";

        //    Set numeric format and precisions for numericouput with
        // cout.
        cout << setiosflags(ios::fixed) << setprecision(2);

        //    Begin the main program loop
        do
        {
            //    Call the mainMenu() function to display the menu and
            //    get the user's menu choice.
            operation = mainMenu();

            //    Evaluate the user choice and determine what we need
            //    to do.
            if ((operation == 'A') || (operation == 'S') || (operation
                == 'M') || (operation == 'D')|| (operation == 'Q'))
            {
                //    Get the user values
                value1 = getUserValue("first");
                value2 = getUserValue("second");

                //    Perform the requested operation and display the results.
                switch (operation)
                {
                case 'A': result = addition(value1, value2);
                    printf("Result: %.2f + %.2f = %.2f\n\n", value1, value2, result);
                    break;

                case 'S': result = subtraction(value1, value2);
                    printf("Result: %.2f - %.2f = %.2f\n\n", value1, value2, result);
                    break;

                case 'M': result = multiplication(value1, value2);
                    printf("Result: %.2f * %.2f = %.2f\n\n", value1, value2, result);
                    break;

                case 'D': if (value2 != 0.0)
                          {
                              result = division(value1, value2);
                              printf("Result: %.2f / %.2f = %.2f\n\n", value1, value2, result);
                          }
                          else
                          cout << "\nError: Division by zero is not permitted.\n\n";
                        //    end if/else
                    break;

                case 'E': if (value != 0.0)
                          {
                              result = exponentiation(value1, value2);
                              printf("Result: %.2f < %.2f = %.2f\n\n", value1, value2, result);
                              break;
                          }

                case 'R': if (value != 0.0)
                          {
                              result = Modulus(value1, value2);
                              printf("Results: %.2f < %.2f = %.2f\n\n", value1, value2, result);
                              break;
                        
                }
                //    end switch

            }
            
            else if (operation != 'Q')
                printf("\nError: Invalid operation requested. Operation %c is not defined.\n\n",
                operation);

            else
                cout << "\nGoodbye!\n";
            //    end if/else

           //    Pause the program for keystroke.
                pauseForEnter();
        }
        while (operation != 'Q');
        //    end do while

        //    Terminate the program
        return 0;
    }
    //    end function main()

    //    The function mainMenu() displays an explanation of the purpose of
    //    the program, as well as a menu of choices for the user. The user
    //    choice is obtained and returned to the caller.
    char mainMenu()
    {
        //    Declare variables
        char userChoice = ' ';

        //    Clear the screen, display the menu, and get the user menu
        //    choice.
        system("cls");
        cout << "This program will accept two numeric values and the perform\n"
            << "The desired operation on those values.\n"
            << "\nTo select the desired operation, enter the characters\n"
            << "Associated with the operation as shown below.\n\n";
        cout << "\tA or a - Addition\n";
        cout << "\tS or s - Suntraction\n";
        cout << "\tM or m - Multiplication\n";
        cout << "\tD or d - Division\n";
        cout << "\tQ or q - Quit\n\n";
        cout << "Which operation would you like to perform? ";
        cin >> userChoice;
        cin.clear();

        //    Convert the user to uppercase and return to caller.
        return toupper(userChoice);
    }
    //    end function mainMenu()

    //    The function getUserValue() prompts the user to enter a value
    //    to be used is the operation requested. The value is obtained
    //    and returned to the caller.
    double getUserValue(string whichValue)
    {
        //    Declare variables
        double value = 0.0;

        //    Prompt the user to enter a value and return it to the caller.
        cout << "Enter the " << whichValue << " numeric value:";
        cin >> value;
        cin.clear();

        //    Return the value
        return value;
    }
    //    end function getUserValue()

    //    The function addition() calculates and return the sum of its
    //    parameter
    double addition(double num1, double num2)
    {
        return (num1 + num2);
    }
    //    end function addition

    //    The function subtraction() calculates and return the difference
    //    of its parameter
    double subtraction(double num1, double num2)
    {
        return (num1 - num2);
    }
    //    end function subtraction

    //    The function multiplication() calculates and return the product
    //    of its parameter
    double multiplication(double num1, double num2)
    {
        return (num1 * num2);
    }
    //    end function multiplication

    //    The function division() calculates and return the qoutient
    //    of its parameter
    double division(double num1, double num2)
    {
        return (num1 / num2);
    }
    //    end function division

    //    The function pauseforEnter() waits for the user to press the
    //    (Enter) key.
void pauseForEnter()
{
    //    Declare vaiables
        string keypress = "";

    //    Pause the program for a keystroke
        cout << "Please press (Enter)...";
        cin.ignore (1000,'\n');
        getline(cin, keypress);

    //    The function of exponentiation is to raise a numer x to the next power y
        void exponetiation(double, double, double);


}    //    end program


edit: modified title, added tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

msg555
RE: Exponents In Calculations
3 Dec, 2006 - 12:26 PM
Post #2

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 5 times
Dream Kudos: 25
My Contributions
The ^ operator isn't exponentation, it is a bitwise xor.

You could always use the pow function.
User is offlineProfile CardPM
+Quote Post

okyup
RE: Exponents In Calculations
3 Dec, 2006 - 03:55 PM
Post #3

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
#include <math.h>

...

num=pow(base,exp);
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 07:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month