exception occured in C++ function -


when given number "n" , radix "d", function followed used calculate reversed number under radix d, , convert decimal , return.

the problem that, method 1 can work succefully while method 2 comes exception. there tell me what's problem method 2 ? much.

int getrevn(int n, int d)  { //-------method 1-------------------------//      int revn = 0;     while (n)     {         revn = revn * d + n % d;         n /= d;     }     return revn;  //------method 2-------------------------//      string s;     while (n)     {         s = char(n % d + '0') + s;         n /= d;     }     int rev = 0;     (unsigned int = s.size() - 1; >= 0; i--)         rev = rev * d + s[i] - '0';     return rev; } 

in last loop, you're declaring i unsigned. i >= 0, , loop won't terminate.

instead, i wrap around large number, causes crash. change i signed.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -