c++ - Function does not return expected value -


i not getting correct value function "checkpos" variable "thesi1. using codeblocks in windows. suggestions.

int checkpos(int thesi) {     switch(thesi)     {         case 6:             thesi = 4;             break;         case 3:             thesi = 8;             break;         case 7:             thesi = 3;             break;         case 9:             thesi = 16;             break;         case 14:             thesi = 10;             break;          return thesi;     } }    int main(){       thesi1 = checkpos(newpos);     cout << "your position " << thesi1 << endl; 

you need move return statement right after closing } of switch statement. written, function has undefined behavior since there no return statement after switch statement. should turn warning level of compiler detect such errors.

int checkpos(int thesi) {     switch(thesi)     {         case 6:             thesi = 4;             break;         case 3:             thesi = 8;             break;         case 7:             thesi = 3;             break;         case 9:             thesi = 16;             break;         case 14:             thesi = 10;             break;     }     return thesi; } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -