# C++: Calculator

• (2 Pages)
• 1
• 2

## 20 Replies - 1733 Views - Last Post: 24 October 2010 - 02:00 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=196532&amp;s=4e825a42e75d3341416cff19340fa80f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 trae_marshall901

Reputation: 0
• 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

• D.I.C Regular

Reputation: 163
• 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 .
Your question isn't clear.Can you give us more details? What are you trying to do?

### #3 trae_marshall901

Reputation: 0
• 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

### #4 Munawwar

• D.I.C Regular

Reputation: 163
• Posts: 457
• Joined: 20-January 10

## Re: C++: Calculator

Posted 24 October 2010 - 10:41 AM

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

### #5 trae_marshall901

Reputation: 0
• 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;

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

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

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

### #6 Alex6788

Reputation: 144
• Posts: 1,667
• Joined: 15-July 10

## Re: C++: Calculator

Posted 24 October 2010 - 10:52 AM

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

### #7 trae_marshall901

Reputation: 0
• 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;

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!!!

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

### #8 Munawwar

• D.I.C Regular

Reputation: 163
• 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

### #9 trae_marshall901

Reputation: 0
• 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;

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

CORRECT???

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

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

### #10 Coding in the name of

• D.I.C Regular

Reputation: 67
• Posts: 252
• Joined: 07-July 10

## Re: C++: Calculator

Posted 24 October 2010 - 12:07 PM

Try compiling it and find out. Don't guess; deduce.

### #11 Munawwar

• D.I.C Regular

Reputation: 163
• 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

### #12 Alex6788

Reputation: 144
• Posts: 1,667
• Joined: 15-July 10

## Re: C++: Calculator

Posted 24 October 2010 - 12:18 PM

Also post code in code tags

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

### #13 trae_marshall901

Reputation: 0
• 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;

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!!!

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

### #14 Munawwar

• D.I.C Regular

Reputation: 163
• 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;

```

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

### #15 trae_marshall901

Reputation: 0
• 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