# Power of (Exponentiation) trouble...

Page 1 of 1

## 4 Replies - 524 Views - Last Post: 09 December 2009 - 01:21 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=144065&amp;s=7f3f75c699f67cd7787b764fa247ce90&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Komaqtion

Reputation: 0
• Posts: 22
• Joined: 24-October 09

# Power of (Exponentiation) trouble...

Posted 07 December 2009 - 03:08 PM

Hi!

Ok, I'm just trying to do a simple exponentiation app for the DOS window (Which I'm hoping to be able to do in a Win32 window soon )
I also wanna test if I know how to use "If"s and "while"...

Anyways, so here's the code:
```#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{

int number;
int power;
int result;

cout << "What number would you like to exponentiate ?" << endl;

cin >> number;

cout << "And by what number should it be exponentiated ?" << endl;
cout << "Btw, number is: " << number << endl;

cin >> power;

cout << "Btw, power is: " << power << endl;

result = number;

cout << "Btw, result is: " << result << endl;

if( power > 1 )
{

while( power > 1)
{

result = result * number;
power = power - 1;

}

}

cin.get();

return 0;

}
```

And it all works fine, all messages show fine, and with the correct values...
All except the last one, and also the "cin.get()" doesn't seem to work there as it quickly exits the window after I type in the second number...

So my questions are:
Is there something wrong inside the If and While ? :S
And why doesn't the last cin.get() work ?

Is This A Good Question/Topic? 0

## Replies To: Power of (Exponentiation) trouble...

### #2 Bench

• D.I.C Lover

Reputation: 938
• Posts: 2,462
• Joined: 20-August 07

## Re: Power of (Exponentiation) trouble...

Posted 07 December 2009 - 03:24 PM

Have a look here (sticky thread at the top of this forum) about keeping your window open, and why cin.get() sometimes has problems.

http://www.dreaminco...wtopic30581.htm

As for your final output, look at the variable you're using to store the result of your calculation, vs the one that you're displaying;
```        while( power > 1)
{

result = result * number;
```

This post has been edited by Bench: 07 December 2009 - 03:28 PM

### #3 Komaqtion

Reputation: 0
• Posts: 22
• Joined: 24-October 09

## Re: Power of (Exponentiation) trouble...

Posted 07 December 2009 - 03:31 PM

Ok, thanks for clearing that up !
So I should always use all of that when I use cin.get() or what ?

And, even if all the numbers show now, the last one still is only 2 when I first type 2 and then the exponentiation is also 2... This would make it 4 right ?! XD

### #4 jjl

• Engineer

Reputation: 1168
• Posts: 4,782
• Joined: 09-June 09

## Re: Power of (Exponentiation) trouble...

Posted 07 December 2009 - 03:47 PM

only when your pausing the window. Also i realize this is a learning experience but once you finish take a look at the <cmath> library and look at the pow() function

### #5 Komaqtion

Reputation: 0
• Posts: 22
• Joined: 24-October 09

## Re: Power of (Exponentiation) trouble...

Posted 09 December 2009 - 01:21 PM

Yeah, I kinda new that this existed already, but yeah I'm learning

Any answers to my other question(s) ?

Ok, I figured it out XD
(I used "number" as the final output, when I was supposed to use "result" XD)

```#include <cstdlib>
#include <iostream>
#include <limits>

using namespace std;

int main()
{

int number;
int power;
int result;

cout << "What number would you like to exponentiate ?" << endl;

cin >> number;

cout << "And by what number should it be exponentiated ?" << endl;

cin >> power;

result = number;

if( power > 1 )
{

while( power > 1)
{

result = result * number;
power = power - 1;

}

}

cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
cin.get();

return 0;

}
```

This post has been edited by Komaqtion: 09 December 2009 - 01:28 PM