Register
It is currently Fri Oct 31, 2014 12:35 pm

Simple maths calculator loop.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon Dec 19, 2011 9:29 am   

Joined: Mon Dec 19, 2011 9:14 am
Posts: 1
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" ]
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.


Top
 Profile  
 PostPosted: Mon Jan 02, 2012 4:14 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Just remove the exit and wrap your code into
Code:
while : ; do
    ... your code ...
    echo -n 'Continue? (y/n) '
    read continue
    if [[ $continue == n ]] ; then break ;fi
done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP