C++: Calculator

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1313 Views - Last Post: 24 October 2010 - 02:00 PM Rate Topic: -----

#1 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

C++: Calculator

Posted 24 October 2010 - 10:21 AM

How do i use exponents in a for loop??
Is This A Good Question/Topic? 0
  • +

Replies To: C++: Calculator

#2 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++: Calculator

Posted 24 October 2010 - 10:27 AM

#include <iostream>
int main() {
   for(int i=0;i<10;i++)
      std::cout<<"exponents"<<std::endl;
   return 0;
}


Just kidding :P.
Your question isn't clear.Can you give us more details? What are you trying to do?
Was This Post Helpful? 0
  • +
  • -

#3 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

Re: C++: Calculator

Posted 24 October 2010 - 10:35 AM

I have to write a program that simulates a calculator. The user should be able to input an expression with two values and an operator (+,-,*,/,^,and %) and the computer will print out the proper result. Your calculator should include the ^ operator for exponentiation.

I want to know how to put 5^5 in for loop but what if the user wants to put in a greater power than 5 how do I do that?? Hopefully this is clear enough to understand
Was This Post Helpful? 0
  • +
  • -

#4 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++: Calculator

Posted 24 October 2010 - 10:41 AM

Ok let's start from your code for 5^5. Post your code.
The aim is to generalize from 5^5 to 5^y where y will be entered by user (using scanf or cin or whatever).
And the next step would be to generalize 5^y to x^y where x too is entered by user.

This post has been edited by Munawwar: 24 October 2010 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#5 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

Re: C++: Calculator

Posted 24 October 2010 - 10:46 AM

Well i dont know if im the right track but this is what i have so far.

//cpp4.com
//trae marshall
//making a calculator

#include "iostream.h"

int main()
{
	char abc,
	     num;

	int input_YI,
		  TY,
		  impossible,
		  invalid_O,
		  result,
		  invalid_N;

	
//Input your Expression


for(num = 1, num <= 9, ++num);
{
   cout << endl;


MOD EDIT: When posting code...USE CODE TAGS!

:code:

This post has been edited by JackOfAllTrades: 25 October 2010 - 04:19 AM

Was This Post Helpful? 0
  • +
  • -

#6 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 145
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: C++: Calculator

Posted 24 October 2010 - 10:52 AM

Change
#include "iostream.h"
to
#include <iostream>


Hope that is helpful :)
Was This Post Helpful? 0
  • +
  • -

#7 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

Re: C++: Calculator

Posted 24 October 2010 - 11:00 AM

//cpp4.com
//trae marshall
//making a calculator

#include <iostream.h>

int main()
{
	char abc,
		 num,
		 y,
		 x;

	int input_YI,
		TY,
		impossible,
		invalid_O,
		result,
		invalid_N;

	
//Input your Expression


for(num = 1; num <= y; ++num);
{
	cout << endl;
	num = 1; num <= y; ++num;
}


WHAT CORRECTIONS DO I NEED TO MAKE??? IF ANY??

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 25 October 2010 - 04:20 AM

Was This Post Helpful? 0
  • +
  • -

#8 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++: Calculator

Posted 24 October 2010 - 11:19 AM

Nah, lets start from scratch.

Let's say we need to calculate 5^3. Let's do it without a for loop first.
We need a variable that will store the result.
int result=1;



Then we multiply result with the number 5, exactly 3 times:
result*=5; //OR result=result*5;
result*=5;
result*=5;


(Similarly if you want 5^y you would write the statement y number of times.)

Now write a for loop to do this.
The loop should count from 1 to 3 for doing this.
for(int i=1;i<=3;i++) {
   result*=5;
}


Now, can you modify this code for looping from 1 to y so that you can get 5^y? Try it and post back

EDIT: Post code between code tags.

This post has been edited by Munawwar: 24 October 2010 - 11:23 AM

Was This Post Helpful? 0
  • +
  • -

#9 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

Re: C++: Calculator

Posted 24 October 2010 - 12:04 PM

//cpp4.com
//trae marshall
//making a calculator

#include "iostream.h"

int main()
{
	char abc,
		 y;

	int input_YI,
		TY,
		impossible,
		invalid_O,
		result = 1,
		invalid_N;

	
//Input your Expression


for(result = 1; result <= y; result++);
{
	cout << endl;
	result *= y;
}


CORRECT???

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 25 October 2010 - 04:20 AM

Was This Post Helpful? 0
  • +
  • -

#10 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 231
  • Joined: 07-July 10

Re: C++: Calculator

Posted 24 October 2010 - 12:07 PM

Try compiling it and find out. Don't guess; deduce.
Was This Post Helpful? 0
  • +
  • -

#11 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++: Calculator

Posted 24 October 2010 - 12:18 PM

We'll No. To find 5^y you should do result*=5; not result*=y;.
And y should be int, not char.

Also y should be equal to something.You can ask a the user for an input:
cout<<"Finding 5^y. Enter the value for y:"<<endl;
cin>>y;



Correct the code and post again. And post between code tags. You can get the code tags by using the <> button on the editor toolbar.

This post has been edited by Munawwar: 24 October 2010 - 12:18 PM

Was This Post Helpful? 0
  • +
  • -

#12 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 145
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: C++: Calculator

Posted 24 October 2010 - 12:18 PM

Also post code in code tags :code:

and you're still using
#include "iostream.h"

it should be
#include <iostream> // no h and <> instead of ""

This post has been edited by Alex6788: 24 October 2010 - 12:19 PM

Was This Post Helpful? 0
  • +
  • -

#13 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

Re: C++: Calculator

Posted 24 October 2010 - 12:19 PM

//cpp4.com
//trae marshall
//making a calculator

#include "iostream.h"

int main()
{
	char abc;
		 

	int input_YI,
		thank_you,
		impossible,
		invalid_O,
		result = 1,
		invalid_N,
		y = 1;

	
//Input your Expression


for(result = 1; result <= y; result++);
{
	cout << endl;
	result *= y;
}



return 0;
}


THESE ARE MY ERRORS:

--------------------Configuration: Cpp4 - Win32 Debug--------------------
Compiling...
Cpp4.cpp
C:\Users\Owner\Desktop\C++ Programs\Cpp4.cpp(13) : warning C4101: 'thank_you' : unreferenced local variable
C:\Users\Owner\Desktop\C++ Programs\Cpp4.cpp(9) : warning C4101: 'abc' : unreferenced local variable
C:\Users\Owner\Desktop\C++ Programs\Cpp4.cpp(14) : warning C4101: 'impossible' : unreferenced local variable
C:\Users\Owner\Desktop\C++ Programs\Cpp4.cpp(12) : warning C4101: 'input_YI' : unreferenced local variable
C:\Users\Owner\Desktop\C++ Programs\Cpp4.cpp(15) : warning C4101: 'invalid_O' : unreferenced local variable
C:\Users\Owner\Desktop\C++ Programs\Cpp4.cpp(17) : warning C4101: 'invalid_N' : unreferenced local variable

Cpp4.obj - 0 error(s), 6 warning(s)

WHAT DOES THAT MEAN???

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 25 October 2010 - 04:21 AM

Was This Post Helpful? 0
  • +
  • -

#14 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: C++: Calculator

Posted 24 October 2010 - 12:31 PM

You don't need so many variables:
int input_YI,
thank_you,
impossible,
invalid_O,
result = 1,
invalid_N,
y = 1;



The ones you need right now are:
int result = 1, y = 1;



Now follow the advice at post #11 and #12.

This post has been edited by Munawwar: 24 October 2010 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#15 trae_marshall901  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 24-October 10

Re: C++: Calculator

Posted 24 October 2010 - 12:39 PM

I want to be able to use any number i was just using 5 as an example
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2