13 Replies - 2723 Views - Last Post: 07 March 2009 - 11:43 AM Rate Topic: -----

#1 msbrownsugah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 09

Need some assistance with calculator program

Post icon  Posted 04 March 2009 - 09:14 PM

Hello
I have a huge issue which may not be that big but I am a little stumped. I don't think I have the whole code and may be missing some pieces so I just need some help getting this puzzle together.

Assignment is: write a prog which acts like a calculator for integers. it should perform as such:
1.) ask user if they would like to start a new calculation. if they say yes initialize the result to 0
2.)ask user what operation they would like to perform (add=1,sub=2,mult=3,divide=4)
3.)ask user for integer value they want to add/sub/mult/divide
4.)perform oper. with following restrictions...you may only use add and subtract for all operations
5.)display result of operation
6.) ask user if they would like to perform another operation in this calculation, start over, or quit

THANKS FOR ANY HELP THAT WILL BE GIVEN.

output as such:
enter the operation you which to perform (enter 1 for add, 2 for subtract,
3 for multiply, 4 for divide): 1
enter the value you wish to add: 4
0 + 4 = 4
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 2
enter the value you wish to subtract: 2
4 - 2 = 2
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 3
enter the value you wish to multiply by: 2
2 * 2 = 4
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 4
enter the value you wish to divide by: 3
4/3 = 1
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 2
enter the value you wish to subtract: 4
1 - 4 = 3
would you like to perform another operation (enter 1 for yes, 0 for no): 0
would you like to perform another calculation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 3
enter the value you wish to multiply by: 6
0 * 6 = 0
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 4
enter the value you wish to divide by: 4
0/4 = 0
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 1
enter the value you wish to add: 4
0 + 4 = 4
would you like to perform another operation (enter 1 for yes, 0 for no): 1
enter the operation you which to perform (1 for add, 2 for subtract,
3 for multiply, 4 for divide): 4
enter the value you wish to divide by: 0
4/0 error - division by 0
would you like to perform another operation (enter 1 for yes, 0 for no): 0
would you like to perform another calculation (enter 1 for yes, 0 for no): 0




#include <stdio.h> /* C standard input & output library*/
int main (int argc, char **argv) /* declare the main function*/
{
	char run='n';
	printf("Do you want to run the calculator? (Y/N):");
	scanf("%c",&run);
	if(run=='Y')
	{
	}
	if(run=='Y'||run=='y')
	{
	}
	printf("Do you want to run the calculator? (Y/N):");
	scanf("%c",&run);
	while(run=='Y'||run=='y')
	{
		printf("Do you want to run the calculator,again? (Y/N:");
		scanf("%c",&run);
	}
	int result=0;
	int choice=0;
	printf("Enter the operation you wish to perform:(1=add,2=subtract,3=multiply,4=divide)");
	scanf("%d",&choice);
	string selection="";
	if(choice==1)
	{
		selection= "add:";
	}
	if(choice==2)
	{
		selection= "subtract:";
	}
	if(choice==3)
	{
		selection= "multiply by:";
	}
	if(choice==4)
	{
		selection= "divide by:";
	}
	int num=0;
	printf("Enter the value you wish to %s",selection);
	scanf("%d",&num);
	int answer=0;
	if(choice==1)
	{
		answer=result+num;
		printf("%d+%d=%d",result,num,answer);
		result=answer;
	}
	if(choice==2)
	{
		answer=result-num;
		printf("%d-%d=%d",result,num,answer);
		result=answer;
	}
	if(choice==3)
	{
		answer=result*num;
		printf("%d*%d=%d",result,num,answer);
		result=answer;
	}
	if(choice==4)
	{		
		if(num==0)
		{
			printf("%d/%d error-division by 0", result,num);
		}
		else 
		{
			answer=result/num;
			printf("%d/%d=%d",result,num,answer);
			result=answer;


Is This A Good Question/Topic? 0
  • +

Replies To: Need some assistance with calculator program

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Need some assistance with calculator program

Posted 04 March 2009 - 09:22 PM

What exactly are you having problems with?
Was This Post Helpful? 0
  • +
  • -

#3 msbrownsugah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 09

Re: Need some assistance with calculator program

Posted 04 March 2009 - 09:26 PM

PROBLEM: I DON'T THINK I HAVE ALL THE VARIABLES CORRECT. I GOT HELP FROM INSTRUCTOR WHICH LEAD ME TO THIS PILE OF STUFF... I STARTED TAKING OUT SOME THINGS..

WHEN I SET UP FOR DO U WANT TO RUN CALCULATOR..Y/N ...THAT WORKS...
NOW I GET ABOUT 10 ERRORS..ABOUT (;) MISSING...AND IT WONT RUN. IM CONFUSED AND NOT SURE WHAT I NEED TO DO QUITE HONESTLY.

OH AND I HAVE THE MULTIPLICATION AND DIVISION WRONG... HOW WOULD I GET IT TO MULTIPLY / DIVIDE...WITH JUST USING ADD AND SUBTRACT

** Edit ** No need to quote yourself
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need some assistance with calculator program

Posted 04 March 2009 - 09:44 PM

if the compiler tells you that a ; or a ) is missing, it also tells you which line, so you should be able to find and fix those errors. For example, I just noticed this one:
	  printf("Do you want to run the calculator,again? (Y/N:");

Notice that the number of ( and ) aren't balanced -- you're missing a ) at the end.

Regarding multiplication and division:
you can multiply just by adding repeatedly (in a loop). For example 3 x 4 is the same as 3 + 3 + 3 + 3.

And remember that INTEGER division truncates (discards remainders), so 9 / 2 = 4. Well, you can also do that by subtracting, and counting how many times you can subtract the divisor until the remainder is less than the divisor. That is:
9 - 2 - 2 - 2 - 2 = 1.
You can subtract 2 from 9 4 times, so the answer (9/2) is 4.
Was This Post Helpful? 0
  • +
  • -

#5 msbrownsugah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 09

Re: Need some assistance with calculator program

Posted 04 March 2009 - 10:02 PM

scanf("%d",&choice);
I receive error stating undeclaired identifier

'=' : 'int' differs in levels of indirection from 'char [5]'
selection= "add:";
this is on ever selection statement. should i put int selection; to store for computer?

: error C2143:
int num=0;
this is on other int statements as well.

This post has been edited by msbrownsugah: 04 March 2009 - 10:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need some assistance with calculator program

Posted 04 March 2009 - 10:32 PM

Those errors don't make sense in terms of the code snippets you are providing. The code you posted is incomplete, and when I try to compile just the initial menu (by terminating the main function after "selection="divide by") the only error I get is because you are trying to use c++ strings but you didn't #include <string>
and you didn't write
using namespace std;
at the beginning of the program.

Are you supposed to be using C or C++. If C++, why are you using C I/O instead of C++ I/O functions?
If C, why are you using C++ strings?

Better decide which language you want to use, and post your complete code so we can see exactly where your errors are coming from.
Was This Post Helpful? 0
  • +
  • -

#7 msbrownsugah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 09

Re: Need some assistance with calculator program

Posted 04 March 2009 - 10:37 PM

Thanks. I will go look through my book. I may have misunderstood my prof. and others that assisted. It is suppose to all be in C.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need some assistance with calculator program

Posted 04 March 2009 - 11:16 PM

View Postmsbrownsugah, on 5 Mar, 2009 - 12:37 AM, said:

It is suppose to all be in C.

In that case, ignore what I said about <string> and namespace std, but do read what I wrote about multiply and divide in post #4.

For C, all of your variable declarations should be at the beginning of the function. I see declarations/definitions "int result=0;", "int choice=0;", "int num = 0" and "int answer = 0" scattered all over your code. Some compilers may allow that, others will not. Better to put them all together at the beginning.

Another suggestion: you can eliminate all of this:
	string selection="";
	if(choice==1)
	{
		selection= "add:";
	}
	if(choice==2)
	{
		selection= "subtract:";
	}
	if(choice==3)
	{
		selection= "multiply by:";
	}
	if(choice==4)
	{
		selection= "divide by:";
	}


and instead use an array of string constants (which also should be defined with the other variables at the beginning of your code):
char* selection[] = {"add", "subtract", "multiply by", "divide by"};

Then when you want to use them:
printf("Enter the value you wish to %s",selection[choice]);
(assuming you have already read in the value for choice)

You also have some "junk" near the beginning of your code that's not doing anything:
	if(run=='Y')
	{
	}
	if(run=='Y'||run=='y')
	{
	}



Keep working at it.
Was This Post Helpful? 0
  • +
  • -

#9 msbrownsugah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 09

Re: Need some assistance with calculator program

Posted 05 March 2009 - 10:13 PM

Ok..I been working on this most of the day...so I decided to back track..work this out step by step so i can fully understand for myself.

 #include <stdio.h> /* C standard input & output library*/
int main (int argc, char **argv) /* declare the main function*/
{
	int result=0;
	int choice=0;
	int num=0;
	int answer=0;
	int group;
	
	char run='n';
	printf("Do you want to run the calculator? (Y/N):");
	scanf("%c",&run);
	while(run=='Y'||run=='y')
	{
		printf("Enter the operation you wish to perform:(1=add,2=subtract,3=multiply,4=divide)");
		scanf("%d",&choice);
	}	
	if(choice==1){
		printf("add");
		scanf("%d",&num);
		
	}
	if(choice==2){
		printf("subtract");
	}
	if(choice==3){
		printf("multiply by");
	}
	if(choice==4){
		printf("divide by");
	}	
	


I have deleted up to this part.. all work okay except when i prompt user to choose what operation to use.. i added scanf("%d",&num); after if(choose==1) ..not sure if thats right just testing stuff out because i am unsure.. my output is it keep asking to choose operation no matter what number i select.
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need some assistance with calculator program

Posted 05 March 2009 - 10:33 PM

When you write
while(run=='Y'||run=='y')
{ // some code
}


it will keep doing that "some code" over and over as long as the expression (run=='Y'||run=='y') is true. Since "some code" in your program doesn't change the value of run, it will keep doing that forever.

In this case, what you want is "if", not "while".

... that is, unless you really want the calculator to keep running, i.e., do the user's operation and then return to that menu to do another operation.

If that's what you want, then you can put ALL of the calculator code inside the {} brackets of the while loop, and add one more choice, "quit", to the menu. If the user selects "quit", it should change the value of run to 'n' to get out of the loop.

This post has been edited by r.stiltskin: 05 March 2009 - 10:40 PM

Was This Post Helpful? 1
  • +
  • -

#11 msbrownsugah  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-March 09

Re: Need some assistance with calculator program

Posted 06 March 2009 - 07:22 PM

ok finally i have the program working pretty well. i am just stumped on how to insert a loop to keep it prompting user if they want to start completely over, start with enter operation, or quit.

 #include <stdio.h> /* C standard input & output library*/
int main (int argc, char **argv) /* declare the main function*/
{
	int result=0;
	int choice=0;
	int num=0;
	int answer=0;			
	char run='n';
	

	printf("Do you want to run the calculator? (Y/N):");
	scanf("%c",&run);
	if(run=='Y'||'y')
	{
		printf("Enter the operation you wish to perform:(1=add,2=subtract,3=multiply,4=divide)");
		scanf("%d",&choice);
	}
	printf("Enter the integer value you wish to ");
	if(choice==1){
		printf("add");
	}
	if(choice==2){
		printf("subtract");
	}
	if(choice==3){
		printf("multiply by");
	}
	if(choice==4){
		printf("divide by");
	}
	
	scanf("%d",&num);

	if(choice==1){
		answer=result+num;
		printf("%d+%d=%d",result,num,answer);
		result=answer;
	}
	else if	(choice==2){
		answer=result-num;
		printf("%d-%d=%d",result,num,answer);
		result=answer;
	}
	else if(choice==3){
		int temp=num;
		while(temp>0){
		  answer=answer+result;
		  temp--;
		}
		printf("%d*%d=%d",result,num,answer);
		result=answer;
	}
	else if(choice==4){
		if(num==0)
		{
			printf("%d/%d error-division by 0",result,num);
		}
		else{
			int temp=num;
			while (temp>0){
				answer=answer-result;
				temp--;
			}
			printf("%d/%d=%d",result,num,answer);
			result=answer;
		


Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need some assistance with calculator program

Posted 06 March 2009 - 10:13 PM

It's very easy to get it to return to choosing an operation:

Where you have
if(run == 'Y'||run == 'y')
oops, I just noticed you have an error there. You can't say (run == 'Y'||'y'). You must have a complete logical expression on both sides of the ||. See how I wrote it above.

Anyway, where you have "if", just change it to "while". Also, remove the closing bracket } of that "if" block and put it all the way down at the bottom of your code, so all of the calculator code will be inside the "while" block.

Add one more option to the prompt for "choice": 5=quit

And you'll have to add code to handle breaking out of the loop: if(choice==5) { ... } I'll let you work that out. (Are you familiar with "break"?)

------------

If you also want to offer the option to repeat the same operation but with different numbers, you'll have to put each operation's code in its own separate while block, and just before the end of each one offer a prompt asking if the user wants to "do it again" or "return to main menu" or "quit", plus code to respond to each of those choices, so it get's a little more involved but you should be able to handle it.

-------------

I also noticed one glaring omission in your code. All of those operations take two operands, but you are only asking the user for one and are assuming that the other operand is 0.

Also, you have variables called "num", "answer" and "result" which is confusing since answer and result both mean essentially the same thing, and num is not very informative either. It's preferable to have variable names that describe what they mean. In this case maybe "operand1", "operand2" and "result", for example.
Was This Post Helpful? 0
  • +
  • -

#13 Newfie Ken  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 30
  • Joined: 22-February 09

Re: Need some assistance with calculator program

Posted 07 March 2009 - 11:39 AM

#include <stdafx.h>
#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int add(), subt(), mult(), div(), option(), sqrt(), sqr(), help(), about(), pow(), ee(), choice, neweq;
double a, b, result, power;
#define PI 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706


int main()
{
	system ("COLOR 0c");
	system ("TITLE Calculator");
  do
	{
	system ("CLS");
	cout << "----------------------------------------------------------------- Information -" << endl << endl;

	cout << "1.   Help" << endl;
	cout << "2.   About" << endl << endl;

	cout << "------------------------------------------------------------------ Operations -" << endl << endl;

	cout << "3.   Add" << endl;
	cout << "4.   Subtract" << endl;
	cout << "5.   Multiply" << endl;
	cout << "6.   Divide" << endl;
	cout << "7.   Square Root" << endl;
	cout << "8.   Square a Number" << endl;
	cout << "9.   X ^ Y" << endl;
	cout << "10.  EE" << endl << endl;

	cout << "------------------------------------------------------------------- Constants -" << endl << endl;

	cout << "PI = " << PI << endl << endl;

	cout << "------------------------------------------------------------------------ Task -" << endl << endl;

	cout << "Which task would you like to perform?" << endl << endl;
	cout << "Task: ";
	cin >> choice;
	switch (choice)
	{
		   case 1: help();
		   break;
		   case 2: about();
		   break;
		   case 3: add();
		   break;
		   case 4: subt();
		   break;
		   case 5: mult();
		   break;
		   case 6: div();
		   break;
		   case 7: sqrt();
		   break;
		   case 8: sqr();
		   break;
		   case 9: pow();
		   break;
		   case 10: ee();
		   break;
	}
	}
	while (option() == 1);
}
int add()
{
	cout << endl << "Number 1:";
	cin >> a;
	cout << endl << "Number 2:";
	cin >> b;
	result = a + b;
	cout << endl << a << " + " << b << " = " << result << endl << endl;

	return 0;
}
int subt()
{
	cout << endl << "Number 1:";
	cin >> a;
	cout << endl << "Number 2:";
	cin >> b;
	result= a - b;
	cout << endl << a << " - " << b << " = " << result << endl << endl;
	return 0;
}
int mult()
{
	cout << endl << "Number 1:";
	cin >> a;
	cout << endl << "Number 2:";
	cin >> b;
	result = a * b;
	cout << endl << a << " * " << b << " = " << result << endl << endl;
	return 0;
}
int div()
{
	cout << endl << "Number 1:";
	cin >> a;
	cout << endl << "Number 2:";
	cin >> b;
	result= a / b;
	cout << endl << a << " / " << b << " = " << result << endl << endl;
	return 0;
}
int sqrt()
{
	cout << endl << "Take the square root of:";
	cin >> a;
	result = sqrt (a);
	cout << endl << result << endl << endl;
	return 0;
}
int sqr()
{
	cout << endl << "Square this number: ";
	cin >> a;
	result = a * a;
	cout << endl << result << endl << endl;
	return 0;
}
int pow()
{
	cout << endl << "Base: ";
	cin >> a;
	cout << endl << "Power: ";
	cin >> b;
	result = pow(a, b);
	cout << endl << a << " ^ " << b << " = " << result << endl << endl;
	return 0;
}
int ee()
{
	cout << endl << "Number: ";
	cin >> a;
	cout << endl << "Power: ";
	cin >> b;
	result = a * (pow(10, b));
	cout << endl << a << " * 10 ^ " << b << " = " << result << endl << endl;
	return 0;
}
int help()
{
	cout << endl << "Choose a operation which you would like to perform by entering the number which corresponds to it. Follow the instructions that are given." << endl << endl;
	return 0;
}
int about()
{
	cout << endl << "Version 1" << endl;
	cout << "This calculator was made by Newfie Ken" << endl;
	cout << "Copyright 2009" << endl << endl;
	return 0;
}
int option()
{
	cout << "What would you like to do now?" << endl << endl;
	cout << "1. New" << endl;
	cout << "2. Esc" << endl << endl;
	cout << "Choice: ";
	cin >> neweq;
	return neweq;
}



Here's what you need and then some more :). You can take out the swuare root and other stuff if you want.
Was This Post Helpful? 0
  • +
  • -

#14 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Need some assistance with calculator program

Posted 07 March 2009 - 11:43 AM

View PostNewfie Ken, on 7 Mar, 2009 - 01:39 PM, said:

Here's what you need and then some more :). You can take out the swuare root and other stuff if you want.


The purpose of this forum is to help people learn to program. Not to supply them with YOUR code to hand in for THEIR homework.

That's called cheating, not helping.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1