Loops with Exponents

Heya, guys. I've been doing pretty well, lately with the whole C++ thing. I've been getting the hang of it, and so I'm really happy. I've trying to push myself, and so I made a calculator program that does the basic four operator stuff with two numbers, but now I want to test my Loop Skills (Which I have barely any) and create Exponents.

My first question is: Do I use a For Loop or a While Loop? Why?

Right here is my code, and I just want to know if this seems on track SO FAR. Please do not tell me the answer, because I'm trying to push myself. Thanks a lot! The Loop section isn't done because I'm stuck. I just need to think it out, but I want to know if I use a For Loop or a While Loop.

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
#include <iostream>
using namespace std;

int main()
{
    int firstNumber;
    int yesNo;
    int exponentTimes;
    int secondNumber;
    int operatorNumber;


    cout << "Hello, user!  Today we will be using a very basic calculator." << endl;
    cout << "Hit ENTER to continue." << endl;
    cin.get();

    cout << "Do you mind printing in your first number: ";
    cin >> firstNumber;

    cout << "\n\n Would you like to multiply this number with exponents? (1 for yes, 0 for no)" << endl;
    cin >> yesNo;
        if (yesNo == 1){
            for // Is this the correct Loop?
        }



    cout << "\nNow, please enter your second number that you would like to use: ";
    cin >> secondNumber;


    cout << "\n\nGreat!  So, please enter the number corresponding to the operator below: " << endl;
    cout << "1. +\n2. -\n3. *\n4. /" << endl;
    cin >> operatorNumber;

        if (operatorNumber == 1){
            cout << "The sum of the two numbers is: " << firstNumber + secondNumber << endl;
        }
            else if (operatorNumber == 2){
                cout << "The difference of the two numbers is: " << firstNumber - secondNumber << endl;
            }

            else if (operatorNumber == 3){
                cout << "The product of the two numbers is: " << firstNumber * secondNumber << endl;
            }

            else if (operatorNumber == 4){
                cout << "The quotient of the two numbers is " << firstNumber / secondNumber << endl;
            }

                else {
                    cout << "I'm sorry, you entered a number that is not on the list, please try again." << endl;
                }
    return 0;
}
I don't understand what you mean by 'do you want to multiply this number with exponents', but I can tell you that either loop will work. The one you choose depends on how you have it set up so far, there is no right or wrong kind of loop to use.
Alright, I worded it wrong. By the User will chose one number, say "5". The next number will be the power of that number. Let's say the user chooses 6.

So, it'll be 5^6. (5*5*5*5*5*5).

That's my new project, using exponents with loops, but I don't really know how to use loops that well. So is there anyone that can point me to a tutorial or maybe help me out? Give me a little boost, and I'll see if I can finish it? Any help would be appreciated. Just don't finish the entire code, please.
Say you have the number 5. Say you need to raise it to the exponent n. How many times would you need to multiply 5 by itself to get 5^n?

That's how many times your loop needs to run. A short bit of pseudo code:

1
2
for (counter = 0 and original = variable, counter <  times_your_loop_needs_to_run, ++counter)
multiply_variable_by_original;


-Albatross
Last edited on
Topic archived. No new replies allowed.