Thread: Quick Question

  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
    28,413
    Notice that you don't change x in that part of the program, so the value of x remains the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 12: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, 09:50 PM
  4. Quick Question
    By Teresa in forum C Programming
    Replies: 3
    Last Post: 05-30-2002, 06:28 AM
  5. quick question
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2001, 01:34 PM