473,385 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

About while do loop,what is wrong with this code ?

Hello my teacher gave me an exercise to enter my name and 2 numbers the output must show the sum and the product of these 2 numbers and keep repeating the same procedure using a while loop till i answered "no" to a question if i want to continue,so I wrote this shell code but something is wrong ,I am using Ubuntu and vi editor:
Expand|Select|Wrap|Line Numbers
  1. #! /bin/bash
  2. while :
  3. do
  4. echo "please enter your name"
  5. read name
  6. echo"please enter your first number"
  7. read firstnumber
  8. echo"please enter your second number"
  9. read secondnumber
  10. echo "sum of the 2 numbers are"
  11. expr $firstnumber + $secondnumber
  12.  
  13. echo "product of the 2 numbers is"
  14. expr $firstnumber \*  $secondnumber
  15.  
  16. echo " do you wish to continue? yes/no"
  17. read input
  18. if [$input =="no"]; then
  19. break
  20. fi
  21. done
  22.  
# anyway to improve it and what is wrong? thank you for help.
Nov 4 '08 #1
3 2193
ashitpro
542 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. #! /bin/bash
  2. while :
  3. do
  4. echo "please enter your name"
  5. read name
  6. echo "please enter your first number"
  7. read firstnumber
  8. echo "please enter your second number"
  9. read secondnumber
  10. echo "sum of the 2 numbers are"
  11. expr $firstnumber + $secondnumber
  12.  
  13. echo "product of the 2 numbers is"
  14. expr $firstnumber \* $secondnumber
  15.  
  16. echo " do you wish to continue? yes/no"
  17. read input
  18. if [ $input == "no" ]
  19. then
  20. break
  21. fi
  22. done
  23.  
I've made some minor changes for you....
Compare this with original code...you'll come to know about problems...
Nov 5 '08 #2
Thank you ashitpro for your help but still the code is not working properly:
first it says about :"command not found" and secondly it seems like i can't get out of the loop when i press no.
Any further help ?thank you
Nov 5 '08 #3
ashitpro
542 Expert 512MB
Thank you ashitpro for your help but still the code is not working properly:
first it says about :"command not found" and secondly it seems like i can't get out of the loop when i press no.
Any further help ?thank you
Have you check out the spaces, that I've added..?
Have you compared this code with original one?
Use if then exactly as shown

if [ $input == "no" ]
then

there are spaces after and before $input and "no"
Nov 6 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: wongjoekmeu | last post by:
Hello All, I am learning C++ at the moment. Going through the book of SAM of learning C++ in 21 days I have learned about pointers that it is good custome to always initialise them and to use...
66
by: Cor | last post by:
Hi, I start a new thread about a discussion from yesterday (or for some of us this morning). It was a not so nice discussion about dynamically removing controls from a panel or what ever. It...
11
by: Sensei | last post by:
Hi again! I have still curiosity about the reason of some C constructs/keywords... The first is about static functions. What was the reason of restricting a function to be visible just in a...
53
by: jaso | last post by:
Can you give any comments on this code? I used one goto, is it bad? #include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #include <assert.h> #define NOT_NULL 1
6
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
37
by: Hilton | last post by:
Hi, for (int i = 0; i < list.Count; i++) has a hidden performance hit; i.e. list.Count gets evaluated each time, so we write something like: int listCount = list.Count; for (int i = 0; i <...
16
by: Andy B | last post by:
I have the following code inside of a WebBrowser.DocumentCompleted event: For index As Integer = 0 To Me.Browser.Document.GetElementsByTagName("ul").Item(0).GetElementsByTagName("li").Count ...
0
by: Bart van Ingen Schenau | last post by:
On Oct 30, 12:20 pm, upyzl <zj262...@163.comwrote: <snip - code> Change your loop to look like this: // scanf line before the loop removed while (scanf("%d", &nonnega_int) == 1) { // code...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.