heyyolol's blog

By heyyolol, history, 5 years ago, In English

Recently I tried using getchar to take in char input (c++), the sample works fine on my compiler , but got idleness limit exceeded on judge, I tried the code in custom invocation, and it turns out getchar was reading random characters lol. My code is something like

for(int i=0;i<n;++i) for(int j=0;j<n;++j) while(grid[i][j]!='.'&&grid[i][j]!='#') grid[i][j]=getchar()

Does anyone know how to fix this problem? Thank you!! :)

  • Vote: I like it
  • +5
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it +2 Vote: I do not like it

Why not use cin or scanf?

»
5 years ago, # |
  Vote: I like it +18 Vote: I do not like it
scanf("%d %d\n", &n, &m);
for(int i = 1; i <= n; ++i) {
    for(int j = 1; j <= m; ++j) {
        a[i][j] = getchar();
    }
    tmp = getchar();
}
  • »
    »
    5 years ago, # ^ |
      Vote: I like it +5 Vote: I do not like it

    But isn't this the same as the code I provided? Since the while grid[i][j] != expected input I will do another getchar? Thanks.