User Input - C++ Tutorial For Beginners #6

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

This video tutorial covers user input in C++ for beginners, providing a foundational understanding of how to handle user interactions within C++ programs.

Full Transcript

it's not a game it's a [Music] r what is going on guys welcome back to the C++ tutorial series on nural 9 today's video we're going to talk about user input so let us get right into it all right so up until now our programs were pretty static we didn't have any user interaction we didn't have any uh information from outside of the program code everything we did was hardcoded every value that we chose uh was hardcoded and when we wanted to change something we had to change the code and recompile it and we're going to change this in today's video by introducing user input by by learning how to process user input and you already know about the function stdc out which takes some text and puts it out onto the screen uh but there's also the function called cin so SCD C in and it's the opposite thing it gets something from the console and gets it into the program code so to say um and let's say we just put out something like please please enter your name and we're not going to do an end line here because we want the user to continue to put the text into the same line um now what we can do is we can call the cin function but since this function is getting something from the console window from the user and putting it into this into the script uh not script sorry into the program uh this function is called C in and also we need to use the arrows in the opposite direction so in this case we don't say see out and then we feed something into it we we say C in and extract something out of it so in this case we can put this into a variable and let's create this variable by saying SD string name and then we say stdc in the result shall be saved in name and after that we can go ahead and say your name is name like that so when we have this we can go ahead press F6 uh enter the name let's say neural 9 and you can see your name is neural 9 in this case uh so this is how you process user input you just say C in and whatever you get out of cin by pressing enter in the end uh you save into the variable that you define um now you can also do this with other data types so if you now pass 10 it's going to be saved in a string so it's going to be not 10 as a as a numerical value but 10 like this uh as a string just a text 10 but if I change this for example to let's say in h and we say please enter your H and we want to see in the H and then you say your H is H uh this is now an integer so whatever we put into this uh program is going to be considered an integer we don't need to typ cast it it's going to be typ casted automatically the cin function returns or or what we whatever we extract out of the cin function uh will be the data type that we need in this case integer of course um I'm not sure if this is going to work with all classes and objects and data types but it works for INT it works for float it works for Strings and so on so for the primitive data types it seems to work and we can can look at it enter your age 10 your age is 10 and to see that this is actually an integer and not uh just a string you can go ahead and add 20 here to it just to see what happens and when we now enter 10 we get 30 and since we can Calculate 10 + 20 we know that H is in fact an integer as we already stated so this is how you process user input in C++ by the way for those of you who are wondering what I'm using to compile the program code all the time I'm using an extension here in Visual Studio code called CC plus plus compile run it is by Daniel pinto8 zz6 a very basic extension you just install it and then you can press F6 to compile the code and run it and you can press F7 to run it with some additional parameter for those of you who are wondering what I'm using to uh compile all the all the program code here by just clicking F6 all right so with the knowledge we now have we should be able to easily Implement a basic addition calculator with uh the numbers put in by the user so let's say we have something like int and one and two then we say see out actually by the way for those of you who are wondering you can also go ahead and say using STD um see out if you just want to use use STD C out and not import the whole namespace STD so if you want to do that you can do that with C out we can do that with C in and we can do that with endline uh I'm not claiming that this is in any way best practice or something but since we're just learning we can use it like that uh usually I think it's best practice to just say STD all the time so to specify it uh separately or individually every time um but since we're just learning here it doesn't matter too much if we're using best practices uh however we can now go ahead and just say C out and we can say please enter the first number like that and we're not going to use an end line here and we're going to do the same thing for the second number and in between we're going to say CN N1 and C in and two and in the end what we're going to do is we're going to say c out sum is or let's let's use an English sentence here the sum is N1 + N2 end line and if we compile and run this you can see we can enter a number here let's say 8 and 19 and we get 27 as a sum so that's how you uh can build a basic addition uh Dynamic Addition calculator in C++ by using user input and then a basic arithmetic operation now one more little thing that we can do with the cin function here especially when we're learning C++ is we can just wait for user input before we terminate the program so let's say we have something like hello world like in the first video where we did some actual code hello world uh the problem is when we go ahead and compile this here um and run it of course uh oh yeah we need Tod again we just go ahead and compile this and run run this it's going to work but as you can see the script uh not the script sorry the program terminated immediately and if we go ahead and uh go to the desktop here where this is compiled and we run it you can see that it opens up down down in the Tas but you can see it opens up for a second and then it's gone again because uh it's already terminated you just print hello world and that's it and you're gone if you want to wait uh before you terminate for a user input what you can do is you can say STD cn. get this essentially just waits for some user input before uh it proceeds with the code and since it's not proceeding since it's not done with everything yet that is coded inside of this program it's going to wait and the window is not going to disappear before you click enter so we're going to compile this here and when we do this oh of course we need a semicolon here when we do this you can see that it says hello world but it's it's not terminating it's still running when I press enter it's done and we can see that when we go ahead and run this executable file here we can see Hello World actually waiting for us for more input and I can put in some stuff here and it's still not terminating but when I click enter it's going to terminate and this is something that we can use in order to pause and to wait for the user to click enter before we actually terminate the script so that's it for this video hope you enjoyed and hope you learn something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to Channel and click the notification Bell to not miss a single future video other than that thank you very much for watching see you in the next video and bye [Music]

Original Description

In this video we talk about user input in C++. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video teaches beginners how to work with user input in C++, covering the basics of input/output streams and console interaction. By watching this tutorial, viewers will gain hands-on experience with handling user input in C++ programs. The tutorial is designed for those new to C++ and looking to build a strong foundation in programming fundamentals.

Key Takeaways
  1. Create a new C++ project
  2. Set up input/output streams
  3. Handle user input using console input
  4. Validate and process user input
  5. Output results to the console
💡 Understanding how to handle user input is crucial for creating interactive C++ programs, and this tutorial provides a step-by-step guide on how to achieve this.

Related AI Lessons

Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →