What's wrong with my program (scanf, C++)? -


what wrong program?

#define _crt_secure_no_warnings  #include <cstdio>  using namespace std;  int n;  char x[110];  int main() {      scanf("%d", &n);      while (n--) {          scanf("0.%[0-9]...", &x);          printf("the digits 0.%s\n", x);      }      return 0;  }  

this console application in vs 2013, here sample input , output:

input :

1  0.123456789...  

output

the digits 0.123456789  

but when input this output : digits 0 have tried inputitng , manually , text, in code::blocks , vs 2013, neither worked. , when inputting manually, program doesn't wait me input numbers after have entered n. should do?

the scanf() in loop fails, didn't notice. fails because can't handle newline before literal 0. so, fix format string adding space, , code testing return value scanf():

if (scanf(" 0.%[0-9]...", x) != 1)     …oops — wrong format… 

the blank in format string skips 0 or more white space characters; newlines (and tabs , spaces, etc) white space characters.

you don't want & in front of x. strictly, causes type violation: %[0-9] expects char * pass char (*)[100] quite different type. however, happens same address, away it, correct code shouldn't that.


Comments

Popular posts from this blog

android - Automated my builds -

how to proxy from https to http with lighttpd -

python - Flask migration error -