Simple calculator with menu input - Need Help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple calculator with menu input - Need Help
# 1  
Old 01-17-2013
Wrench Simple calculator with menu input - Need Help

I am trying to make a calculator. The user Enters number 1, chooses and operation, enters number 2, then chooses another operation or for the answer to be displayed.

eg. 1 + 1 = or 1 + 1 + 2 + 1 =

Both of these should be possible.

Code:
#!/bin/bash

read -p "what's the first number? " n1
PS3="what's the operation? "
select ans in add subtract multiply divide equals; do
case $ans in 
add) op='+' ; break ;;
subtract) op='-' ; break ;;
multiply) op='*' ; break ;;
divide) op='/' ; break ;;
*) echo "invalid response" ;;
esac
done
read -p "what's the second number? " n2
ans=$(echo "$n1 $op $n2" | bc -l)
printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"

exit 0

This is what I have written so far, but i cannot work out how to make it possible to let the user choose 'equals' or to loop back round to enter another operation. Any ideas what I can do to my code here? I have been stuck on this all day.

I dont want the user to enter the equation themselves, i want them to be choosing from a list.
# 2  
Old 01-17-2013
Not sure what operation 'equals' would do, but try:
Code:
#!/bin/bash
n1=ans
while [ -n "$n1" ]
do
   read -p "what's the first number? (return to exit)" n1
   [[ -n "$n1" ]] && {
      PS3="what's the operation? "
      select ans in add subtract multiply divide exp; do
         case $ans in
            add) op='+' ; break ;;
            subtract) op='-' ; break ;;
            multiply) op='*' ; break ;;
            divide) op='/' ; break ;;
            exp) op='^' ; break ;;
            *) echo "invalid response" ;;
         esac
      done
      read -p "what's the second number? " n2
      ans=$(echo "$n1 $op $n2" | bc -l)
      printf "%s %s %s = %s\n\n" "$n1" "$op" "$n2" "$ans"
   }
done
 
exit 0


Last edited by rdrtx1; 01-17-2013 at 03:24 PM..
# 3  
Old 01-17-2013
Thank you for the responce, however, i do not have a problem with looping the code. My big issue, is getting the code to work like an actual calculator.

Basically I would like the script to
1 - Prompt the user for a number.
2 - Prompt the user for the operation they would like to perform, either add, subtract, multiply or divide.
3 - Prompt the user for a number.
4 - Prompt the user for the operation they would like to perform, either add, subtract, multiply, divide OR EQUALS.
5 - Display the result of the selected operation. The script should then exit or continue from step 3 if the user has not selected equals.

It is the (continue from step 3) bit that I just cannot get to work. Any ideas. Any help is very welcome, i have been racking my brains with this for too long now Smilie
# 4  
Old 01-18-2013
What would the 'equals' do? It doesn't do the operations immediately, just saves them for later?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple Scientific calculator for ADE, the UNIX environment for the AMIGA A1200(HD).

ADE is a UNIX environment for the ancient AMIGA A1200. By default this does NOT have the 'bc' command line calculator. Although I did a DEMO code to create a C source and compile it under python 1.4.0 and ADE using ksh88 and the gcc of the day, I decided to create this baby that requires no Python... (2 Replies)
Discussion started by: wisecracker
2 Replies

2. Shell Programming and Scripting

Help with 'select' for menu input

A lot of my scripting makes use of the 'select' command to create menu driven input. A typical example of how I use it is as: somevar='' PS3='Select one: ' while ]; do select somevar in $(sqlplus -s $dbuser/$dbpw@mydb <<EOF set echo off feedback off verify off... (7 Replies)
Discussion started by: edstevens
7 Replies

3. Homework & Coursework Questions

Simple Calculator

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known/data: Script a simple calculator. In the command line enter the script file /home/etc/mycalc or /home/etc/mycalc 1 +... (6 Replies)
Discussion started by: herb bertz
6 Replies

4. UNIX for Dummies Questions & Answers

Simple bash script menu

Dear Sir, May I know how do I go about adding the following feature into the script below: When user enter values other than 1,2,3,4, a) Message “Wrong entry !!! Pls select 1,2,3 or 4” is displayed b) The screen is cleared again and the menu is displayed. #!/bin/bash clear var=1... (2 Replies)
Discussion started by: fusetrips
2 Replies

5. Shell Programming and Scripting

Two columns output in simple case menu?

EDIT : System used : Any Linux distribution. Hello everyone, I m having quite a headache trying to figure out why I m having a 2 columns output in the following code : #!/bin/ksh menu_rotation() { #Variables CHOIX1="Rotation 1" CHOIX2="Rotation 2" CHOIX3="Rotation 3" ... (11 Replies)
Discussion started by: Sekullos
11 Replies

6. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: johnthebaptist
4 Replies

7. Shell Programming and Scripting

Perl simple text menu with options

Hopefully I'm in the right place. Im new to the forums and linux! I'm looking to add a menu to my perl hangman game i have created. The menu will use user input for the desired option and then perform the operation indicated. I would like something along the lines of: Welcome to Hangman... (1 Reply)
Discussion started by: jahburmski
1 Replies

8. UNIX for Dummies Questions & Answers

Input A Menu Selection In A Variable

hey all, me again....having a problem with my code, where I essentially am trying to show a menu, have the user select an option (from 1 to 5), then display which selection they picked... #!/bin/bash # A LIST OF THE ERROR MESSAGES AND THE PROPER SYNTAX: error_0="Correct amount of... (1 Reply)
Discussion started by: SoVi3t
1 Replies

9. Shell Programming and Scripting

help with scripting a simple menu

Hi there. I'm trying to teach myself UNIX but the book I bought is a bit confusing. I'm trying out this exercise and I think I'm on the right track, but I'd appreciate any suggestions on how to improve what I have so far. Also, I'm not clear on how to use the read command to utilize the user's... (3 Replies)
Discussion started by: Jsmith
3 Replies

10. Shell Programming and Scripting

Simple Menu and coding

I am very new to Unix and don't know much about it. I've been trying to create a menu and what I don't understand is how to execute a command once a user makes a selection. I have the menu set up. In fact, the following is the code that I have thus far: #! /bin/csh # This is the UNIX menu... (0 Replies)
Discussion started by: sinjin
0 Replies
Login or Register to Ask a Question