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!

Recommended Answers

All 8 Replies

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

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.

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

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

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

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.

please help me to make a calculator program guys .

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.

commented: bump 3-year-old post, and the info is wrong too! -4
commented: 3 years too late, no code tags -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.