473,513 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

13 New Member
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 2196
ashitpro
542 Recognized Expert Contributor
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
redcodeheaven
13 New Member
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 Recognized Expert Contributor
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
1509
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
4059
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
1592
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
3121
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
500
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
4024
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
2149
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
1746
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
1163
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
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7388
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
7547
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...
0
5693
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5098
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4751
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1607
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.