Simple maths calculator loop.

Hi, I am trying to make a maths calculator that:

  1. Prompts the user for a number.
  2. Prompts the user for an operation (add, subtract, divide or multiply)
  3. Prompts the user for a number.
  4. Prompts the user for another operation (same as above) OR the option to get the result for the equation.
  5. The script should loop back if the user chooses to add another operation, and ask for another number (repeat until result)

Now, i already have the arithmetic sorted, and i know i have to add an UNTIL loop, but i'm not very good with loops as i find them hard to understand.

Here is a copy of my code so far:

-----

echo "please enter a number"
read n1

echo "please choose an operation"
echo "1. add"
echo "2. subtract"
echo "3. divide"
echo "4. multiply"
read opr

echo "please enter a number again"
read n2

if [ $opr = "1" ]
   then
      echo $((n1+n2))
elif [ $opr = "2" ]
   then  
      echo $((n1-n2))
elif [ $opr = "3" ]
   then
      echo $((n1/n2))
elif [ $opr = "4" ]
   then
       echo $((n1*n2))

fi
exit 0

-----

Can anyone give me any pointers? I really have tried to look for solutions but this is my last hope. ANTHING WILL HELP. THanks

Here are a couple of examples that should illustrate enough to get you going. You should be able to figure out how to 'plug' your code into a loop like one of these:

#!/usr/bin/env ksh

# simple loop examples
# this will work in both bash and kshell
buf="x"                         # seed to enter loop
while [[ -n $buf  ]]            # until they enter an empty buffer
do
    printf "enter a string or nothing to quit: "        # prompt
    read buf
    if [[ -n $buf ]]
    then
        printf "${buf//[aeiou]/}\n"                     # do something with valid input
    fi
done


# this illustrates a different technique, the prompt will only work in kshell
# the two command prompting mechanism from the previous example can 
# be substituted for portability. 
while true          # forever
do
    read buf?"enter a string or nothing to quit: "  # prompt and read user input
    if [[ -z $buf  ]]        # also (silently) support end of file
    then
        printf "\n"         # if they used ^D ensure prompt is on next line
        exit 0
    fi

    printf "${buf//[aeiou]/}\n"     # do something with user input and print
done                # loop for more

Something like this:

echo -n "number:"
read res
PS3="select operation (ENTER=menu again, e=end):"
select f in add sub div mul
do
        [ "$REPLY" = "e" ] && break
        echo -n "number:"
        read n

        case "$f" in
                add)  (( res+=n )) ;;
                sub)  (( res-=n )) ;;
                div)  (( res/=n )) ;;
                mul)  (( res*=n )) ;;
        esac
        echo "res:$res"
done

echo "res:$res"
1 Like

Using as much of the orginal code as possible we can create a simple "while true loop around the decision whether to quit or do another calculation. We enter the value for $n1 once at the top of the script and from that point on seed $n1 with the value of a running total in $total.

echo "please enter a number"
read n1

total=0

while true
do
     echo "please choose an operation (or press c/r to exit)"
     echo "1. add"
     echo "2. subtract"
     echo "3. divide"
     echo "4. multiply"
     read opr
     # Exit if user pressed carriage-return
     if [ "${opr}""X" = "X" ]
     then
         break
     fi

     echo "please enter a number again"
     read n2
     # Do the calculation required
     if [ $opr = "1" ]
         then
              total=$((n1+n2))
         elif [ $opr = "2" ]
         then  
              total=$((n1-n2))
         elif [ $opr = "3" ]
         then
              total=$((n1/n2))
         elif [ $opr = "4" ]
         then
              total=$((n1*n2))
     fi
     # Display running total
     echo ""
     echo "     Running total: $total"
     echo ""
     # Seed $n1 with the current value of $total
     n1=$total 
done

The modified script keeps on asking for more calculation on the running total until the user presses carriage-return.
You could add a line just before "break" to display "Final total is $total" if that is a requirement.
Try it and enjoy.

And the cheats method ... using the unix command "bc".

echo "Enter calculation" ; read calc
echo "${calc}"|bc

10+10+10
30

1024*1024
1048576

((1000*1000)/25+(20*20))
40400