How to Program A Calculator in C++! (Great C++ Microsoft Visual Studio Beginner Project)
Summary
TLDRIn this video, the presenter guides viewers through creating a basic four-function calculator using C++ in Microsoft Visual Studio. The tutorial covers essential programming concepts such as user input and output, console display, and basic mathematical operations. The script includes detailed steps for setting up the project, defining variables, and implementing an if-else statement to handle different arithmetic operations. The video is designed for beginners to practice handling real-time user data and processing it with simple calculations.
Takeaways
- 💻 The video is a tutorial on creating a simple four-function calculator using C++ in Microsoft Visual Studio.
- 📝 The tutorial covers the basics of user input and output, displaying console messages, and performing arithmetic operations.
- 📚 It introduces the use of `iostream` for console interaction and `cmath` for mathematical functions.
- 🔢 The calculator is designed to handle basic arithmetic operations: addition, subtraction, multiplication, and division.
- 🎯 The script guides viewers on defining variables for operations and numbers, using strings and doubles for flexibility.
- 👨💻 It demonstrates how to prompt users for input and store it in variables, using `std::cout` for output and `std::cin` for input.
- 🔑 The tutorial introduces conditional statements (`if`, `else if`, `else`) to handle different arithmetic operations based on user input.
- 🛠️ The script emphasizes the importance of proper formatting and indentation in C++ code for readability.
- 📈 The video serves as a beginner project to help viewers understand how to process real-time user data in C++ programs.
- 🌟 The presenter encourages viewers to engage with the content by liking, subscribing, and providing feedback for future tutorial topics.
Q & A
What is the main focus of the video?
-The video focuses on teaching viewers how to create a simple four-function calculator using C++ in Microsoft Visual Studio.
What are the basic functionalities of the calculator being built in the video?
-The calculator is designed to perform basic arithmetic operations: addition, subtraction, multiplication, and division.
Which programming language is used in the video to create the calculator?
-The programming language used is C++.
What are the user inputs required for the calculator?
-The user inputs required are two numbers and an operation (either addition, subtraction, multiplication, or division).
What does the 'cmath' library provide in the context of the calculator project?
-The 'cmath' library is used to perform basic mathematical functions if needed.
Why is the 'iostream' library included in the project?
-The 'iostream' library is included to handle user inputs and outputs, allowing the program to display information on the console and receive user input.
How does the video guide viewers to handle user inputs for the calculator?
-The video demonstrates how to use 'cin' for taking user inputs and 'cout' for displaying prompts and results to the user.
What data types are used for the variables storing the operation and numbers in the calculator?
-A string data type is used for the operation variable, and double is used for the two number variables to allow for decimal values.
How does the video handle different arithmetic operations in the calculator?
-The video uses a series of 'if-else if' statements to check the entered operation and perform the corresponding arithmetic operation.
What does the video suggest for handling unrecognized operations?
-For unrecognized operations, the video suggests displaying an error message stating 'not a recognized operation'.
What is the purpose of the video in the context of learning C++?
-The video serves as a tutorial for beginners to get familiar with user inputs, outputs, console interactions, and basic arithmetic operations in C++.
Outlines
💻 Introduction to Building a Simple Calculator in C++
The video introduces a tutorial on creating a basic four-function calculator using C++ in Microsoft Visual Studio. It's aimed at those familiar with the basics of C++ and serves as a practical project to understand user input and output, console interaction, and basic mathematical operations. The setup involves including necessary libraries like iostream for console interaction and cmath for mathematical functions. The tutorial begins with defining variables for the operation and two numbers, using doubles for flexibility in handling fractional numbers. It also touches on the efficient practice of defining multiple variables of the same type in a single line.
🔍 Implementing User Input and Conditional Logic
This section of the video script details the process of capturing user input for the calculator. It explains how to prompt the user to enter the first number, the operation (addition, subtraction, multiplication, division), and the second number using standard input/output streams. The script then transitions into using if-else statements to handle the logic for different operations. It demonstrates how to structure these conditional statements to perform calculations based on user input and how to format the code for readability. The tutorial also includes a test run of the calculator with various inputs to ensure the logic is correctly implemented.
🎉 Wrapping Up the Calculator Project and Engaging the Audience
The final paragraph wraps up the tutorial by emphasizing the educational value of the calculator project for beginners in C++. It highlights the use of numerical data types, strings, if-else statements, and console interactions as key components of the project. The speaker encourages viewers to like, subscribe, and comment on the video if they have questions or suggestions for future content. Additionally, there's a call to action for viewers to support the channel through Patreon if they appreciate the content. The video concludes with a thank you note and well wishes for the viewers' coding endeavors.
Mindmap
Keywords
💡C++
💡Microsoft Visual Studio
💡iostream
💡cmath
💡User Inputs
💡Console Window
💡Variables
💡If-Else Statements
💡Arithmetic Operations
💡Real-Time Processing
Highlights
Introduction to creating a simple four-function calculator using C++ in Microsoft Visual Studio.
Explanation of including necessary libraries iostream and cmath for console input/output and basic mathematical functions.
Demonstration of setting up the basic structure of the main function in C++.
Guidance on defining variables for operation and two numbers, with a choice between using doubles for fractional numbers or integers for whole numbers.
Tutorial on defining multiple variables of the same type on the same line for cleaner code.
Instructions on using cout to prompt the user for the first number.
Explanation of using cin to receive user input and store it in the defined variables.
Process of getting user input for the operation and the second number, similar to the first number input.
Introduction to if-else statements in C++ for checking the user's operation choice.
Example of using if-else statements to perform addition based on user input.
Continuation of if-else statements for subtraction, multiplication, and division operations.
Inclusion of an else statement to handle unrecognized operations with a user-friendly error message.
Live demonstration of the calculator handling decimal inputs and performing division.
Testing the calculator with multiplication and checking the result for correctness.
Example of how the calculator handles incorrect operation inputs and displays an error message.
Conclusion summarizing the project's educational value for beginners in C++ programming.
Encouragement for viewers to engage with the content by liking, subscribing, and commenting on the video.
Invitation for viewers to support the channel through Patreon if they appreciate the content.
Transcripts
what's going on everyone welcome back to
the master tech and in today's video i'm
going to show you how to make a simple
four function calculator using c
plus in microsoft visual studio
[Music]
so if you've been following along in the
c
plus uh tutorial series or even if
you're just um familiar with the very
basics of c plus plus this is a great
project to get familiar with uh user
inputs user outputs displaying things to
the console window and performing some
basic operations with inputs you take
from the user so this is going to be
kind of the first program we've built
where you actually get user data in real
time and then process it so to do that
we're going to start just by including
iostream and cmath which we've seen
before and cmath is just basically going
to let us do basic mathematical
functions if we need it and then
iostream is uh how we display things
onto the console and pull feedback from
the console
and this is kind of our basic setup for
our main function that we've had in
every video so far so uh to follow along
just make sure that your code looks like
this in the beginning and let's get into
actually writing the code so the first
thing we want to do is define our
variables and for this project we're
really only going to need three
variables we're going to need the
operation that we're using to
either add subtract multiply or divide
and then we need two numbers that we're
doing the operation too um so to do that
we're going to start by defining a
string for the operation
and then um we're going to define i'm
going to use doubles for my two numbers
just so they can be fractional if i want
them to be fractional you could do it
with integers if you wanted it to only
be integer based math that would be
really simple but you get a little more
flexibility if you make them doubles and
i just want to take this opportunity to
show you we can do one thing that we
haven't talked about yet in the c plus
series you can define multiple variables
of the same variable type on the same
line right here so uh if i don't want to
give them initial values or even if i
did
you can define multiple variables in the
same row now we're going to get the
initial values from the user so i'm not
going to put equals anything for
operation or number like we've talked
about you don't have to give them
initial values when you define them
and so these are the only three
variables we're going to need but it is
nice to know if you had a project with
hundreds of variables you could have all
your strings defined in one place all
your doubles defined in one place
and that can really kind of clean up
your code a little bit when all said and
done
okay so now let's take a look at
actually get
the user input all right so that's going
to be the next section of our code and
to do this uh we're gonna start with c
out which we've used a few times before
which is console out so it's um just
basically us giving the user a prompt
and in this case the prompt is gonna be
enter first number just like that and
we're going to put a space there because
uh it looks kind of good um
and then just make sure you finish every
line with a semicolon again
i'll kind of keep hammering on basic
formatting while we're still doing basic
tutorials
you need every line to end with a
semicolon if it's like the end of a
statement
so then we actually want to take some
feedback back in and for this it should
kind of make sense to you flip the
direction of your arrow keys so instead
of like less than less than sign
it's greater than greater than sign for
something that we're pulling back in
from the console and this is going to
show up as a second row where the user
then enters their data here so it's
going to be cn and then we're going to
write whatever they punch in into number
one our variable number one
just like that so this if i were to run
this we're not doing anything with it
yet but it should run okay if i run it
let's take a look
okay
and so it's asking me to enter first
number and if i go ahead and uh punch in
at 12 and then hit enter it's not going
to do anything but you can see there it
was
prompting me to enter a number and then
waiting for me to put feedback back in
there so we're gonna copy this same
basic format of prompt and then response
um two more two more times and so we're
gonna do it in order we'll get the
number first and then we'll get the
operation
and then we'll get the second number uh
so we'll say enter operation for the
second one and then parentheses i'm just
gonna kind of tell them what they can do
because if you think about like a
calculator you would see all the buttons
that you can press so you would see like
plus minus multiplication division you
would know whether or not something was
an option so i think giving them the
chance to see what operations are their
options is not a bad idea
and then we will just say enter second
number like that and so i'm not going to
run this again because it's going to
look just like what we did for enter
first number um it's just going to ask
us three prompts and then we'll put
stuff in but it's not doing anything
with it yet
um and so what we want is the last bit
is going to be like if else statement
checking
um
entered
spell it right checking entered
operation okay um so i'm working on
better comments in my code because i
want it to be easier to follow along
with so this section we're actually
going to create an if-else series of
statements that check what operation was
just entered and display an output based
on that
so let's go ahead and get started and
this is going to be our first look at
the if statement in c plus as well so if
you've been following along with this
series
if you haven't go check them out but if
you have been following along with the
series you've seen variable types and
some basic string and numerical
operations this is going to be your
first introduction to if else statements
and that's really cool so
if and then in parentheses you want to
put your
kind of if check statement so if
operation is equal to and we'll start
with addition because it's an easy one
that's all you need to put in your
parentheses but then you make these
curly brackets and inside these curly
brackets is where we put what to do in
the event that that is true so if the
operation is plus we just want to
display onto the console
number one plus
number two and it knows how to interpret
that uh one plus two just like that and
then we put our semicolon and then our
closed curly brackets and then right
after that closed curly bracket you can
put else if and then we're just going to
basically copy this
for each of those if
statements for each of our operators i'm
saying a lot i'm going to try to stop
doing that
so
else if operation is minus now we want
to just c out number one minus number
two semicolon make sure you have your uh
formatting correct the indentation
doesn't matter as much in c plus plus as
it would in like python um there's
another one uh so just try to
make it look in a way that feels good to
you because strictly speaking there are
no rules about line breaks so this could
go on for numer this could be one line
of code really if you use the semicolons
and curly brackets in the right spot
so pick a formatting that works for you
and i'm gonna kind of just copy this if
i turn it into an else if and do this
four times uh you can even see the
there's no like automatic formatting
it's gonna do for you
it may or may not just have stuff jump
around and put stuff on a new line
sometimes the default visual studio code
might actually separate it out like this
i think it does
where it puts this curly bracket as like
its own line you can keep it this way if
you want again it really isn't going to
impact the functioning of your code it's
just what do you prefer and if your
company or project team has a standard
that they're trying to follow follow
their standard don't
don't step on their toes just because it
doesn't matter technically
all right so we have multiplication and
division in here and let's do one more
thing which is just else now instead of
an else if and else is just going to
mean they entered something that isn't
any of those operations so we just want
to see out just like the other ones but
now we'll say
not a
recognized
operation just like that and so
basically we're saying okay you didn't
give me any of the four functions that i
told you we would be able to do with
this calculator so i'm just going to
give you an error saying not a
recognized operation like that okay
so let's go ahead and run this let's see
if we actually captured all scenarios
okay
so let's say enter first number let's do
23.5 because it should be able to handle
decimals let's do divided by
and let's do 2.7 something that's not
easy
okay
8.7037 i actually don't know if that's
exactly right but it must be pretty
close um just kind of looking at the
numbers here that makes sense
um so let's do it again uh let's go
ahead and say enter first number 123
um
times
2.678
and we get
329.394 that looks right to me let's go
ahead and just test this scenario of
entering something stupid for an
operation so let's say 12
um
i don't know
pete
as in operation and then second number
23 not a recognized operation there you
go so this is pretty rudimentary code
still we're obviously not into like
really advanced code in any way but if
you're following along in the c plus
plus tutorial series this is a really
cool start to start getting user input
and doing something with that data we're
using numerical data types we're using
strings we're using if else statements
and we're printing to the console we're
taking data back from the console so
this is a really great beginner project
i hope you found it useful if you did be
sure to leave a like on the video
subscribe to the channel let me know in
the comments if you had any questions or
what you'd like to see in a future video
on the channel and if you love the
content i've been putting out you want
to become a super supporter of the
channel feel free to check out the
patreon link in the description of this
video and as always thanks for watching
and good luck with your code thanks bye
浏览更多相关视频
C++ Banking program for beginners 💰
Build this JS calculator in 15 minutes! 🖩
41. OCR A Level (H046-H446) SLR8 - 1.2 Introduction to programming part 2 variables & constants
Asp.Net Core Web API Client/Server Application | Visual Studio 2019
Simple Tkinter Demo
Create Your First C# Windows Forms Application using Visual Studio
5.0 / 5 (0 votes)