hey I'm Chuck McCullough welcome to episode three of our learning to program in C plus plus live
stream here on YouTube I hope you're having a great day and I want to welcome everybody that's
joining the live stream as well as welcome all of you that are joining us on the replay
leave me a comment let me know how you're liking the course and if you want to see anything else
without further Ado though let's jump right into it all right in this lesson we're going to look
at a sample project that will be implementing for a customer from that project those requirements
we're going to make a plan always make a plan no matter how simple or complicated we need to
do a little bit of design work so that we have a feel for what we're doing in order
to implement this application we need to learn about conditionals as well as Loops in C plus
all right so what's this project the customer says they need a program that's going to calculate the
area of circles rectangles and probably in the future some additional shapes when we run the
program they want it to First prompt the user for what kind of shape they want to calculate
C for Circle R for rectangle stuff like that and then based on what they picked we'll prompt them
for the dimensions either the radius or the width or the height then we'll display the area and the
perimeter or circumference in the case of a circle of the two shapes or whatever shape they chose
they also want us to do this over and over again until the user enters Q so when we prompt them for
a circle or rectangle we'll also prompt them for quit and that's how we quit the program
so let's make a plan so I'm going to demonstrate this with a simple flowchart first of all what
type of shape so we'll prompt the user Circle or rectangle C or R and we'll wait for the user to
make that input once they make their input we'll decide is it one of the three valid
characters quit rectangular Circle if it's quit then we're going to end the program terminate
if it's something other than RC or Q we'll just come back and prompt them again
and wait for them to do this until they enter the correct response if it's a c or an R we'll make
a decision if it's a rectangle then we'll prompt them for the height and the width
if it's a circle we'll prompt them for the radius regardless we still have to calculate the area
of the circle or the rectangle and display the results of those calculations after we make the
calculation we'll display the area in perimeter and then go back and display the menu again
so the first thing we need to know how to do is write output now you learned about this last
week in last week's session remember that c out or standard C out is our standard output device
and we use the insertion operator the two less than symbols to insert data onto that stream and
we can insert a large variety of different data types onto that stream floats doubles characters
strings integers a lot of different things can be streamed to an output stream we can also use one
of the special manipulators called indel to add a new line character to the stream anywhere we wish
indel and C out are both defined in the header file IO stream so we need to include that at the
top of our source file to read input we use CN that's our standard input device again Define an
i o stream this time the arrows go the other direction we use the greater than symbol to
read data or extract data from that stream the user input is always text end users interact
with the computer using text or strings but the stream does know how to automatically convert to
primitive types as you saw in the last session when we prompted the user for a circle's radius
okay so with those two things we're ready to create this part of the application where we
prompt the user to enter their menu selection and wait for them to make
that entry so as usual the same old boilerplate stuff with our include our namespace and Main
using a c out and a whole bunch of chain together C out statements terminated with a semicolon
we are presenting our menu I didn't put an indel at the very end because I want the blinking cursor
to be sitting right after the word quit so that when the user when we the user has to enter the
character it will show up right after that menu prompt now once the user has entered something we
need to make a decision on what it is that they entered so we need to talk about if and else
those are used to create conditional blocks so inside curly braces we can have all of the
statements that we want to execute if a condition is true optionally the else a block of statements
that will execute if that condition was false we can also use else if to chain together multiple
conditions so let's implement the logic where we sort out whether it's a q a c or an R so appending
on to the end of the logic that we already have in our application we'll check first to see if
we're supposed to quit then we'll check to see if we're going to do a circle or a rectangle
if the user chooses Q we're going to return 0. the return and then 0 is because our function
returns an integer that's the exit code to the operating system a little beyond the scope of
what we're doing right now we also exit the application when we reach this close brace
as well that also ends the program and the result code of the operating system is garbage because we
didn't specify anything else so it's just whatever random value happens to already be in memory also
I'm checking for upper or lowercase characters using the or operator two vertical bars means or
equality comparison is done with double equal signs so if the variable option is
equal to the character C or it's equal to the character uppercase C same thing with r
if either condition is true then the overall comparison is true and we'll
execute what's inside the body we have a final else here this would be the case
where the user entered something other than q c or R will end up in this block of code
so let's fill in our curly braces with some actual program logic we'll start with the circle
calculation which you did in last week's session so let's just paste that code right inside the if
the else if make sure that you get it neatly inside the curly braces for the C option the
circle option all of our variables are declared within those curly braces so they are scoped to
these curly braces so these variables Pi radius area and circumference they all go away when we
hit the close brace but that's okay because we don't need them anymore after we get that far
you can even run your program now hip and see how it works let's go ahead and add the code for the
rectangle case very similar so under the block for the elsif for rectangle we'll declare variables to
hold the width and height prompt the user for that information calculate the area and the perimeter
and then output the the results to the end user and finally that final else in our sequence
of decision making that will be our unknown option here we'll display that to the end user
to let them know that we didn't understand what it was they asked us to do at this point
our program is pretty much working as required one thing that we're missing is doing it over
and over and over again until the user hits Q to do something repetitively in C plus we use
what's known as a loop C plus plus has several different keywords for looping
one is the for Loop there we will iterate across a range of values like from 1 to 100
or from fifty to Zero by some increment by ones by twos buy threes looping over a range of values
while and do while are used to Loop until a condition is met until a condition becomes false
while the body of the loop will execute zero or more times and do while will execute one or more
times so what do we need for our application well we're going to prompt the user to enter
CR or q and we're going to do that at least once so probably do while would be appropriate for our
application but honestly do while is pretty uncommon so I'm going to structure it in a
way that the while will actually Loop multiple times and we'll structure it using the while loop
so let's just wrap the entire body of what you have inside of main with while true
while true that's an infinite Loop that's going to run forever uh yes it will until we return 0
if the user chooses the Q option we're going to return immediately because that ends not
only the loop but it ends the entire application let's look at the whole thing from top to bottom
so we're including our stuff we have our main function here's our while true and the curly
braces for the wild true are for the entire length there's the close brace for the while
true and there's the close brace for the function Main this is not really an infinite Loop because
if we encounter the return 0 not only do we exit the loop we also exit main our if else's
are stacked up each else needs to immediately follow the closed brace from the preceding if the
else is optional as is the if that's associated with the else so if it's not a q if it's not a c
and if it's not an R we end up in this final else case where we output that it is an unknown option
if we get all the way to the closed brace for the while loop we return execution to the top where
we evaluate the condition the condition is true it's always true so we're going to repeat this
if I run my application we can choose to enter a circle we'll enter the radius of a circle let's
say four and I get a reasonable response look the menu has repeated this time I'll choose R
for rectangle a width of Two and a height of 3. we get the correct answer as a response and then
I'll hit Q oh let's hit something else let's hit K which it says unknown option now we'll
try q and the program exits so the we are checking for both upper and lower case for the queue that
way we can type in either one all right so I hope you learned something valuable today if
else Loops using the while loop and we've learned a little bit about thinking through a program and
executing the application we've declared more variables and we've written a complete program
with decision making and loops hey if you'd like to see anything else or if you have suggestions
or comments about this live stream please leave a comment below if you're watching this in the
replay leave a comment if you liked it give me a thumbs up and always of course subscribe to our
channel so that you're notified when we introduce new videos as well as more live streams now coming
up next week same time same same bat Time same bat Channel we're going to expand our knowledge
of C plus plus by talking about functions hey I'm Chuck Mccalla I'll see you next week