4 Replies - 424 Views - Last Post: 09 December 2009 - 01:21 PM Rate Topic: -----

#1 Komaqtion  Icon User is offline

  • New D.I.C Head

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

Power of (Exponentiation) trouble...

Post icon  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 :o)
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;
			
		}
		
	}
	
	cout << "Your answer is: " << number << endl;
	
   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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • 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; 

cout << "Your answer is: " << number << endl;

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

Was This Post Helpful? 0
  • +
  • -

#3 Komaqtion  Icon User is offline

  • New D.I.C Head

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

Re: Power of (Exponentiation) trouble...

Posted 07 December 2009 - 03:31 PM

Ok, thanks for clearing that up ! :D
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
Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1102
  • View blog
  • Posts: 4,597
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 Komaqtion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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) ? :o

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;
			
		}
	
	}
	
	cout << "Your answer is: " << result << endl;
	
	cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
	cin.get();
	
	return 0;
	
}

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1