SPEAKER: Let's talk about water.
In this problem, we're going to ask the user how many minutes they
spent in the shower.
From there, we're going to estimate the amount of water
that they used returning it to them using
water bottles as a unit of measurement.
Hopefully, after seeing how many water bottles they used in their shower,
they'll start to conserve a little bit more water.
So let me break this problem down into three todos.
Your first task is to prompt and validate the user for their input.
Second is to calculate the equivalent number of bottles
that their shower used.
And lastly, you want to print that number out.
So your program might look something like this.
After including the appropriate libraries,
your main function will first have a section of code
that prompts the user for the minutes they spent in the shower.
Then you'll have a section of code that calculates and outputs
the equivalent number of bottles used.
Let's talk about user input first.
The get_int function is a function in the CS50 library.
This function ensures that the user will input an integer.
We use the function by passing in a prompt as a string.
If the user follows that prompt but does not enter an integer,
then that user will be re-prompted until they provide us with an integer.
So just simply getting an int might look something
like this, where I call the get_int function
and ask the user to give me a number.
Notice that the top that because get_int is a safety library function,
we must #include its declaration.
Next, you'll notice where I have given the user a prompt.
But even though get_int returns an integer,
how can we actually keep track of the integer inputted by the user?
Here's where I'll introduce a variable.
Let's call it n, where in the variable n I'll
store the result of getting that integer from the user.
Now, take a moment to pause this video and perhaps
write that code for yourself so that you know
how to get an integer from the user.
Once you're ready, let's move on to validating the input from that user.
Because even though we have ensured that we've gotten an integer from the user,
what if they give us a negative integer?
It doesn't really make sense for someone to spend negative 15
minutes in the shower.
Let's take a look at the structure of a while loop.
For a while loop, as long as the given condition of the while loop is true,
then the body of that loop will continue to execute until that condition
evaluates to false.
And then we exit the loop and we continue along in our code.
Now, let's take a look at a close relative of the while loop--
a do-while loop.
A do-while loop is unique in that it executes the body of the loop at least
once.
So within the body of the do-while loop, that thing will happen once,
and then we'll go to a regular while loop.
As long as that condition is true, we'll repeat that process.
The do-while loop is particularly useful when it comes to validating user input.
That's because we know that we want to prompt the user at least once.
So take a look at this snippet of code.
Here I prompt the user for an integer asking them for a number.
Now, I store that number in the integer n, the variable that I declared above.
Now, if n is valid, then my condition will evaluate to false
and so I can progress to the rest of my code.
And I don't need to re prompt them.
But if they give me an invalid number, then my loop
will repeat, prompting them again and again
until they give me a valid number.
When you prompt the user, make sure that you read the problem specification
very carefully and use the exact same string that we ask you for.
That way your program will pass check 50.
OK, now that we've prompted the user and validated their input,
we have the minutes that they spent in the shower.
From there, let's go on to calculating the equivalent number of water bottles.
Now, you might be able to tell immediately
how to convert between minutes spent in the shower to bottles of water used.
But let's start getting into the practice
of detecting these sorts of patterns.
That way when they get more complicated, we've already dealt with them.
So we have one minute spent in the shower being
equivalent to 12 bottles of water, two minutes is equivalent to 24.
Moving on, we have five minutes would be 60 bottles of water.
So for any n minutes spent in the shower, how many bottles of water
are used?
In C, if you'd like to conduct some arithmetic operations
then you have addition, subtraction, multiplication, and division using
these symbols as shown on the slide.
All right, so now we know how to convert between minutes spent in the shower
and bottles used.
The last thing to do is to print that number.
Say I wanted to print the fact that I have 0 dogs.
I would pass in the string, "I have 0 dogs."
and you'll notice the /n there just prints a new line for nice formatting.
Now, as a dog lover, the fact that I have 0 dogs is quite sad
and so I know that that number is going to change.
So instead of hard coding that number n I'm
going to introduce, a place holder for an integer in that string.
So if earlier on in my code I declared an integer n.
Then later on in my print statement, I can print, "I have %i I dogs."
And then pass in the variable that I want to be printed--
in this case n.
And so when I adopted my dog Mayu, n incremented by 1.
And so calling that print statement would print, "I have 1 dog."
You've already done so much work, so let's make
sure that your code passes check 50 by reading the specification
carefully so that you know exactly what to output for those bottles
so that the string matches the spec perfectly.
And with that, you've completed the problem.
My name is [? Zamila, ?] and this was water.