From e61e04619d0c9dd4d079532eff3483e234fc8acd Mon Sep 17 00:00:00 2001 From: hybrid Date: Mon, 24 Aug 2026 00:48:14 +0300 Subject: add: lesson{5,6}, CMakeLists.txt --- CMakeLists.txt | 3 ++- src/chapter1/lesson5/cin.cpp | 12 ++++++++++++ src/chapter1/lesson5/multiple_cin.cpp | 14 ++++++++++++++ src/chapter1/lesson5/q1.cpp | 26 ++++++++++++++++++++++++++ src/chapter1/lesson5/q2.cpp | 17 +++++++++++++++++ src/chapter1/lesson6/uninit_var.cpp | 12 ++++++++++++ 6 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/chapter1/lesson5/cin.cpp create mode 100644 src/chapter1/lesson5/multiple_cin.cpp create mode 100644 src/chapter1/lesson5/q1.cpp create mode 100644 src/chapter1/lesson5/q2.cpp create mode 100644 src/chapter1/lesson6/uninit_var.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index eea0bfc..d37d042 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,9 @@ foreach(SOURCE_FILE ${SOURCES}) # form target names (substitute '/' to '_', remove '.cpp') string(REPLACE "/" "_" TARGET_NAME ${REL_PATH}) string(REPLACE ".cpp" "" TARGET_NAME ${TARGET_NAME}) - + add_executable(${TARGET_NAME} ${SOURCE_FILE}) + target_compile_options(${TARGET_NAME} PRIVATE -Wall -Weffc++ -Wextra -Wpedantic ) diff --git a/src/chapter1/lesson5/cin.cpp b/src/chapter1/lesson5/cin.cpp new file mode 100644 index 0000000..0fc25f8 --- /dev/null +++ b/src/chapter1/lesson5/cin.cpp @@ -0,0 +1,12 @@ +#include // for std::cout and std::cin + +int main() +{ + std::cout << "Enter a number: "; // ask user for a number + + int x{}; // define variable x to hold user input (and value-initialize it) + std::cin >> x; // get number from keyboard and store it in variable x + + std::cout << "You entered " << x << '\n'; + return 0; +} diff --git a/src/chapter1/lesson5/multiple_cin.cpp b/src/chapter1/lesson5/multiple_cin.cpp new file mode 100644 index 0000000..991e397 --- /dev/null +++ b/src/chapter1/lesson5/multiple_cin.cpp @@ -0,0 +1,14 @@ +#include // for std::cout and std::cin + +int main() +{ + std::cout << "Enter two numbers separated by a space: "; + + int x{}; // define variable x to hold user input (and value-initialize it) + int y{}; // define variable y to hold user input (and value-initialize it) + std::cin >> x >> y; // get two numbers and store in variable x and y respectively + + std::cout << "You entered " << x << " and " << y << '\n'; + + return 0; +} diff --git a/src/chapter1/lesson5/q1.cpp b/src/chapter1/lesson5/q1.cpp new file mode 100644 index 0000000..2320bee --- /dev/null +++ b/src/chapter1/lesson5/q1.cpp @@ -0,0 +1,26 @@ +#include // for std::cout and std::cin + +int main() +{ + std::cout << "Enter a number: "; // ask user for a number + int x{}; // define variable x to hold user input + std::cin >> x; // get number from keyboard and store it in variable x + std::cout << "You entered " << x << '\n'; + + return 0; +} + +/* + * a) 'h' is not integer type, so x will be 0. + * b) all input before '.' in '3.2' is assigned to x; all the rest remains + * for future extractions. + * c) sign character '-' is allowed, so '-3' is assigned to x + * d) same as variand a) + * e) highest acceptable value by integer type will be used if + * '99999999999999999999' is input + * f) 123 is assigned to x and 'abc' remains for future extractions + * g) first character 'a' is not a valid integer type, so extraction failed + * and x is 0 + * h) spaces droped; '+' is valid character for integer type; 5 is assigned to x + * i) 5 is assigned to x; b6 remains for future extractions +*/ diff --git a/src/chapter1/lesson5/q2.cpp b/src/chapter1/lesson5/q2.cpp new file mode 100644 index 0000000..236a1be --- /dev/null +++ b/src/chapter1/lesson5/q2.cpp @@ -0,0 +1,17 @@ +#include + +/* This program will ask user for three numbers, store each in separate + * variables x, y, and z respectively and print the message with their values. + */ +int main() { + std::cout << "Enter three numbers: "; + + int x{}; + int y{}; + int z{}; + std::cin >> x >> y >> z; + + std::cout << "You entered " << x << ", " << y << ", and " << z << ".\n"; + + return 0; +} diff --git a/src/chapter1/lesson6/uninit_var.cpp b/src/chapter1/lesson6/uninit_var.cpp new file mode 100644 index 0000000..603b8e0 --- /dev/null +++ b/src/chapter1/lesson6/uninit_var.cpp @@ -0,0 +1,12 @@ +#include + +int main() +{ + // define an integer variable named x + int x; // this variable is uninitialized because we haven't given it a value + + // print the value of x to the screen + std::cout << x << '\n'; // who knows what we'll get, because x is uninitialized + + return 0; +} -- cgit v1.3.1