1,105,229 Community Members

Write a calculator program using a do-while loop

Member Avatar
allialli
Newbie Poster
23 posts since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hey. i would really appreciate some help on an assignment i've been struggling with.
i am supposed to:
Write a calculator program. Each time the program starts, reset the current value to zero. The user will then enter an arithmetic operator (either +, -, * or /) and then a number (double). Your program will respond by performing that operation on the current value and displaying the result. The program will stop executing when the user enters a ‘Q’ for the operation. For an extra challenge (not required), make sure the program does not try to divide by zero (see the example below for what the program should display in that case.) YOU MUST USE A DO…WHILE AS THE MAIN LOOP FOR THIS PROGRAM.


so far i have:

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

a problem i am having is that it only adds numbers and doesn't perform the other operations. can anyone please help?
thanks!

Member Avatar
Ancient Dragon
Achieved Level 70
27,572 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

>>if (sign = '+')
line 31 and others similar are using the assignment operator = instead of boolean ==.

Member Avatar
Majestics
Practically a Master Poster
694 posts since Jul 2007
Reputation Points: 84 [?]
Q&As Helped to Solve: 66 [?]
Skill Endorsements: 6 [?]
 
0
 

ALREADY ANCIENT DRAGON EXPLAINED THE PROBLEM ONE MORE POINT IN YOUR LOGIC IS TERMINATION OF THE PROGRAMME

USE

while(sign!='Q')

so when you enter Q then it will terminate the programe else it will do your respected task.

Member Avatar
allialli
Newbie Poster
23 posts since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you both for the help!

however when i enter either 'Q' or 'q', my program still does not terminate like it should. do you have any suggestions??
thanks!

#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 != '/' && sign != 'Q' && sign != 'q')
		{
			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 (sign != 'Q' && sign != 'q');

    return (0);
}
Member Avatar
Ancient Dragon
Achieved Level 70
27,572 posts since Aug 2005
Reputation Points: 5,231 [?]
Q&As Helped to Solve: 3,028 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

add cin.ignore(); after line 21 to clear the keyboard of the '\n' (Enter key). Also read this thread.

Member Avatar
allialli
Newbie Poster
23 posts since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i added the cin.ignore() ; but it still prompts me for a number even after i enter Q. i have used cin.ignore(); before so thanks for reminding me of that! i just dont know what i did wrong that it wont let me terminate the program when the user enters Q or q??

#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;
		cin.ignore();


		if (sign != '+' && sign != '-' && sign != '*' && sign != '/' && sign != 'Q' && sign != 'q')
		{
			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 (sign != 'Q' && sign != 'q');

    return (0);
}
Member Avatar
WonderWomen204
Newbie Poster
21 posts since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hey there. I am doing the same program and I have figured out how to get everything to work that is requried, including getting the program to end, when entering Q. I also attempted to try and have the program give, "ERROR! DIVIDE BY ZERO!" but couldn't figure out how to get the program to end after it says that and since that part is extra credit and optional, I just left it in there and hopefully the professor will give some extra credit for trying...lol. Anyway, here below is the final program:

#include <iostream>
#include <string>

using namespace std;

int main()
{
	//input declarations as doubles for total and counter
	double total = 0, counter = 0;
	//input declarations sign and Q as chars
	char sign, Q = 0;
	//input declaration value as double
	double value;

		//A do..while will loop forever (or until we hit the break statement)
		do 
		{
			//The current value is 0
			cout << "Current value is " << total << endl;
			
			//Please enter an operation
			cout << "Please enter an operation +, -, *, / ('Q' to quit) ";
			cin >> sign;

			//If the operation is Q, will end the program
			if (sign != 'Q')
			{
				//If the operation is not Q, please enter a number.
				cout << "Please enter a number: ";
				cin >> value;
				cin.ignore();

				/* If the value input is <=0, you are an idiot b/c you can't divide 
				   anything by zero. */ 
				if (value <= 0)
				{
					cout << "ERROR! DIVIDE BY ZERO!! "; 
					cout << endl;
				}

				//Otherwise procede with the calulator program
				else
				{
				//If the operation is equal to '+', then the total is added.
				if (sign == '+')
				{
					total += value;
				}
				/* If the operation is equal to '-', then the value is subtracted from
				   the previous number input. */ 
				else
				{
					if (sign == '-')
					{
						total -= value;
					}
					/* If the operation is equal to '*', then the value is multiplied to
					   the previous number input. */ 
					else
					{
						if (sign == '*')
						{
							total *= value;
						}
						/* If the operation is equal to '/', then the value is divided by
						   the previous number input. */ 
						else 
						{
							if ((sign == '/') && (value != 0))
							{
								total /= value;
							}
							

						}
					}
				}
				}
			
		}
	}
		//While the operation is not equal to 'Q', the program will run.
		//Otherwise, if the operation is equal to Q, the program will end. 
		while (sign != 'Q');
			
		//Each time the program runs, reset the current value to zero.
		return (0);	
	
			
}

There you go. Hope this helps and take care.

Member Avatar
roverz
Newbie Poster
1 post since Sep 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

please help me to make a calculator program guys .

Member Avatar
Zulkaif dilawar
Newbie Poster
1 post since Apr 2011
Reputation Points: -8 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

if (sign!='+'||sign!='-'||sign!='/'||sign!='*')
/* use || operator instead of && operator*/
cout<<"unknown operator "<<sign<<" \n";
else
{
if (sign=='+')
/* Use the double equal sign here instead of writing if (sign='+')*/
/*and at the end while condition would be like this while(sign!='Q'||sign!='q');
instead of while(1); or while(sign!='Q'&& sign!='q');*/
Please do these changes and I give you surety for its right working. thanks.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article