Like Tree1Likes
  • 1 Post By laserlight

Quick Question

This is a discussion on Quick Question within the C++ Programming forums, part of the General Programming Boards category; So I'm programming another calculator, and this time I'm trying to do exponents (ex. 3^4=81). Code: #include <iostream> using namespace ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Question Quick Question

    So I'm programming another calculator, and this time I'm trying to do exponents (ex. 3^4=81).
    Code:
    #include <iostream>
    using namespace std;
    int main ()
    {
        int w=1;
        do {
            cout << "\nEnter an expression.\n"; //instructions for the user
            cout << "Key: \n";
            cout << "* = multiply\n";
            cout << "/ = divide\n";
            cout << "+ = add\n";
            cout << "- = subtract\n";
            cout << "^ = x to the yth power\n";
            cout << "Enter your expression.\n";
            int x,z;
            char y;
            cin >> x >> y >> z; //get the expression
            switch (y) {
            case '+': //calculate answer(s)
                cout << "The answer is " << x+z << ".\n";
                break;
            case '-':
                cout << "The answer is " << x-z << ".\n";
                break;
            case '*':
                cout << "The answer is " << x*z << ".\n";
                break;
            case '/':
                cout << "The answer is " << x/z << ".\n";
            case '^':
                for (z; z >= 2; z=z-1)
                {
                    int Q;
                    Q=x*x;
                }
                cout << "The answer is " << x << ".\n";
                break;
            default :
                cout << "The expression is invalid.\n";
                break;
            }
            cout << "To run function again, press 1.  Otherwise, press 0.\n"; //Optional Continuation
            cin >> w;
        } while (w==1);
        cin.get();
    }
    This program runs fine, but when I try to do the exponent, I get the wrong number. Usually it happens like this:
    3^4 = 3
    6^2 = 6
    etc.
    So basically I just get my x value back. How do I fix this?
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,302
    Notice that you don't change x in that part of the program, so the value of x remains the same.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question
    By zach48191 in forum C Programming
    Replies: 7
    Last Post: 10-21-2011, 01:05 AM
  2. please help, quick question.
    By cmanrules in forum C Programming
    Replies: 6
    Last Post: 12-02-2008, 04:04 PM
  3. One quick question...
    By Kross7 in forum C++ Programming
    Replies: 10
    Last Post: 04-13-2007, 10:50 PM
  4. Quick Question
    By Teresa in forum C Programming
    Replies: 3
    Last Post: 05-30-2002, 07:28 AM
  5. quick question
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2001, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21