3 Replies - 10622 Views - Last Post: 31 August 2009 - 05:33 AM Rate Topic: -----

#1 fairuz  Icon User is offline

  • New D.I.C Head

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

how to program calculator using while loops

Posted 28 August 2009 - 08:09 AM

:blink:


	  #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  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 284
  • View blog
  • Posts: 1,501
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

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

Re: how to program calculator using while loops

Posted 28 August 2009 - 09:32 AM

Hello, :D

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;
}


Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 284
  • View blog
  • Posts: 1,501
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1