This is my code which runs perfectly well in Turbo C but not in code blocks. The only problem i'm facing is with flushall(). How can i overcome this problem?
#include<stdio.h>
#include<ctype.h>
int extractDigits(unsigned long int num, int *index, int *digits)
{
if (num)
{
digits[*index] = num % 10;
*index = *index + 1;
extractDigits(num / 10, index, digits);
}
return(*index);
}
int main()
{
int x=0,j,i,index=0,digit,digits[32];
unsigned long int n1,n2,temp,num,count=0;
printf("\n Enter lower value n1 : ");
if(!scanf("%lu",&n1))
x=1;
flushall();
printf("\n Enter higher value n2 : ");
if(!scanf("%lu",&n2))
x=1;
flushall();
printf("\n Enter the digit you wish to count : ");
if(!scanf("%d",&digit))
x=1;
flushall();
if(n1>n2||x)
{
a:printf("\n Invalid Input\n");
goto z;
}
if(n1<0||n1>150000||n2<0||n2>150000)
goto a;
if(!n1)
count++;
for(temp=n1;temp<=n2;temp++)
{
num=temp;
i=extractDigits(num, &index, digits);
for(j=0;j<i;j++)
{
if(digits[j]==digit)
count++;
}
index=0;
}
printf("\n Count : %lu \n",count);
z:return 0;
If I don't use flushall() then I'm unable to run the test conditions for invalid input. What is the solution to this problem?
http://www.cplusplus.com/reference/cstdio/
I didn't find flushall(), may be the compile is diffent.
There is fflush
. Maybe you can use it.
It is not a matter of IDE, but of compiler and most importantly of C standard libraries.
C99 and Posix conforming libraries have fflush(3) which is documented as:
int fflush(FILE* stream);
If the stream argument is NULL, fflush() flushes all open output streams.
So just call
fflush(NULL);
when you want to flush all output streams (which is what I guess your non-standard fflushall
is doing).
Notice that a common habit is to end , not start, with \\n
the printf format string, since stdout
is often line buffered.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.