473,388 Members | 1,286 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,388 software developers and data experts.

dowhile doesn't work out properly

9
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. char another,
  7. int num;
  8. printf("\nenter a number to get its square\n");
  9. scanf("%d",&num);
  10. printf("\nsquare of %d is %d",num,num*num);
  11. printf("\ndo you want to enter another no y/n");
  12. scanf("%c",&another);
  13. }while(another=='y');
  14. getch();
  15. }
" when i try to give input 'y' for another char variable instead of following and continuing do loop my programme closes" plz help out
Sep 27 '10 #1
3 1326
Banfa
9,065 Expert Mod 8TB
I see no do in you program.

For maximum portability in C main returns int not void. In C++ it is mandated that main returns int and not doing so is undefined behaviour.

You never read any of the newlines \n out of the input buffer so when you come to read a character at line 12 the newline character is still in the input buffer and the system happily returns that newline in the character read on line 12. Since newline is not 'y' the loop ends.

The console is basically a line mode input device, that is your program never sees anything until the user presses enter and puts a newline character into the buffer. Reading it as a line mode input device will make your life easier, read the input with fgets into a character array (a good size one) then parse the data you have got with sscanf or strtoul or atoi.
Sep 27 '10 #2
navda
9
thanks a lot for repling me back but i wish if u could send me the code for my better understanding and pointing out the lines where i had made mistakes i have tried this code from let us c eight edition but it was not working in my computer so i need more clear code to get throught this problem
Sep 28 '10 #3
ashitpro
542 Expert 512MB
Have you read what 'Banfa' has said? Have you tried to implement what he said?

You are missing 'do' in your code
You are missing ';' after line 6
Here is the modified code.
It still won't work as you are expecting, cause scanf will read '\n' that you entered after number to be squared. (Here '\n' means 'Enter' key)

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. char another;
  7. int num;
  8. do{
  9. printf("\nenter a number to get its square\n");
  10. scanf("%d",&num);
  11. printf("\nsquare of %d is %d",num,num*num);
  12. printf("\ndo you want to enter another no y/n");
  13. scanf("%c",&another);
  14. }while(another=='y');
  15. getch();
  16. }
  17.  
At line 13 try to use 'getch' instead of 'scanf' to read the input from user.
Sep 28 '10 #4

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

Similar topics

1
by: Mike | last post by:
Hi, I'm new to ASP and can't get this form to work. It's a simple page that draws a calendar for the current month and then you can select a new month and it draws the new calendar. It works...
0
by: Mark Barinstein | last post by:
Hello. W2K, db2 v7, FP10a. Before now our replication worked properly. But some days ago capture suddenly stopped loggin and filling cd tables without any messages. The last message was like...
6
by: dddddddd2444444 | last post by:
Hi,please help... It works fine when I define a 2-D array like char code. But it won't work when I try to define the array dynamically using a function. It just crashes. Does anyone know why?...
1
by: TT (Tom Tempelaere) | last post by:
Hi there, I made a custom drawn ComboBox. The DrawItem event handler is (explanation follows) <code> private void OnDrawItem ( object sender, System.Windows.Forms.DrawItemEventArgs e ) {
1
by: Bill Nguyen | last post by:
Several comboboxes in my app are not working properly with VS2005. I ahve both .NET 1.1 and 2.0 installed in my development PC. Below is an example of the code. It never worked the first time...
2
by: whezker | last post by:
Hi, I did some controls that needs a background transparent, cause roundoed corners or a simulated shadow and some effects in the paint event. I set the background property to color.transparent...
0
by: Phil | last post by:
Has anyone else had a problem using the AXWindowsMediaPlayer control and VB2008? It all seems to work fine for me, but I have sent my application out to a few other people to test, and I have had...
13
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this:...
1
by: Guramrit Singh | last post by:
Hi experts, I'm having this problem since I'm using content type as application/xhtml+xml. I'd added a UpdatePanel and some child controls inside this, on click of a button I wanted to show this...
0
by: sourpo | last post by:
wonder if anyone can help... I have access db with a whole lot of vb code... If i use access 2000 it works perfectly but when using 2003 it doesn't work properly. The db copies outlook mailboxes......
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.