KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I wrote a function in C that converts a string to an integer and returns the integer. When I call the function I also want it to let me know if the string is not a valid number. In the past I returned -1 when this error occurred, because I didn't need to convert strings to negative numbers. But now I want it to convert strings to negative numbers, so what is the best way to report the error? In case I wasn't clear about this: I don't want this function to report the error to the user, I want it to report the error to the code that called the function. ("Report" might be the wrong word to use...) Here's the code: s32 intval(const char *string) { bool negative = false; u32 current_char = 0; if (string[0] == '-') { negative = true; current_char = 1; } s32 num = 0; while (string[current_char]) { if (string[current_char] < '0' || string[current_char] > '9') { // Return an error here.. but how? } num *= 10; num += string[current_char] - '0'; current_char++; } if (negative) { num = -num; } return num; }
Tags (comma-separated)
Save Edits
Cancel