Register a domain and help support LQ
 Home Forums HCL Reviews Tutorials Articles Register Search Today's Posts Mark Forums Read
 LinuxQuestions.org problem coding odd loop
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

Notices

 07-16-2004, 09:12 PM #1 shams Member   Registered: Jan 2004 Posts: 400 Rep: problem coding odd loop hi, it is suppose to be an odd loop but it is terminating after the first output,plz tell me where is the mistake: include main() { char another='y'; int num; while(another=='y') { printf("Enter a number\n"); scanf("%d",&num); printf("square of %d is %d\n",num,num*num); printf("\nWant to enter another number y/n\n"); scanf("%c",&another); }
 07-16-2004, 09:33 PM #2 kwigibo Member   Registered: Apr 2002 Location: Australia Distribution: Mandrake 8.2 Posts: 53 Rep: You are missing a right brace. I'm suprised this compiled at all. include main() { char another='y'; int num; while(another=='y') { printf("Enter a number\n"); scanf("%d",&num); printf("square of %d is %d\n",num,num*num); printf("\nWant to enter another number y/n\n"); scanf("%c",&another); } }/* missing brace here*/
 07-17-2004, 06:31 AM #3 arvind_sv Member   Registered: Oct 2002 Location: Bangalore Distribution: Gentoo Linux Posts: 96 Rep: Hi shams, Try putting a space before the %c. Make the scanf line: Code: `scanf(" %c",&another);` Arvind
 07-17-2004, 02:22 PM #4 itsme86 Senior Member   Registered: Jan 2004 Location: Oregon, USA Distribution: Slackware Posts: 1,246 Rep: scanf() leaves the \n (from the ENTER key) on the input butter. After each scanf() try adding a getchar(); to get rid of it.