Solved

Help with calculator

Posted on 2014-02-16
11
235 Views
Last Modified: 2014-02-21
Hi Guys please help,

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 "1) Addition"	#add two Numbers
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 "You've picked Additions"
	echo "Choose a number"
	read NUM1
	echo "Choose a another number to add to the first number"
	read NUM2
	VALUE=$(($NUM2+$NUM1))
	;;
"2")	echo "You've picked Subtract"
	echo "Choose a number"
	read NUM1
	echo "Choose a another number to Subtract to the first number"
	read NUM2
	VALUE=$(($NUM1-$NUM2))
	;;
"3")	echo "You've picked Multiply"
	echo "Choose a number"
	read NUM1
	echo "Choose a another to multiply"
	read NUM2
	VALUE=$(($NUM1*$NUM2))
	;;
"4")	echo "You've picked Divide"
	echo "Choose a number"
	read NUM1
	echo "Choose a another to divide the first number"
	read NUM2
	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
	read NUM1			#ask another number
	echo "Pick another number"
	read NUM2
	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"
echo "PLEASE TRY AGAIN"
	;;
esac				#show answer

echo "The answere is: $VALUE"
read -p "Press Enter to continue..."  #pause for answere

clear		
		#space
echo " \"Welcome\" to the most awsome calc evar"
echo "1) Addition"	#add two Numbers
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

Open in new window

0
Comment
Question by:stellarsystems
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 21

Expert Comment

by:Mazdajai
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"

Open in new window

0
 
LVL 1

Author Comment

by:stellarsystems
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

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

Author Comment

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

Expert Comment

by:Technodweeb
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:stellarsystems
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

by:stellarsystems
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 "1) Addition"	#add two Numbers
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 "You've picked Additions"
	echo "Choose a number"
	read NUM1
	echo "Choose a another number to add to the first number"
	read NUM2
	VALUE1=$(($NUM2+$NUM1))
echo "The answere is: $VALUE1"
read -p "Press Enter to continue..."  #pause for answere
	;;
"2")	echo "You've picked Subtract"
	echo "Choose a number"
	read NUM1
	echo "Choose a another number to Subtract to the first number"
	read NUM2
	VALUE2=$(($NUM1-$NUM2))
echo "The answere is: $VALUE2"
read -p "Press Enter to continue..."  #pause for answere
	;;
"3")	echo "You've picked Multiply"
	echo "Choose a number"
	read NUM1
	echo "Choose a another to multiply"
	read NUM2
	VALUE3=$(($NUM1*$NUM2))
echo "The answere is: $VALUE3"
read -p "Press Enter to continue..."  #pause for answere
	;;
"4")	echo "You've picked Divide"
	echo "Choose a number"
	read NUM1
	echo "Choose a another to divide the first number"
	read NUM2
	VALUE4=$(($NUM1/$NUM2))
echo "The answere is: $VALUE4"
read -p "Press Enter to continue..."  #pause for answere
	;;
"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"
read -p "Press Enter to continue..."  #pause for answere
	;;
"6") 	echo "You've picked Compare"
	echo "Pick a number"			#ask number
	read NUM1			#ask another number
	echo "Pick another number"
	read NUM2
	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
read -p "Press Enter to continue..."  #pause for answere
	;;
*)

echo "YOU ENTERED INVAILD OPTIONS"
echo "PLEASE TRY AGAIN"
	;;
esac				#end case

clear		
		#space
echo " \"Welcome\" to the most awsome calc evar"
echo "1) Addition"	#add two Numbers
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

Open in new window

0
 
LVL 48

Accepted Solution

by:
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
function answer
{
  echo "The answer is: $1"
  read -p "Press Enter to continue..."  
}

while true
do
  clear

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

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

EOF

  printf "Enter menu option: "
  read CAL			

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

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

	answer $FACTORIAL
	;;
  6) 	echo "You've picked Compare"
	printf "Pick a number: "	
	read NUM1			
	printf "Pick another number: "
	read NUM2

	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"
	echo "PLEASE TRY AGAIN"
	read -p "Press Enter to continue..."  
	;;
  esac				

done

Open in new window

0
 
LVL 11

Expert Comment

by:Technodweeb
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

by:Tintin
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

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now