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

Problem with WHILE loop?

When I execute the program, first it says "enter a number"
then i give a number and it shows output square of this is this...the program is okay till this point.

Then it says "Want to enter another number" and when i type y...the program exits...WHY ITS NOT LOOPING..

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5. char another = 'y' ;
  6. int num ;
  7. clrscr();
  8. while ( another == 'y' )
  9. {
  10. printf ( "Enter a number " ) ;
  11. scanf ( "%d", &num ) ;
  12. printf ( "square of %d is %d", num, num * num ) ;
  13. printf ( "\nWant to enter another number y/n " ) ;
  14. scanf ( " %c", &another ) ;
  15. }
  16. getch();
  17. }
  18.  
Nov 9 '13 #1
7 1291
i think the problem is with your scanf where you accept the user's response.. look carefully.. there is a space in your statement " %c"
it should be
Expand|Select|Wrap|Line Numbers
  1. scanf ( "%c", &another );
Nov 9 '13 #2
You should write again this program.
Nov 9 '13 #3
@Shirish no thats not the problem,...it still doesnt work
Nov 9 '13 #4
ya.. that's not a problem..

i tried your program on gcc compiler, works fine..

which compiler are you using?
Nov 9 '13 #5
@Shirish its Turbo c++
Nov 9 '13 #6
donbock
2,426 Expert 2GB
I would flush stdin before the scanf just to make sure there aren't any spurious characters waiting there.

The program will only loop back and repeat for lowercase 'y'. Are you sure you aren't typing uppercase 'Y'?
Nov 10 '13 #7
@donbock

the program reads an integer and then a character so i don't think there is any need to flush stdin.. the program works fine on gcc..

@sauravamatyaw

as mr. donbock said, you must be entering 'Y' instead of 'y'
or
you must be kidding. good luck
Nov 10 '13 #8

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

Similar topics

7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
1
by: pauljturner99 | last post by:
Hi, I'm trying to pass a parameter from a for loop to the nested while loop but only the first counter is passed. Here is the code: dim ctr redim ctr(5) ctr(0) = 2 ctr(1) = 4 ctr(2) = 6
0
by: adriann | last post by:
I have a strange problem whereby an attempt at a looped SQL query only passes successfully once. I get the error.. Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result...
14
by: Jan Schmidt | last post by:
Hi, in a nested do-while-loop structure I would like to "continue" the outer loop. With goto this should be no problem in while-loops. However, for do-while I cannot get it to work (without a...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
3
by: bmerlover | last post by:
I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do....
5
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
0
by: Marc Vangrieken | last post by:
Hi I have a really weird problem... I'm using some .NET assemblies in classic ASP pages (VBScript) through COM. To make it short; i have some methods and they all return an instance of...
3
by: robin1983 | last post by:
I have a problem while loop of the following program. The problem is that, it showing the output that i wished to show, but the other remaining HTML portion afer the while loop is not showing in my...
2
by: Peter9588 | last post by:
I am trying to read in every line from a given file and check (amongst other things) that the 3rd character of every line is a ';' (a semi colon). The files i make to go into my database needs to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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.