{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basics and data types\n", "\n", "Here is the basic structure for a C++ application.\n", "If you're developing locally, you'll start with just a main function, but this step is unnecesary in a notebook environment." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "#include \n", "\n", "int main(){\n", " double x = static_cast(5.5);\n", " std::string y = \"Hello world\";\n", "\n", " std::cout << x << std::endl;\n", " std::cout << y << std::endl;\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "main();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Here's how to print a variable" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "int v = 5000;\n", "std::cout << v << std::endl;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use variables defined in earlier cells:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "std::cout << x << std::endl;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with strings\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#include \n", "\n", "char name[30];\n", "scanf(\"%29s\", name);\n", "printf(\"Hello %s\", name);" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5\n", "Caleb Curry\n", "Subscriber is 10 characters long\n", "Caleb Curry\n", "Subscriber\n" ] }, { "name": "stdin", "output_type": "stream", "text": [ " Tacos\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Tacos" ] } ], "source": [ "#include \n", "#include \n", "\n", "std::string name = \"Caleb\";\n", "std::cout << name.size() << std::endl;\n", "\n", "name += \" Curry\";\n", "\n", "std::cout << name << std::endl;\n", "\n", "char you[] = \"Subscriber\";\n", "std::cout << you << \" is \" << strlen(you) << \" characters long\" << std::endl;\n", "\n", "//you += \" forever\"; //NOPE\n", "\n", "std::string copy1 = name;\n", "\n", "std::cout << copy1 << std::endl;\n", "\n", "char copy2[11];\n", "strcpy(copy2, you);\n", "\n", "std::cout << copy2 << std::endl;\n", "\n", "std::cin >> name;\n", "std::cout << name;\n" ] } ], "metadata": { "kernelspec": { "display_name": "C++17", "language": "C++17", "name": "xcpp17" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "17" } }, "nbformat": 4, "nbformat_minor": 4 }