-
Notifications
You must be signed in to change notification settings - Fork 3
Бобрик Вячеслав Сергеевич #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include <cstdlib> | ||
| #include <iostream> | ||
| #include <ctime> | ||
|
|
||
| #define PRINT(text) std::cout << text << std::endl | ||
|
|
||
| int read_user_number() { | ||
| int result; | ||
|
|
||
| std::cout << "Guess a number from 0 to 9: "; | ||
| std::cin >> result; | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| int main() { | ||
| srand(unsigned(time(0))); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Приведение выполнится неявно автоматически. От приведения в стиле C особо ничего не изменится, разве что преобразование теперь перед глазами) |
||
|
|
||
| auto number = rand() % 10; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если переменную можно сделать константой — так лучше и поступить |
||
| auto user_number = read_user_number(); | ||
|
|
||
| while (user_number != number) | ||
| { | ||
| PRINT("Wrong!"); | ||
| user_number = read_user_number(); | ||
| } | ||
|
|
||
| PRINT("Correct!"); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Запись в неинициализированную переменную, строго говоря, не UB. Но вообще в реальных кодовых базах чаще стараются все же инициализировать все переменные:
int result {};