c++ - Error in inputting a 2d character array -


i entering 2d character array , have stop entering when user hit enter key . code not showing output.
input:

5  // number of columns,     // number of rows unknown have taken maximum rows as: 40 

array:

toioynnkpheleaigshareconhtomesnlewx 

expected output:

i = 7, j = 5 

here code:

int main(){     char a[100][100];     int n, i, j, p, q;     cin >> n;     if(n==0)        exit(0);      for(i = 0; < 40; i++){         for(j = 0; j < n; j++){             cin >> a[i][j];              if(a[i][j]==13)  // 13 = ascii code enter key                 goto jump;         }     }  jump:     cout<<i<<"\n"<<j<<"\n";  } 

but not printing anything.

what wrong it?

this happening because cin ignores white space , newline ('\n', ascii code 13). means condition if(a[i][j] == 13) never evaluates true.

solution: use cin.get(a[i][j]) instead of cin>>a[i][j]

this works because cin.get() method not ignore newline character ('\n').


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -