Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string GetUserNameFromSystem(int user_id) {
- return UserNameFromCorrectId(user_id);
- }
- void PrintUserName(int user_id) {
- string user_name = GetUserNameFromSystem(user_id);
- cout<<user_name;
- }
- int main() {
- PrintUserName(2);
- }
- // Связываем их сообщениями об ошибках
- bool GetUserNameFromSystem(int user_id, string& user_name) {
- if (!IsCorrectUserId(user_id))
- return false;
- else
- user_name = UserNameFromCorrectId(user_id);
- return true;
- }
- bool PrintUserName(int user_id) {
- string user_name;
- bool got_name = GetUserNameFromSystem(user_id, user_name);
- if (!got_name)
- return false;
- cout<<user_name;
- return true;
- }
- int main() {
- if (!PrintUserName(2))
- cerr<<"Наверное, неправильный id пользователя!";
- }
- // Более реальная проверка ошибок
- enum ErrorCode {
- SUCCESS,
- FILE_NOT_FOUND,
- UNKNOWN_USER,
- NOT_ENOUGH_MEMORY,
- SYSTEM_IS_SHUTTING_DOWN
- };
- ErrorCode GetUserNameFromSystem(int user_id, string& user_name) {
- ErrorCode ec = SUCCESS;
- if (!IsCorrectUserId(user_id)) return UNKNOWN_USER;
- user_name = UserNameFromCorrectId(user_id);
- return ec;
- }
- ErrorCode PrintUserName(int user_id) {
- ErrorCode ec = SUCCESS;
- string user_name;
- ec = GetUserNameFromSystem(user_id, user_name);
- if (ec != SUCCESS) return ec;
- cout<<user_name;
- return ec;
- }
- int main() {
- ErrorCode ec = PrintUserName(2);
- int main_return_code = 0;
- switch(ec) {
- case SUCCESS: // ничего не нужно делать, всё прошло хорошо
- main_return_code = 0;
- break;
- case UNKNOWN_USER; cerr<<"Наверное, неправильный id пользователя!";
- main_return_code = 1;
- break;
- case ...
- default: cerr<<"Неизвестная ошибка!";
- main_return_code = 42;
- }
- return main_return_code ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement