Solved

Help with calculator

Posted on 2014-02-16
11
239 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:Gregory Miller
ID: 39863799
Either delete line 82 or put a # sign in the first column to ignore the line altogether.
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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:Gregory Miller
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

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:Gregory Miller
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

789 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