diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index 94a25f7..35eb1dd 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 91acb14..4e665a1 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,19 @@ project(LearnCPP)
set(CMAKE_CXX_STANDARD 20)
-add_executable(LearnCPP main.cpp
- main.cpp
- main.cpp
- PrintStandard.cpp)
+add_executable(LearnCPP c0/helloworld.cpp
+ c0/helloworld.cpp
+ c0/helloworld.cpp
+ c0/PrintStandard.cpp
+ c1/Variables.cpp
+ c1/Variables.cpp
+ c1/InitialisationType.cpp
+ c1/ListInitialisation.cpp
+ c1/MaybeUnused.cpp
+ c1/cout.cpp
+ c1/cin.cpp
+ c1/buffering.cpp
+ c1/LiteralsVariables.cpp
+ c1/Expression.cpp
+ c1/FirstProgram.cpp
+)
diff --git a/PrintStandard.cpp b/c0/PrintStandard.cpp
similarity index 100%
rename from PrintStandard.cpp
rename to c0/PrintStandard.cpp
diff --git a/main.cpp b/c0/helloworld.cpp
similarity index 61%
rename from main.cpp
rename to c0/helloworld.cpp
index a3b6aff..9cb7991 100644
--- a/main.cpp
+++ b/c0/helloworld.cpp
@@ -5,11 +5,10 @@
const int numStandards = 7;
// The C++26 stdCode is a placeholder since the exact code won't be determined until the standard is finalized
-const long stdCode[numStandards] = { 199711L, 201103L, 201402L, 201703L, 202002L, 202302L, 202612L};
-const char* stdName[numStandards] = { "Pre-C++11", "C++11", "C++14", "C++17", "C++20", "C++23", "C++26" };
+const long stdCode[numStandards] = {199711L, 201103L, 201402L, 201703L, 202002L, 202302L, 202612L};
+const char *stdName[numStandards] = {"Pre-C++11", "C++11", "C++14", "C++17", "C++20", "C++23", "C++26"};
-long getCPPStandard()
-{
+long getCPPStandard() {
// Visual Studio is non-conforming in support for __cplusplus (unless you set a specific compiler flag, which you probably haven't)
// In Visual Studio 2015 or newer we can use _MSVC_LANG instead
// See https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
@@ -24,36 +23,21 @@ long getCPPStandard()
#endif
}
-int main()
-{
+int main() {
long standard = getCPPStandard();
- if (standard == -1)
- {
+ if (standard == -1) {
std::cout << "Error: Unable to determine your language standard. Sorry.\n";
return 0;
}
- for (int i = 0; i < numStandards; ++i)
- {
+ for (int i = 0; i < numStandards; ++i) {
// If the reported version is one of the finalized standard codes
// then we know exactly what version the compiler is running
- if (standard == stdCode[i])
- {
+ if (standard == stdCode[i]) {
std::cout << "Your compiler is using " << stdName[i]
- << " (language standard code " << standard << "L)\n";
- break;
- }
-
- // If the reported version is between two finalized standard codes,
- // this must be a preview / experimental support for the next upcoming version.
- if (standard < stdCode[i])
- {
- std::cout << "Your compiler is using a preview/pre-release of " << stdName[i]
- << " (language standard code " << standard << "L)\n";
+ << " (language standard code " << standard << "L)\n";
break;
}
}
-
- return 0;
}
\ No newline at end of file
diff --git a/c1/Expression.cpp b/c1/Expression.cpp
new file mode 100644
index 0000000..299554d
--- /dev/null
+++ b/c1/Expression.cpp
@@ -0,0 +1,24 @@
+#include
+
+// five() is a function that returns the value 5
+int five()
+{
+ return 5;
+}
+
+int main()
+{
+ int a{ 2 }; // initialize variable a with literal value 2
+ int b{ 2 + 3 }; // initialize variable b with computed value 5
+ int c{ (2 * 3) + 4 }; // initialize variable c with computed value 10
+ int d{ b }; // initialize variable d with variable value 5
+ int e{ five() }; // initialize variable e with function return value 5
+
+ int x {};
+ x = 5; // x = 5 has side effect of assigning 5 to x, evaluates to x
+ x = 2 + 3; // has side effect of assigning 5 to x, evaluates to x
+ std::cout << x; // has side effect of printing value of x to console, evaluates to std::cout
+
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/FirstProgram.cpp b/c1/FirstProgram.cpp
new file mode 100644
index 0000000..4eea70a
--- /dev/null
+++ b/c1/FirstProgram.cpp
@@ -0,0 +1,14 @@
+#include
+
+// preferred version
+int main()
+{
+ std::cout << "Enter an integer: ";
+
+ int num{ };
+ std::cin >> num;
+
+ std::cout << "Double that number is: " << num * 2 << '\n'; // use an expression to multiply num * 2 at the point where we are going to print it
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/InitialisationType.cpp b/c1/InitialisationType.cpp
new file mode 100644
index 0000000..4ae6ad6
--- /dev/null
+++ b/c1/InitialisationType.cpp
@@ -0,0 +1,15 @@
+#include
+
+int main() {
+ int a; // default-initialization (no initializer)
+
+ // Traditional initialization forms:
+ int b = 5; // copy-initialization (initial value after equals sign)
+ int c ( 6 ); // direct-initialization (initial value in parenthesis)
+
+ // Modern initialization forms (preferred):
+ int d { 7 }; // direct-list-initialization (initial value in braces)
+ int e {}; // value-initialization (empty braces)
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/ListInitialisation.cpp b/c1/ListInitialisation.cpp
new file mode 100644
index 0000000..f3ebf96
--- /dev/null
+++ b/c1/ListInitialisation.cpp
@@ -0,0 +1,13 @@
+int main()
+{
+ // An integer can only hold non-fractional values.
+ // Initializing an int with fractional value 4.5 requires the compiler to convert 4.5 to a value an int can hold.
+ // Such a conversion is a narrowing conversion, since the fractional part of the value will be lost.
+
+ int w1 { 4.5 }; // compile error: list-init does not allow narrowing conversion
+
+ int w2 = 4.5; // compiles: w2 copy-initialized to value 4
+ int w3 (4.5); // compiles: w3 direct-initialized to value 4
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/LiteralsVariables.cpp b/c1/LiteralsVariables.cpp
new file mode 100644
index 0000000..0218ca3
--- /dev/null
+++ b/c1/LiteralsVariables.cpp
@@ -0,0 +1,10 @@
+#include
+
+int main()
+{
+ std::cout << 5 << '\n'; // print the value of a literal
+
+ int x { 5 };
+ std::cout << x << '\n'; // print the value of a variable
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/MaybeUnused.cpp b/c1/MaybeUnused.cpp
new file mode 100644
index 0000000..5a1c841
--- /dev/null
+++ b/c1/MaybeUnused.cpp
@@ -0,0 +1,30 @@
+#include
+
+int bad()
+{
+ // Here's some math/physics values that we copy-pasted from elsewhere
+ double pi { 3.14159 };
+ double gravity { 9.8 };
+ double phi { 1.61803 };
+
+ std::cout << pi << '\n'; // pi is used
+ std::cout << phi << '\n'; // phi is used
+
+ // The compiler will likely complain about gravity being defined but unused
+
+ return 0;
+}
+
+int main()
+{
+ [[maybe_unused]] double pi { 3.14159 }; // Don't complain if pi is unused
+ [[maybe_unused]] double gravity { 9.8 }; // Don't complain if gravity is unused
+ [[maybe_unused]] double phi { 1.61803 }; // Don't complain if phi is unused
+
+ std::cout << pi << '\n';
+ std::cout << phi << '\n';
+
+ // The compiler will no longer warn about gravity not being used
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/Variables.cpp b/c1/Variables.cpp
new file mode 100644
index 0000000..0234683
--- /dev/null
+++ b/c1/Variables.cpp
@@ -0,0 +1,15 @@
+#include
+
+int main()
+{
+ int width; // define a variable named width
+ width = 5; // copy assignment of value 5 into variable width
+
+ std::cout << width; // prints 5
+
+ width = 7; // change value stored in variable width to 7
+
+ std::cout << width; // prints 7
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/buffering.cpp b/c1/buffering.cpp
new file mode 100644
index 0000000..fec4017
--- /dev/null
+++ b/c1/buffering.cpp
@@ -0,0 +1,16 @@
+#include // for std::cout and std::cin
+
+int main()
+{
+ std::cout << "Enter two numbers: ";
+
+ int x{};
+ std::cin >> x; // If you enter 4 5 here, the buffer will be {4, 5}
+
+ int y{};
+ std::cin >> y; //Thus the prompt will not appear as we have an item in the buffer {5}
+
+ std::cout << "You entered " << x << " and " << y << '\n'; // The program will go directly here and print "You entered 4 and 5"
+
+ return 0;
+}
\ No newline at end of file
diff --git a/c1/cin.cpp b/c1/cin.cpp
new file mode 100644
index 0000000..a052f7b
--- /dev/null
+++ b/c1/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;
+}
\ No newline at end of file
diff --git a/c1/cout.cpp b/c1/cout.cpp
new file mode 100644
index 0000000..1f10d73
--- /dev/null
+++ b/c1/cout.cpp
@@ -0,0 +1,11 @@
+#include // for std::cout
+
+int main()
+{
+ int x{ 5 };
+ std::cout << "x is equal to: " << x << '\n'; // single quoted (by itself) (conventional)
+ std::cout << "Yep." << "\n"; // double quoted (by itself) (unconventional but okay)
+ std::cout << "And that's all, folks!\n"; // between double quotes in existing text (conventional)
+ std::cout << std::endl; // Move to a new line, flush buffer (slow, prefer using \n)
+ return 0;
+}
\ No newline at end of file