How to Program A Calculator in C++! (Great C++ Microsoft Visual Studio Beginner Project)

LeMaster Tech
11 Aug 202210:37

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

00:00

πŸ’» 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.

05:01

πŸ” 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.

10:03

πŸŽ‰ 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++

C++ is a high-level programming language developed by Bjarne Stroustrup. It is an extension of the C programming language and supports object-oriented programming, among other paradigms. In the video's context, C++ is used to demonstrate how to create a simple four-function calculator, highlighting its capabilities for handling user inputs and outputs, as well as performing arithmetic operations.

πŸ’‘Microsoft Visual Studio

Microsoft Visual Studio is an integrated development environment (IDE) from Microsoft that facilitates the development of applications for various platforms. The video references using Visual Studio as the platform for creating the calculator program in C++, showcasing its role in providing tools for coding, debugging, and compiling.

πŸ’‘iostream

In C++, iostream is a header file that provides functions for input and output stream operations. The video mentions including iostream to handle user inputs and outputs, such as displaying prompts and reading user inputs, which are essential for the calculator's functionality.

πŸ’‘cmath

The cmath library in C++ provides support for mathematical functions, including trigonometric, logarithmic, and exponential functions. Although the video's calculator focuses on basic arithmetic, the mention of cmath suggests its potential use for more complex mathematical operations if needed.

πŸ’‘User Inputs

User inputs are data provided by the user during the execution of a program. The video script discusses getting user inputs as a key aspect of the calculator project, allowing the program to dynamically receive and process numbers and operations from the user.

πŸ’‘Console Window

The console window is a text-based interface where users interact with the program. In the video, the console window is used to display prompts and receive user inputs for the calculator, demonstrating how to interact with users through a command-line interface.

πŸ’‘Variables

Variables in programming are used to store data values. The script defines variables such as 'operation' and 'number' to hold the user's input for the operation and the numerical values, respectively. These variables are crucial for the calculator to perform the correct arithmetic operation.

πŸ’‘If-Else Statements

If-else statements are control flow statements used in programming to execute different blocks of code based on conditions. The video introduces if-else statements to check the user's operation choice and perform the corresponding arithmetic operation, illustrating a fundamental concept in programming logic.

πŸ’‘Arithmetic Operations

Arithmetic operations are basic mathematical operations such as addition, subtraction, multiplication, and division. The video's calculator is designed to perform these operations based on user input, demonstrating how to implement arithmetic logic in a C++ program.

πŸ’‘Real-Time Processing

Real-time processing refers to the ability of a system to process data as it is received. The video emphasizes the calculator's real-time aspect by showing how it processes user inputs immediately to perform calculations, which is a key feature in interactive applications.

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

play00:00

what's going on everyone welcome back to

play00:01

the master tech and in today's video i'm

play00:03

going to show you how to make a simple

play00:04

four function calculator using c

play00:07

plus in microsoft visual studio

play00:11

[Music]

play00:17

so if you've been following along in the

play00:18

c

play00:19

plus uh tutorial series or even if

play00:22

you're just um familiar with the very

play00:23

basics of c plus plus this is a great

play00:26

project to get familiar with uh user

play00:28

inputs user outputs displaying things to

play00:30

the console window and performing some

play00:32

basic operations with inputs you take

play00:35

from the user so this is going to be

play00:37

kind of the first program we've built

play00:39

where you actually get user data in real

play00:43

time and then process it so to do that

play00:46

we're going to start just by including

play00:47

iostream and cmath which we've seen

play00:50

before and cmath is just basically going

play00:52

to let us do basic mathematical

play00:54

functions if we need it and then

play00:56

iostream is uh how we display things

play00:58

onto the console and pull feedback from

play01:00

the console

play01:01

and this is kind of our basic setup for

play01:04

our main function that we've had in

play01:05

every video so far so uh to follow along

play01:08

just make sure that your code looks like

play01:10

this in the beginning and let's get into

play01:12

actually writing the code so the first

play01:14

thing we want to do is define our

play01:16

variables and for this project we're

play01:19

really only going to need three

play01:20

variables we're going to need the

play01:21

operation that we're using to

play01:24

either add subtract multiply or divide

play01:26

and then we need two numbers that we're

play01:28

doing the operation too um so to do that

play01:31

we're going to start by defining a

play01:33

string for the operation

play01:36

and then um we're going to define i'm

play01:38

going to use doubles for my two numbers

play01:39

just so they can be fractional if i want

play01:41

them to be fractional you could do it

play01:43

with integers if you wanted it to only

play01:45

be integer based math that would be

play01:47

really simple but you get a little more

play01:49

flexibility if you make them doubles and

play01:52

i just want to take this opportunity to

play01:53

show you we can do one thing that we

play01:55

haven't talked about yet in the c plus

play01:57

series you can define multiple variables

play01:59

of the same variable type on the same

play02:02

line right here so uh if i don't want to

play02:04

give them initial values or even if i

play02:06

did

play02:07

you can define multiple variables in the

play02:10

same row now we're going to get the

play02:13

initial values from the user so i'm not

play02:15

going to put equals anything for

play02:16

operation or number like we've talked

play02:19

about you don't have to give them

play02:20

initial values when you define them

play02:23

and so these are the only three

play02:25

variables we're going to need but it is

play02:27

nice to know if you had a project with

play02:29

hundreds of variables you could have all

play02:31

your strings defined in one place all

play02:33

your doubles defined in one place

play02:35

and that can really kind of clean up

play02:37

your code a little bit when all said and

play02:39

done

play02:40

okay so now let's take a look at

play02:42

actually get

play02:43

the user input all right so that's going

play02:46

to be the next section of our code and

play02:48

to do this uh we're gonna start with c

play02:50

out which we've used a few times before

play02:52

which is console out so it's um just

play02:55

basically us giving the user a prompt

play02:57

and in this case the prompt is gonna be

play03:00

enter first number just like that and

play03:02

we're going to put a space there because

play03:04

uh it looks kind of good um

play03:07

and then just make sure you finish every

play03:09

line with a semicolon again

play03:11

i'll kind of keep hammering on basic

play03:13

formatting while we're still doing basic

play03:14

tutorials

play03:16

you need every line to end with a

play03:18

semicolon if it's like the end of a

play03:19

statement

play03:20

so then we actually want to take some

play03:23

feedback back in and for this it should

play03:25

kind of make sense to you flip the

play03:27

direction of your arrow keys so instead

play03:29

of like less than less than sign

play03:31

it's greater than greater than sign for

play03:33

something that we're pulling back in

play03:35

from the console and this is going to

play03:37

show up as a second row where the user

play03:40

then enters their data here so it's

play03:41

going to be cn and then we're going to

play03:44

write whatever they punch in into number

play03:46

one our variable number one

play03:49

just like that so this if i were to run

play03:51

this we're not doing anything with it

play03:53

yet but it should run okay if i run it

play03:56

let's take a look

play03:58

okay

play03:59

and so it's asking me to enter first

play04:01

number and if i go ahead and uh punch in

play04:03

at 12 and then hit enter it's not going

play04:06

to do anything but you can see there it

play04:08

was

play04:09

prompting me to enter a number and then

play04:11

waiting for me to put feedback back in

play04:12

there so we're gonna copy this same

play04:15

basic format of prompt and then response

play04:17

um two more two more times and so we're

play04:20

gonna do it in order we'll get the

play04:22

number first and then we'll get the

play04:24

operation

play04:25

and then we'll get the second number uh

play04:27

so we'll say enter operation for the

play04:31

second one and then parentheses i'm just

play04:33

gonna kind of tell them what they can do

play04:35

because if you think about like a

play04:36

calculator you would see all the buttons

play04:38

that you can press so you would see like

play04:40

plus minus multiplication division you

play04:42

would know whether or not something was

play04:44

an option so i think giving them the

play04:47

chance to see what operations are their

play04:49

options is not a bad idea

play04:51

and then we will just say enter second

play04:53

number like that and so i'm not going to

play04:55

run this again because it's going to

play04:57

look just like what we did for enter

play04:59

first number um it's just going to ask

play05:01

us three prompts and then we'll put

play05:02

stuff in but it's not doing anything

play05:04

with it yet

play05:05

um and so what we want is the last bit

play05:08

is going to be like if else statement

play05:11

checking

play05:12

um

play05:13

entered

play05:14

spell it right checking entered

play05:17

operation okay um so i'm working on

play05:20

better comments in my code because i

play05:21

want it to be easier to follow along

play05:23

with so this section we're actually

play05:25

going to create an if-else series of

play05:28

statements that check what operation was

play05:30

just entered and display an output based

play05:32

on that

play05:34

so let's go ahead and get started and

play05:35

this is going to be our first look at

play05:37

the if statement in c plus as well so if

play05:39

you've been following along with this

play05:41

series

play05:42

if you haven't go check them out but if

play05:43

you have been following along with the

play05:45

series you've seen variable types and

play05:46

some basic string and numerical

play05:48

operations this is going to be your

play05:50

first introduction to if else statements

play05:52

and that's really cool so

play05:54

if and then in parentheses you want to

play05:56

put your

play05:57

kind of if check statement so if

play06:00

operation is equal to and we'll start

play06:02

with addition because it's an easy one

play06:04

that's all you need to put in your

play06:06

parentheses but then you make these

play06:07

curly brackets and inside these curly

play06:09

brackets is where we put what to do in

play06:12

the event that that is true so if the

play06:14

operation is plus we just want to

play06:16

display onto the console

play06:19

number one plus

play06:21

number two and it knows how to interpret

play06:23

that uh one plus two just like that and

play06:26

then we put our semicolon and then our

play06:28

closed curly brackets and then right

play06:30

after that closed curly bracket you can

play06:32

put else if and then we're just going to

play06:35

basically copy this

play06:37

for each of those if

play06:39

statements for each of our operators i'm

play06:42

saying a lot i'm going to try to stop

play06:44

doing that

play06:45

so

play06:46

else if operation is minus now we want

play06:49

to just c out number one minus number

play06:52

two semicolon make sure you have your uh

play06:55

formatting correct the indentation

play06:57

doesn't matter as much in c plus plus as

play06:59

it would in like python um there's

play07:02

another one uh so just try to

play07:05

make it look in a way that feels good to

play07:08

you because strictly speaking there are

play07:10

no rules about line breaks so this could

play07:12

go on for numer this could be one line

play07:15

of code really if you use the semicolons

play07:18

and curly brackets in the right spot

play07:20

so pick a formatting that works for you

play07:23

and i'm gonna kind of just copy this if

play07:27

i turn it into an else if and do this

play07:31

four times uh you can even see the

play07:34

there's no like automatic formatting

play07:36

it's gonna do for you

play07:38

it may or may not just have stuff jump

play07:41

around and put stuff on a new line

play07:43

sometimes the default visual studio code

play07:47

might actually separate it out like this

play07:49

i think it does

play07:50

where it puts this curly bracket as like

play07:53

its own line you can keep it this way if

play07:55

you want again it really isn't going to

play07:57

impact the functioning of your code it's

play08:00

just what do you prefer and if your

play08:02

company or project team has a standard

play08:05

that they're trying to follow follow

play08:06

their standard don't

play08:08

don't step on their toes just because it

play08:10

doesn't matter technically

play08:13

all right so we have multiplication and

play08:15

division in here and let's do one more

play08:17

thing which is just else now instead of

play08:20

an else if and else is just going to

play08:22

mean they entered something that isn't

play08:25

any of those operations so we just want

play08:27

to see out just like the other ones but

play08:29

now we'll say

play08:31

not a

play08:32

recognized

play08:34

operation just like that and so

play08:37

basically we're saying okay you didn't

play08:39

give me any of the four functions that i

play08:41

told you we would be able to do with

play08:43

this calculator so i'm just going to

play08:45

give you an error saying not a

play08:47

recognized operation like that okay

play08:50

so let's go ahead and run this let's see

play08:52

if we actually captured all scenarios

play08:54

okay

play08:55

so let's say enter first number let's do

play08:58

23.5 because it should be able to handle

play09:00

decimals let's do divided by

play09:03

and let's do 2.7 something that's not

play09:06

easy

play09:07

okay

play09:08

8.7037 i actually don't know if that's

play09:11

exactly right but it must be pretty

play09:13

close um just kind of looking at the

play09:15

numbers here that makes sense

play09:17

um so let's do it again uh let's go

play09:20

ahead and say enter first number 123

play09:24

um

play09:25

times

play09:27

2.678

play09:29

and we get

play09:30

329.394 that looks right to me let's go

play09:33

ahead and just test this scenario of

play09:35

entering something stupid for an

play09:37

operation so let's say 12

play09:39

um

play09:40

i don't know

play09:43

pete

play09:44

as in operation and then second number

play09:46

23 not a recognized operation there you

play09:49

go so this is pretty rudimentary code

play09:52

still we're obviously not into like

play09:54

really advanced code in any way but if

play09:56

you're following along in the c plus

play09:58

plus tutorial series this is a really

play10:00

cool start to start getting user input

play10:02

and doing something with that data we're

play10:04

using numerical data types we're using

play10:06

strings we're using if else statements

play10:09

and we're printing to the console we're

play10:10

taking data back from the console so

play10:12

this is a really great beginner project

play10:14

i hope you found it useful if you did be

play10:16

sure to leave a like on the video

play10:18

subscribe to the channel let me know in

play10:20

the comments if you had any questions or

play10:21

what you'd like to see in a future video

play10:23

on the channel and if you love the

play10:24

content i've been putting out you want

play10:26

to become a super supporter of the

play10:27

channel feel free to check out the

play10:29

patreon link in the description of this

play10:31

video and as always thanks for watching

play10:34

and good luck with your code thanks bye

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
C++ TutorialCalculator ProjectUser InputConsole OutputBasic OperationsProgramming BasicsVisual StudioCode TutorialIf-Else StatementsBeginner Project