LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 07-16-2004, 08:12 PM   #1
shams
Member
 
Registered: Jan 2004
Posts: 396

Rep: Reputation: 30
Unhappy 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 <stdio.h>
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);

}
 
Old 07-16-2004, 08:33 PM   #2
kwigibo
Member
 
Registered: Apr 2002
Location: Australia
Distribution: Mandrake 8.2
Posts: 53

Rep: Reputation: 15
You are missing a right brace. I'm suprised this compiled at all.

include <stdio.h>
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*/
 
Old 07-17-2004, 05:31 AM   #3
arvind_sv
Member
 
Registered: Oct 2002
Location: Bangalore
Distribution: Gentoo Linux
Posts: 96

Rep: Reputation: 15
Hi shams,

Try putting a space before the %c. Make the scanf line:
Code:
scanf(" %c",&another);
Arvind
 
Old 07-17-2004, 01:22 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
scanf() leaves the \n (from the ENTER key) on the input butter. After each scanf() try adding a getchar(); to get rid of it.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
coding problem on php auto send mail after a period akamaru607 Programming 12 09-01-2005 07:48 AM
Loop module problem.. kingroach Linux - Software 2 02-21-2005 12:55 PM
problem coding conditional operators shams Programming 1 07-13-2004 07:39 PM
bash for loop problem deadlock Programming 5 09-04-2003 04:32 AM
loop problem andym Linux - Newbie 0 06-05-2003 05:12 AM


All times are GMT -5. The time now is 08:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration