# how to program calculator using while loops

Page 1 of 1

## 3 Replies - 23975 Views - Last Post: 31 August 2009 - 05:33 AMRate 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=122683&amp;s=f37603c39d17ff3c266ab59e86462d3a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 fairuz

Reputation: 0
• Posts: 1
• Joined: 28-August 09

# how to program calculator using while loops

Posted 28 August 2009 - 08:09 AM

```	  #include <iostream>

#include <string>

using namespace std;

int result;

char sign;

int value;

int main()

{

result = 0;

// Loop forever (or till we hit the break statement)

do

{

cout << "Current value is " << result << '\n';

cout << "Please enter an operation +, -, *, / <'Q' to quit>: ";

cin >> sign;
cout << "Please enter a number: ";

cin >> value;

if (sign != '+' && sign != '-' && sign != '*' && sign != '/')

{

cout << "Unknown operator " << sign << '\n';

}

else

{

if (sign = '+')
{

result += value;

}

else

{

if (sign = '-')

{

result -= value;

}

else

{

if (sign = '*')

{

result *= value;

}

else

{

if (sign = '/')

{

result /= value;

}

}

}

}

}

}

while (1);

return (0);

}
```

Is This A Good Question/Topic? 0

## Replies To: how to program calculator using while loops

### #2 rgfirefly24

• D.I.C Lover

Reputation: 448
• Posts: 2,184
• Joined: 07-April 08

## Re: how to program calculator using while loops

Posted 28 August 2009 - 08:24 AM

couple of things i see.

1. you have no break operation to get out of the loop
2. If the user enters Q you tell them its not a valid entry and continue on inside the loop.
3. You are using alot of nested if statements. What you should do is use if...else if...else

1.
You'll want to add one of the following statements to an If check

return 0, exit(0), or Break;

Explanation of the above:

Early Termination

2.
Use an else if statement to say that if sign = 'Q' OR sign = 'q' break from the program using one of the operations in number 1

3.

here is your operands in an if...else if...else format
```if (sign = '+')
{
result += value;
}
else if (sign = '-')
{
result -= value;
}
else if (sign = '*')
{
result *= value;
}
else if (sign = '/')
{
result /= value;
}

```

here is your code fixed up to add in the break and to clean up the nested ifs

```#include <iostream>
#include <string>

using namespace std;

int result,value;
char sign;

int main()
{
result = 0;

// Loop forever (or till we hit the break statement)
do
{
cout << "Current value is " << result << endl;

cout << "Please enter an operation +, -, *, / <'Q' to quit>: ";

cin >> sign;
cout << "Please enter a number: ";

cin >> value;

if (sign = '+' || sign = '-' || sign = '*' || sign = '/')
{
if (sign = '+')
{
result += value;
}
else if (sign = '-')
{
result -= value;
}
else if (sign = '*')
{
result *= value;
}
else if (sign = '/')
{
result /= value;
}
}
else if (sign = 'q' || sign = 'Q')
{
break;
}
else
{
cout << "Unknown operator " << sign << endl;
}
}while(1);

return 0;
}

```

This post has been edited by rgfirefly24: 28 August 2009 - 08:36 AM

### #3 Elcric

• D.I.C Regular

Reputation: 102
• Posts: 453
• Joined: 02-May 09

## Re: how to program calculator using while loops

Posted 28 August 2009 - 09:32 AM

Hello,

Very nice program.

The computed did what you told it to do instead of what you wanted it to do.

You told the compute =

When you wanted the compute to do ==

I make this mistake all the time.

Take a look at the following and see what I mean.

```#include <string>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
int result = 0;
char sign = ' ';
int value = 0;

// Loop forever (or till we hit the break statement)

do
{
system("CLS");
cout << endl << endl;
cout << "Current value is " << result << endl << endl;
cout << "Please enter an operation +, -, *, /, or Q to quit," << endl << endl;
cout << "then press Enter ==> ";
cin >> sign;
cout << endl << endl;
cout << "You entered " << sign;
cout << endl << endl;
system("PAUSE");
cout << endl << endl;
if ((sign == 'Q') || (sign == 'q'))
{
return EXIT_SUCCESS;
}
cout << "Please enter a number then press Enter ==> ";
cin >> value;
cout << endl << endl;
cout << "You entered " << value;
cout << endl << endl;
system("PAUSE");
cout << endl << endl;

if (sign != '+' && sign != '-' && sign != '*' && sign != '/')
{
cout << "Unknown operator " << sign << '\n';
}else
{
if (sign == '+')
{
result += value;
}else
{
if (sign == '-')
{
result -= value;

}else
{
if (sign == '*')
{
result *= value;
}else
{
if (sign == '/')
{
result /= value;
}
}
}
}
}

cout << "The result is " << result;
cout << endl << endl;
system("PAUSE");
cout << endl << endl;

}while((sign!='Q') && (sign!='q'));
system("PAUSE");
return EXIT_SUCCESS;
}

```

### #4 rgfirefly24

• D.I.C Lover

Reputation: 448
• Posts: 2,184
• Joined: 07-April 08

## Re: how to program calculator using while loops

Posted 31 August 2009 - 05:33 AM

bah thanks for the catch, i've been doing vbscript for the last 8 months and didn't catch myself there