Getting error when using scanf

Since today I started getting errors whenever I try to use scanf.

#include <iostream>

int main() {
  int n;
  scanf("%d", &n);
}

Console:

./main.cpp:5:3: error: ignoring return value of function declared with 'warn_unused_result' attribute [-Werror,-Wunused-result]
  scanf("%d", &n);
  ^~~~~ ~~~~~~~~
1 error generated.
make: *** [Makefile:10: main] Error 1

I know that I can fix it by saving scanf to a variable, but the question is why is this happening? It seems as though replit has changed the -Werror flag upon running the code? Why? It was never like this and I never received this error.

It may had some changes, AFAIK Replit usually update their settings to encourage better coding practices. For example, ensuring that return values are checked can prevent some bugs and security issues in more complex scenarios.

I think we as devs should have the flexibility to choose our own settings, especially on the paid plans

open a new tab to edit the hidden Makefile file so you can configure the warning flags.
I’d recommend creating a .ccls file containing e.g.

clang
-Wmost

so the warnings in the editor are consistent with the Run button.

1 Like

Do I need to do this for every new C++ file?

you’d have to do it for each repl. You could always Fork a repl to carry its Makefile to the new repl.