Simple maths calculator loop.
Page 1 of 1

Author:  piedpiper [ Mon Dec 19, 2011 9:29 am ]
Post subject:  Simple maths calculator loop.

Hi, I am trying to make a maths calculator (IN NANO) 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" ]
echo $((n1+n2))
elif [ $opr = "2" ]
echo $((n1-n2))
elif [ $opr = "3" ]
echo $((n1/n2))
elif [ $opr = "4" ]
echo $((n1*n2))

exit 0


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

Author:  choroba [ Mon Jan 02, 2012 4:14 am ]
Post subject:  Re: Simple maths calculator loop.

Just remove the exit and wrap your code into
while : ; do
    ... your code ...
    echo -n 'Continue? (y/n) '
    read continue
    if [[ $continue == n ]] ; then break ;fi

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group •