Solved

# Help with calculator

Posted on 2014-02-16
241 Views

You've picked Compare
Pick a number
1
Pick another number
5
First Number is less then the Second Number
The answere is: <-- need to remove this
Press Enter to continue...

``````echo " \"Welcome\" to the most awsome calc evar"
echo "2) Subtract"	#subtract two numbers
echo "3) Multiply"	#multiply two numbers
echo "4) Divide"	#divide two numbers
echo "5) Factorial"	#Factorial two numbers
echo "6) Campare"	#Campare two numbers
echo "7) EXIT"		#exit the calculator
read CAL			#get number for selection on CALC
while [ \$CAL != 7 ]
#if CAL = to 7 will exit, if not continue next line
do
case \$CAL in			#database arrays
"1")
echo "Choose a number"
echo "Choose a another number to add to the first number"
VALUE=\$((\$NUM2+\$NUM1))
;;
"2")	echo "You've picked Subtract"
echo "Choose a number"
echo "Choose a another number to Subtract to the first number"
VALUE=\$((\$NUM1-\$NUM2))
;;
"3")	echo "You've picked Multiply"
echo "Choose a number"
echo "Choose a another to multiply"
VALUE=\$((\$NUM1*\$NUM2))
;;
"4")	echo "You've picked Divide"
echo "Choose a number"
echo "Choose a another to divide the first number"
VALUE=\$((\$NUM1/\$NUM2))
;;
"5")    echo "You've picked Factorial"
read NUM	# get a number
FOR=1
FOR=\$((\$FOR*\$NUM))	# FOR TIME NUM
NUM=\$((\$NUM-1))		# TAKE 1 OFF NUM
FOR=\$((\$FOR*\$NUM))	#FOR TIMES WITH NUM
NUM=\$((\$NUM-1))		#TAKE 1 OFF NUM
while [ \$NUM -gt 1 ]	# LOOP UNTIL NUM = 1
do
FOR=\$((\$FOR*\$NUM))
#times End by nu
NUM=\$((\$NUM-1))
#takes 1 off NUM
done
;;
"6") 	echo "You've picked Compare"
echo "Pick a number"			#ask number
echo "Pick another number"
if [ \$NUM1 -eq \$NUM2 ]		#if NUM1 = NUM2
then
echo "First Number is equal to Second Number"	#show anwser NUM1=NUM2
else			#or
if [ \$NUM1 -lt \$NUM2 ]		#NUM1 less less than NUM2
then
echo "First Number is less then the Second Number"		#show answer A less B
else
echo "First Number is Greater than Second Number"			#or answer is A is Greater than B
fi
fi
;;
*)

echo "YOU ENTERED INVAILD OPTIONS"
;;

clear
#space
echo " \"Welcome\" to the most awsome calc evar"
echo "2) Subtract"	#subtract two numbers
echo "3) Multiply"	#multiply two numbers
echo "4) Divide"	#divide two numbers
echo "5) Factorial"	#Factorial two numbers
echo "6) Campare"	#Campare two numbers
echo "7) EXIT"		#exit the calculator
read CAL	       #get number for selection on CALC
done
``````
0
Question by:stellarsystems
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 5
• 3
• 2
• +1

LVL 21

Expert Comment

ID: 39863785
What are you trying to remove?

On Line 82 you can change what you want to display on the screen:

``````echo "The answere is: \$VALUE"
``````
0

LVL 1

Author Comment

ID: 39863789
it all displays correctly except compare show the answer is after the if statements i need to stop this
0

LVL 11

Expert Comment

ID: 39863799
Either delete line 82 or put a # sign in the first column to ignore the line altogether.
0

LVL 1

Author Comment

ID: 39863812
no that wont word as i need that line for the rest of the calculator
0

LVL 11

Expert Comment

ID: 39863826
Then you need to rewrite where the line is output. I get it, the example you have shown us does not add or subtract, it compares and gives the answer. You do not need the line "The Answer Is:" in this case but in other cases you do.

You could do this in a number of ways... Put the answer statement within the case section that needs it. This will require many duplicate line. Better way would be to place the lines in a subroutine that is called from with the case statements. I am certain I could come up with more...
0

LVL 1

Author Comment

ID: 39863863
Thanks technoweeb could you give an example on how id put the answere statement within the case section for each area? iv been trying but keep getting syntax errros
0

LVL 1

Assisted Solution

stellarsystems earned 0 total points
ID: 39863923
I Think its working =)
spent a while but it now does what i want it too do thanks for the hints

``````echo " \"Welcome\" to the most awsome calc evar"
echo "2) Subtract"	#subtract two numbers
echo "3) Multiply"	#multiply two numbers
echo "4) Divide"	#divide two numbers
echo "5) Factorial"	#Factorial two numbers
echo "6) Campare"	#Campare two numbers
echo "7) EXIT"		#exit the calculator
read CAL			#get number for selection on CALC
while [ \$CAL != 7 ]
#if CAL = to 7 will exit, if not continue next line
do
case \$CAL in			#database arrays
"1")
echo "Choose a number"
echo "Choose a another number to add to the first number"
VALUE1=\$((\$NUM2+\$NUM1))
;;
"2")	echo "You've picked Subtract"
echo "Choose a number"
echo "Choose a another number to Subtract to the first number"
VALUE2=\$((\$NUM1-\$NUM2))
;;
"3")	echo "You've picked Multiply"
echo "Choose a number"
echo "Choose a another to multiply"
VALUE3=\$((\$NUM1*\$NUM2))
;;
"4")	echo "You've picked Divide"
echo "Choose a number"
echo "Choose a another to divide the first number"
VALUE4=\$((\$NUM1/\$NUM2))
;;
"5")    echo "You've picked Factorial"
read NUM	# get a number
FOR=1
FOR=\$((\$FOR*\$NUM))	# FOR TIME NUM
NUM=\$((\$NUM-1))		# TAKE 1 OFF NUM
FOR=\$((\$FOR*\$NUM))	#FOR TIMES WITH NUM
NUM=\$((\$NUM-1))		#TAKE 1 OFF NUM
while [ \$NUM -gt 1 ]	# LOOP UNTIL NUM = 1
do
FOR=\$((\$FOR*\$NUM))
#times End by nu
NUM=\$((\$NUM-1))
#takes 1 off NUM
done
echo "And the answer is \$FOR"
;;
"6") 	echo "You've picked Compare"
echo "Pick a number"			#ask number
echo "Pick another number"
if [ \$NUM1 -eq \$NUM2 ]		#if NUM1 = NUM2
then
echo "First Number is equal to Second Number"	#show anwser NUM1=NUM2
else			#or
if [ \$NUM1 -lt \$NUM2 ]		#NUM1 less less than NUM2
then
echo "First Number is less then the Second Number"		#show answer A less B
else
echo "First Number is Greater than Second Number"			#or answer is A is Greater than B
fi
fi
;;
*)

echo "YOU ENTERED INVAILD OPTIONS"
;;
esac				#end case

clear
#space
echo " \"Welcome\" to the most awsome calc evar"
echo "2) Subtract"	#subtract two numbers
echo "3) Multiply"	#multiply two numbers
echo "4) Divide"	#divide two numbers
echo "5) Factorial"	#Factorial two numbers
echo "6) Campare"	#Campare two numbers
echo "7) EXIT"		#exit the calculator
read CAL	       #get number for selection on CALC
done
``````
0

LVL 48

Accepted Solution

Tintin earned 500 total points
ID: 39863967
Easiest way is to use a function so as to not repeat code.

Here's an example that shows how to use a function.  As a bonus, I've fixed your spelling errors, corrected your indentation and fixed your factorial code.

``````#!/bin/bash
{
read -p "Press Enter to continue..."
}

while true
do
clear

cat <<EOF
"Welcome" to the most awesome calc ever

2) Subtract
3) Multiply
4) Divide
5) Factorial
6) Compare
7) EXIT

EOF

case \$CAL in
printf "Choose a number: "
printf "Choose a another number to add to the first number: "
VALUE1=\$((\$NUM2+\$NUM1))
;;
2)	echo "You've picked Subtract"
printf "Choose a number: "
printf "Choose a another number to Subtract to the first number: "
VALUE2=\$((\$NUM1-\$NUM2))
;;
3)	echo "You've picked Multiply"
printf "Choose a number: "
printf "Choose a another to multiply: "
VALUE3=\$((\$NUM1*\$NUM2))
;;
4)	echo "You've picked Divide"
printf "Choose a number: "
printf "Choose a another to divide the first number:"
VALUE4=\$((\$NUM1/\$NUM2))
;;
5)	echo "You've picked Factorial"
printf "Choose the number to factorialize: "
FACTORIAL=1

for (( i=\$NUM; i>1; i-- ))
do
FACTORIAL=\$(( \$FACTORIAL * \$i ))
done

;;
6) 	echo "You've picked Compare"
printf "Pick a number: "
printf "Pick another number: "

if [ \$NUM1 -eq \$NUM2 ]
then
echo "First Number is equal to Second Number"
elif [ \$NUM1 -lt \$NUM2 ]
then
echo "First Number is less then the Second Number"
else
echo "First Number is Greater than Second Number"
fi
read -p "Press Enter to continue..."
;;
7)	exit
;;
*) 	echo "YOU ENTERED AN INVALID OPTION"
read -p "Press Enter to continue..."
;;
esac

done
``````
0

LVL 11

Expert Comment

ID: 39865578
So I am just curious... The last comment by the OP is that it was working.
I Think its working =)
spent a while but it now does what i want it too do thanks for the hints
The comment after that was from Tintin who did what I suggested and rewrote the script using a function (I said subroutine, basically accomplishing the same outcome). He also contributed to the script with some clean ups as well.

I have been a member here for a very long time and it seems that answering or completing homework assignments has always been frowned upon by EE Staff and Community. This question, as well as at least one previous question from the OP appear as homework assignments. I chose to not challenge that fact for this question but give a push in the right direction so the OP can learn what it is they obviously want to learn. I do not find that learning is accomplished by someone just doing the work for me. I also do not know for 100% certainty that this was a homework assignment. I could have just as easily re-written the assignment and sent the OP on his way, but this is not truly in the spirit of the EE site.
0

LVL 48

Expert Comment

ID: 39866059
I was not aware that this was a homework assignment.   I assumed it was a self learning exercise.

If it is an assignment, then I definitely shouldn't have posted a full solution.
0

LVL 1

Author Closing Comment

ID: 39876321
i figured it out with help from the community
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group â€” Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions thaâ€¦
Utilizing an array to gracefully append to a list of EmailAddresses
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This videoâ€¦
###### Suggested Courses
Course of the Month7 days, 12 hours left to enroll