SPEAKER 1: Let's talk about water.
In this problem, we ask the user to tell us how long in minutes their shower was
and then we calculate the equivalent number of bottles of water
that their shower used.
>> So what are our to-do's for this problem?
First, we're going to want to prompt and then validate
the input that the user gives us.
Once we ensure that we have valid user input,
we're going to calculate the equivalent number of bottles of water
that the user had during their shower, and then we're
going to tell them by printing out that number.
>> So let's tackle our first task, prompting and validating
the user input.
For this we've written a function for you called
get_int located in the CS50 Library.
get_int ensures that the user inputs an integer, so any positive numbers,
negative numbers, or zero are all accepted.
But if the user inputs anything else, so any combination of letters or numbers
or decimal numbers, then the user will be prompted to retry
and the function won't accept anything until the user gives them an integer.
>> So how do we use get_int?
If you open your workspace and create a file named integer.c let's
type the following.
Hashtag include cs50.h.
This is necessary because get_int is a cs50 library function,
so we must hashtag include its declaration in order
to use the function.
And then in my main function I'm just going
to simply call the function get_int.
>> So let's run this and see how it works.
I've already compiled it, so let's just go ahead and run this program,
./integer.
Here I have a prompt and here's where I input the value.
Say I put in just some integer, 50, the program excepts and quits
and that's that.
>> But say I run it again and I input something else.
Perhaps, hello world.
That's not an integer so the program will prompt me to retry.
Let's try again with perhaps a decimal this time.
0.5, again, not an integer so the program won't accept it
and it'll tell me to retry.
So let's give it another number.
The program accepts it, quits, and we're done.
>> So now we have a function that allows us and ensures
that the user inputs an integer, but how can we
actually keep track of the integer inputted by the user?
Well, all we'll do is store this value in a variable, say n.
So if I declare an integer n, and I set that value to get_int,
then n will then store whatever value the user inputted.
>> All right, so now we've ensured that the user will give us an integer
and we know how to keep track of the integer, but remember,
integers are both positive and negative.
So it doesn't really make sense in the context of this problem
that the user takes a shower of say negative 12 minutes.
So we need to make sure that the user actually gives us a positive integer.
Now we don't have just a single function for that,
so we're going to have to create that ourselves.
>> So we want to continually prompt the user
until they give us a positive integer.
If I'm doing something continuously then that's akin to a loop, a repetition.
So one of the constructs that we use in c to implement repetition and loops
is a while loop.
So a while loop, as shown here, will execute
whatever is in the body of the loop as long as the given condition evaluates
to true.
As soon as that condition evaluates to false, then
the program will proceed to whatever comes after the body of the loop.
>> So while loops are going to be really useful in CS50.
But in this particular case, we know that we're
going to prompt the user at least once and then only loop if necessary.
So here we come to a special construct and that's
very similar to the while loop called a do-while loop.
>> So a do-while loop executes the body of the loop at least once
and then it checks to see whether it should execute,
as opposed to a while loop, which will check the condition
and then execute the body.
So in a do-while loop what we might do is prompt the user for an integer,
and then check if it's invalid or not.
If it's invalid, then we'll repeat the process asking the user
to give us another integer, and then only when that integer is valid,
we'll continue to whatever comes afterwards.
>> Now you'll notice that the declaration of the integer n
is slightly different to what we did before in the previous example
and this is because of scope.
If we had declared the integer n inside of the body of the do-while loop,
then we wouldn't be able to access that value of n outside of those curly
braces indicating the body of the loop.
But we do in fact want to access the value of n later on in our program.
>> OK, so now let's talk about what this condition should be.
We want to only reprompt the user as long as n is invalid.
So think to yourself what an invalid integer value would look like
and then create a Boolean expression to express that.
>> We're almost finished with our subtask of prompting and validating user input.
So let's make this a little bit more user friendly
and give the user a little bit more information
of what we're prompting them for.
So let's prompt the user, per the spec, with the string minutes.
So using your printf statements, make sure that you match this exactly.
>> All right.
So now we have a valid user input, a positive integer
value for how many minutes they spent in the shower.
So what's next is to calculate the equivalent number of bottles.
What we're going to do here might be very obvious to you at first,
and that's OK.
What we want to do is we want to start practicing
the idea of detecting patterns and developing formulae for the problem.
>> So we're told per the spec that one minute in the shower
is equivalent to about 12 bottles of water.
So then two minutes would be equivalent to 24,
and five minutes would be equivalent to 60.
So now if you think you've got it, then let's
see if you can come up with a pattern or formula
to express if we have say, n minutes, how many bottles of water
would that be expressed in terms of n?
>> Again, even though this might be simple at first,
later on when we get to more complicated problems
we're going to want to get into the practice of identifying
patterns and developing formulae to figure these things out.
>> In c you have access to standard arithmetic operations,
addition, subtraction, multiplication, and division.
So I'll leave it to you now to figure out
how to express the equivalent number of bottles
that the user consumed during their shower.
>> All right, we're almost done.
We've prompted the user for their input, we've ensured that it's valid input,
and then we've figured out how to calculate the equivalent
number of bottles that they consumed.
So the last thing to do is to output the equivalent number of bottles
and hopefully encourage them to conserve some water.
>> Outputting the value will be a printf statement.
If I wanted to tell you that I have three pets, for instance,
I might use a printf statement that looks something like this.
I have three pets, and a new line for nice formatting.
>> Now we don't want to simply hard code things.
Say the number of my pets changes over time.
Then I'm going to make use of placeholders in my printf statement.
So here my number is an integer.
So I'm going to make a placeholder for an integer using %i.
I'm going to write the string, and then after the string
I write a comma and then the variable that I want to print.
So that value will be printed in lieu of that placeholder, %i.
>> You can use placeholders, as well, for floats with a %f.
You can also have multiple placeholders in a string.
For instance, if I have some number of dogs and some number of cats,
I put two placeholders here and then my two variables in respective order.
>> So now that we know how to print the values stored in variables,
the very last thing to do is to ensure that we
print the value in the exact format indicated in the specification.
With that, we've prompted the user and validated their input.
We've then calculated the equivalent number of bottles of water
that they consume during their shower, and we've outputted that value to them.
And so, we've completed water.
>> My name is [? Zamila, ?] and this is CS50.