{"id":19758126,"url":"https://github.com/marineks/cpp_modules","last_synced_at":"2025-10-04T06:08:50.100Z","repository":{"id":45738515,"uuid":"510760041","full_name":"marineks/CPP_modules","owner":"marineks","description":"Prompt: Learn the fundamentals of C++","archived":false,"fork":false,"pushed_at":"2023-03-26T15:23:44.000Z","size":5958,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-10T22:47:46.116Z","etag":null,"topics":["cpp","training-module"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/marineks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-05T13:59:55.000Z","updated_at":"2022-10-12T22:10:34.000Z","dependencies_parsed_at":"2023-01-19T23:47:32.917Z","dependency_job_id":null,"html_url":"https://github.com/marineks/CPP_modules","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marineks%2FCPP_modules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marineks%2FCPP_modules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marineks%2FCPP_modules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marineks%2FCPP_modules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marineks","download_url":"https://codeload.github.com/marineks/CPP_modules/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241089508,"owners_count":19907765,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","training-module"],"created_at":"2024-11-12T03:23:02.390Z","updated_at":"2025-10-04T06:08:45.068Z","avatar_url":"https://github.com/marineks.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CPP_modules\n\n## Module 00\n\nModule 00 is all about getting acquainted with CPP basics: Namespaces, classes, member functions, stdio\nstreams, initialization lists, static, const, and so on.\n\n\u003cdetails\u003e\u003csummary\u003e:loudspeaker: Ex00 - Megaphone \t:mega:  \u003c/summary\u003e\n\u003cp\u003e\n\nA really straightforward exercice to practice with the iostream library and std::cout.\n\n\u003cimg src=\"assets/megaphone_example.png\" alt=\"Example\" width=\"450\"/\u003e\n\u003c/p\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e:bookmark_tabs: Ex01 - Phonebook :phone:  \u003c/summary\u003e\n\u003cbr\u003e\u003c/br\u003e\n\u003cp\u003e\n\n\u003cimg src=\"assets/phonebook_example.png\" alt=\"Example\" width=\"450\"/\u003e\n \nSome issues I got and ressources which helped me solve them:\n\n### Not being able to retrieve multiples words from std::cin\n\nExample:\n```cpp\nstd::string input;\nstd::cin \u003e\u003e input;\n\n// If the input typed by the user is \"Hello World\", input will be equal to \"Hello\" and not \"Hello World\".\n```\nThis issue was fixed with std::getline, which reads all the characters from an input stream and puts them onto a string.\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\n \nint main() {\n    // Declare a firstname (String)\n    std::string firstname;\n \n    std::cout \u003c\u003c \"What is your firstname ?\" \u003c\u003c std::endl;\n \n    // Get the input from std::cin and store into firstname\n    std::getline(std::cin, firstname);\n \n    return 0;\n}\n```\n\n### Particularities of std::getline and std::cin\n\n***Combining std::cin and std::getline***\n\nIf you try and run the following code, you will see that the *firstname* getline will be skipped and the *\"lastname\"* prompt will  be displayed. This is because of std::cin's usage just above. \n\n```cpp\nint main(void)\n{\n   char *input;\n   std::cout \u003c\u003c \"Please enter a one-word command\" \u003c\u003c std::endl;\n   std::cin \u003e\u003e input; // Here is where std::cin precedes the usage of std::getline()\n   \n   if (input == \"ADD\")\n   {\n     std::string firstname;\n     std::cout \u003c\u003c \"Enter your firstname\" \u003c\u003c std::endl;\n     std::getline(std::cin, firstname);\n     std::cout \u003c\u003c \"First name registered: \" \u003c\u003c firstname \u003c\u003c std::endl;\n     \n     std::string lastname;\n     std::cout \u003c\u003c \"Enter your lastname\" \u003c\u003c std::endl;\n     std::getline(std::cin, lastname);\n     std::cout \u003c\u003c \"Last name registered: \" \u003c\u003c lastname \u003c\u003c std::endl;\n   }\n   return (0);\n}\n```\nExplanation:\n\n\u003e \"std::getline() does not ignore any leading white-space / newline characters. Because of this, if you call std::cin \u003e\u003e var; just before getline(), there will be a newline still remaining in the input stream, after reading the input variable. So, if you call getline() immediately after cin, you will get a newline instead, since it is the first character in the input stream! To avoid this, simply add a dummy std::getline() to consume this new-line character!\"\n\n**Ressource**\n[How to use getline](https://www.journaldev.com/39743/getline-in-c-plus-plus#:~:text=Basic%20Syntax%20of%20std%3A%3Agetline()%20in%20C%2B%2B\u0026text=We%20need%20to%20import%20the,string%26%20output%2C%20char%20delim)\n\n***Catching errors with std::cin.fail()***\n\nIn this exercise, you will have to ask the user's input several times, in order to add a contact to the phonebook. I used std::getline() for strings and std::cin for the phone number, because we were dealing with (long) **ints**.\n\nHowever, how do you prevent the user from entering some alpha characters and crashing the program? \n\nWell, you don't, buuuut you can catch the error with the method std::cin.fail():\n\n```cpp\n\nint main(void)\n{\n  long int phonenumber;\n   \n  std::cin \u003e\u003e phonenumber;\n  if (std::cin.fail() == true) // For example if the input is invalid because it is not the right type\n  {\n    std::cin.clear(); // Clears the error flag from cin.fail();\n    std::cin.ignore(); // Ignores the fail that just happened\n    std::cout \u003c\u003c \"Invalid input ! Try again.\" \u003c\u003c std::endl;\n    std::cin \u003e\u003e phonenumber;\n  }\n  std::cout \u003c\u003c \"Good ! Your phonenumber is : \" \u003c\u003c phonenumber \u003c\u003c std::endl;\n}\n\n```\n**Ressource**\n[How to use cin.fail() in c++ properly](https://stackoverflow.com/questions/33284483/how-to-use-cin-fail-in-c-properly)\n\n***std::cin.ignore tip :***\n\nIf you try and keep asking for the user's input while the input given is wrong/triggers std::cin.fail(), your error_msg will repeat itself for as many times as there are chars in the input's string. To avoid this, use the argument:\n\n```cpp\n// will ignore any other input that is not an integer and will skip to the new line. \n   std::cin.ignore(std::numeric_limits\u003cstd::streamsize\u003e::max(), '\\n'); \n```\n**Ressources**\n[1](https://stackoverflow.com/questions/16726657/checking-for-valid-type-input-using-stdcin-c)\n[2](https://stackoverflow.com/questions/66433755/stdcin-failure-leading-to-looped-if-statement-in-while-loop)\n\n\n***Preventing the program from crashing with a EOF***\n\nIf the user's (or any tester) tries to end the program with CTRL+D while we are in the std::cin/getline stage, you will have a never-ending loop of your prompt. To catch this signal, use the std::cin.eof() method:\n\n```cpp\n\t\nvoid\taskForNickname(Contact *contact)\n{\n\tstd::string\tnickname;\n\n\tstd::cout \u003c\u003c BLUE \u003c\u003c \"📜 May I ask for your nickname?\" \u003c\u003c RESET \u003c\u003c std::endl;\n\tstd::getline(std::cin, nickname);\n\tif (std::cin.eof() == true) // If the program catches an EOF, exit safely the program\n\t\t\texit(0);\n\n\twhile (nickname.empty()) // aka \"while the user keep hitting the return key\"\n\t{\n\t\tstd::cout \u003c\u003c BLUE \u003c\u003c \"📜 Don't be shy! Please speak louder.\" \u003c\u003c RESET \u003c\u003c std::endl;\n\t\tstd::getline(std::cin, nickname);\n\t\tif (std::cin.eof() == true)\n\t\t\texit(0);\n\t}\n\tcontact-\u003esetNickname(nickname);\n\treturn ;\n}\n```\n\n\n### Comparing strings (difference with C)\n\n ```cpp\n#define SUCCESS 0\n\nstd::string input;\nstd::string add_command(\"ADD\");\n\nstd::getline(std::cin, input);\nstd::cout \u003c\u003c add_command.compare(input) == SUCCESS ? \"OK\" : \"KO\" \u003c\u003c std::endl;\n \n// is the same as:\nstd::cout \u003c\u003c (input == \"ADD\") ? \"OK\" : \"KO\" \u003c\u003c std::endl;\n```\n \n### Formatting your output on the terminal\n\nNo need to code formatting functions from scratch with the iomanip library! \n\n| Function         | Use                                                                   | Link  |\n|------------------|-----------------------------------------------------------------------|-------|\n| std::right       | Modifies the positioning of the fill characters in an output stream.  | [Here](https://en.cppreference.com/w/cpp/io/manip/left) |\n| std::setw(int n) | Sets the field width to be used on output operations.                 | [Here](https://cplusplus.com/reference/iomanip/setw/)\n\n**Useful member functions to truncate the strings according to the subject's needs:**\n\u003e \"Si le texte dépasse la largeur de la colonne, il faut le tronquer et remplacer le dernier caractère affiché par un point (’.’).\"\n\n```cpp\nstd::string\ttrunc(std::string info)\n{\n\tif (info.length() \u003e 10)\n\t{\n\t\tinfo.resize(9); // keeps the first 9th chars of the string\n\t\tinfo.append(\".\"); // appends a dot as required to the precedently modified string\n\t}\n\treturn info;\n}\n```\n\nAll std::string member functions [here](https://cplusplus.com/reference/string/string/).\n\u003c/p\u003e\n\u003c/details\u003e\n\n\n## Module 01\n\n\u003c!--pb pour imprimer addresse\n\n static cast trop cool a apprendre\n difference between a pointer and a reference\n https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable +pb d'init constructeur avec une classe en param  / exo sed file exists: https://stackoverflow.com/questions/1647557/ifstream-how-to-tell-if-specified-file-doesnt-exist / exo sed directories : https://stackoverflow.com/questions/29310166/check-if-a-fstream-is-either-a-file-or-directory / pointer to functions / switch case prend que enums ou integrals --\u003e\n\n## Module 02\n\n## Module 03\n\n## Module 04\n\n## Module 05\n\n## Module 06\n\nfloat fmod(float a, float b) =\u003e retourne le reste d'une division de a par b (ici, toujours 1)\n\t\t// instant nerd : difference remainder v. fmod = la façon dont on arrondit\n\t\t/*\n\t\t\tremainder : x - r * y, où r est le résultat de x/y, arrondi à la valeur entière la plus proche\n\t\t\tfmod : x _ t * y, où t est le résultat tronqué (aka arrondi vers 0) de x/y\n\n\t\t\tEXEMPLE : \n\t\t\tdouble x = 5.1, y = 3;\n\t\t\tdouble result = remainder(x, y); =\u003e output is -0.9 (car 5.1/3 = 1.7, et là on arrondit à 2. Donc 5.1 - 2 * 3 = -0.9)\n\t\t\tdouble result2 = fmod(x, y); =\u003e output is 2.1\n\t\t\tSource: https://stackoverflow.com/questions/25734144/difference-between-c-functions-remainder-and-fmod \n\n## Module 07\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarineks%2Fcpp_modules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarineks%2Fcpp_modules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarineks%2Fcpp_modules/lists"}