-
Repeat condition problem
Hi All!
I am getting a problem in using scanf for yes/no condition
Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
long fibonacci(int count);
void main()
{
int count,n;
char resp='Y';
clrscr();
do
{
printf("\nHow many fibonacci: ");
scanf("%d",&n);
for(count=1;count<=n;++count)
printf("\ni= %2d\tF= %ld", count, fibonacci(count));
printf("\nDo you want to check again? (Y/N):");
scanf("%c",&resp);
}while(toupper(resp)!='N');
printf("\nBye, have a nice day!");
getch();
}
long fibonacci(int count)
{
static long int f1=1, f2=1;
long int f;
f=(count<3)?1:f1+f2;
f2=f1;
f1=f;
return(f);
}
Here i am facing the problem that this programme is not asking to user for the condition "Do You want to contineu Y/N: ".
Please help..
Anil
-
why do you have two scanf()
u have to get a single value wheather Y or N
use just one scanf to read one value from the user either y or n check that for your further processing
Code:
scanf("%c",&resp);
scanf("%c",&resp);}
please use some tabing space in your prog is looks neat
s.s.harish
-
try a getchar() before the scanf() statement.
-
looking at your code again i found few bugs
Code:
.
.
void clr_buf();
.
.
for(count=1;count<=n;++count)
printf("\ni= %2d\tF= %ld", count, fibonacci(count));
printf("\nDo you want to check again? (Y/N):");
clr_buf(); // clears the input buffer
scanf("%c",&resp);}while(toupper(resp)!='N');
printf("\nBye, have a nice day!");
getch();
}
void clr_buf()
{
int ch;
while((ch=getchar()) != '\n' || ch != EOF);
}
the problem here was the scacnf fucniotn will always leaves the reuturn key in the buffer so when u use scanf twice the first scanf picks up the return('\n' or enter key) and clears the buffer and agian when u use the scanf it gets the value from the user when entered by the user
dont use the conio.h its not standard library and dont use the clrscr() RAD FAQ
main() function should always return an int
s.s.harish