scanf does not stop at a loop

Asked

Viewed 135 times

0

int main(){
char texto[1000];
do{ 
    scanf("%[^\n]", texto);
    if(!fakeEquals(texto)){
        if(palindromo(texto)){
            printf("SIM\n");
        }else{
            printf("NAO\n");
        }
    }
}while(!fakeEquals(texto));

return 0;

It is a program that returns if a String is palindrome or not, when it was used scan("%s", text); it was working, but since I need to read spaces too, I changed to scanf("%[ n], text); only when I type any word it is returning YES or NOT infinitely...

1 answer

2


Just put a space in the scanf(" %[^\n]", texto); because you need to clean the buffer before reading again, otherwise it reads once and then never reads the other word again

  • 1

    Thank you! That’s right :)

  • Great, if you solved the problem vote for the answer, it’s important to vote when you solve our problem vote

  • @Victorharry If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.