Is there any clear function?
#include <stdio.h>
#include <string.h>
main()
{
char s[2][3];
int c;
for( c = 0 ; c < 2 ; c++ )
{
printf("Your %d string: ", c);
scanf("%s", s[c]);
if( strlen(s[c]) > 3 )
{
c--;
printf("More than 3 characteres!\n");
continue;
}
}
return 0;
}
Question:
Whenever the user enter more than 3 characters which exceeds
the allocated space, this program will prompt the user to reenter their desire string. But the problem is when I purposely enter more than 3 characters in second attempt, the compiler prints
out as usual but with error windows pop up. I know this error was due to over-stored characters. So, is there any function that can clear that particular array and treat is as new one or maybe there is other way of doing so?
Purposely causing an error.
>>I suggest you look at the function fgets() which allows you to set a maximum number characters to be read.
Thanks for your suggestion.
>>well....im not sure but u can try using pointers...correct me if im wrong.
Thanks for your suggestion too.
New question (purposely causing an error):
#include <stdio.h>
main()
{
int n;
while(1)
{
printf("Please enter your number in the range of 0 - 30 ");
scanf("%d", &n);
if( n < 1 || n > 30 )
{
printf("Wrong!\n");
continue;
}
}
return 0;
}
From the above code it is already obvious that the program requires the user to enter an integer number. If let say the user accidentally or purposely entered a character which opposes the declared integer variable, how do I clear the entered characters? I had tried to use this isalpha() function to detect the existence of character but the problem again is too late as what stated by C_Coder (Checking the amount of characters after you have read them is to late). What should I do?:confused:
Clear function numbers 0 to 30
#include <stdio.h>
int main()
{
int n;
printf("Please enter your number in the range of 0 - 30\n ");
scanf("%i", &n);
fflush(stdin);
while(( n < 0) || (n > 30 ))
{
printf("Wrong! number must be in the range 0 - 30\n");
scanf("%i", &n);
fflush(stdin);
}
printf( "you got it!!!\n\n");
system("PAUSE");
return 0;
}
Curious and have no idea!
>>fflush(stdin);
Why can we use this function as it clears whatever entered?
>>system("PAUSE")
What does this function, function as?
>>scanf("%i", &n);
Haven't seen this '%i' before. What does it takes?
Thanks for reading guys!:D
Re: Clear function numbers 0 to 30
Code:
Coded by fred_scotland
[B]#include <stdio.h>
while(( n < 0) || (n > 30 ))
{
printf("Wrong! number must be in the range 0 - 30\n");
scanf("%i", &n);
fflush(stdin);
}
If you want the range of the number that is entered, change your conditional statement in your while loop.
Code:
while (n >= 0 && n <= 30) {
do.blah();
}