LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   problem coding odd loop (http://www.linuxquestions.org/questions/programming-9/problem-coding-odd-loop-206068/)

shams 07-16-2004 08:12 PM

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);

}

kwigibo 07-16-2004 08:33 PM

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*/

arvind_sv 07-17-2004 05:31 AM

Hi shams,

Try putting a space before the %c. Make the scanf line:
Code:

scanf(" %c",&another);
Arvind

itsme86 07-17-2004 01:22 PM

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


All times are GMT -5. The time now is 12:39 PM.