By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,045 Members | 1,305 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,045 IT Pros & Developers. It's quick & easy.

dowhile doesn't work out properly

P: 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
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 8,916
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

P: 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

Expert 100+
P: 542
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. }
At line 13 try to use 'getch' instead of 'scanf' to read the input from user.
Sep 28 '10 #4

Post your reply

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