1 00:00:00,000 --> 00:00:00,690 2 00:00:00,690 --> 00:00:02,700 SPEAKER: Let's talk about water. 3 00:00:02,700 --> 00:00:05,550 In this problem, we're going to ask the user how many minutes they 4 00:00:05,550 --> 00:00:06,990 spent in the shower. 5 00:00:06,990 --> 00:00:09,450 From there, we're going to estimate the amount of water 6 00:00:09,450 --> 00:00:12,180 that they used returning it to them using 7 00:00:12,180 --> 00:00:14,850 water bottles as a unit of measurement. 8 00:00:14,850 --> 00:00:18,360 Hopefully, after seeing how many water bottles they used in their shower, 9 00:00:18,360 --> 00:00:21,720 they'll start to conserve a little bit more water. 10 00:00:21,720 --> 00:00:25,260 So let me break this problem down into three todos. 11 00:00:25,260 --> 00:00:30,150 Your first task is to prompt and validate the user for their input. 12 00:00:30,150 --> 00:00:33,810 Second is to calculate the equivalent number of bottles 13 00:00:33,810 --> 00:00:34,920 that their shower used. 14 00:00:34,920 --> 00:00:39,130 And lastly, you want to print that number out. 15 00:00:39,130 --> 00:00:42,210 So your program might look something like this. 16 00:00:42,210 --> 00:00:44,520 After including the appropriate libraries, 17 00:00:44,520 --> 00:00:47,460 your main function will first have a section of code 18 00:00:47,460 --> 00:00:50,730 that prompts the user for the minutes they spent in the shower. 19 00:00:50,730 --> 00:00:54,090 Then you'll have a section of code that calculates and outputs 20 00:00:54,090 --> 00:00:57,730 the equivalent number of bottles used. 21 00:00:57,730 --> 00:01:00,340 Let's talk about user input first. 22 00:01:00,340 --> 00:01:04,830 The get_int function is a function in the CS50 library. 23 00:01:04,830 --> 00:01:09,210 This function ensures that the user will input an integer. 24 00:01:09,210 --> 00:01:13,770 We use the function by passing in a prompt as a string. 25 00:01:13,770 --> 00:01:17,290 If the user follows that prompt but does not enter an integer, 26 00:01:17,290 --> 00:01:22,170 then that user will be re-prompted until they provide us with an integer. 27 00:01:22,170 --> 00:01:25,320 So just simply getting an int might look something 28 00:01:25,320 --> 00:01:28,080 like this, where I call the get_int function 29 00:01:28,080 --> 00:01:31,630 and ask the user to give me a number. 30 00:01:31,630 --> 00:01:35,590 Notice that the top that because get_int is a safety library function, 31 00:01:35,590 --> 00:01:38,910 we must #include its declaration. 32 00:01:38,910 --> 00:01:42,600 Next, you'll notice where I have given the user a prompt. 33 00:01:42,600 --> 00:01:46,350 But even though get_int returns an integer, 34 00:01:46,350 --> 00:01:50,670 how can we actually keep track of the integer inputted by the user? 35 00:01:50,670 --> 00:01:53,440 Here's where I'll introduce a variable. 36 00:01:53,440 --> 00:01:56,790 Let's call it n, where in the variable n I'll 37 00:01:56,790 --> 00:02:00,400 store the result of getting that integer from the user. 38 00:02:00,400 --> 00:02:03,090 Now, take a moment to pause this video and perhaps 39 00:02:03,090 --> 00:02:06,150 write that code for yourself so that you know 40 00:02:06,150 --> 00:02:08,949 how to get an integer from the user. 41 00:02:08,949 --> 00:02:12,870 Once you're ready, let's move on to validating the input from that user. 42 00:02:12,870 --> 00:02:18,060 Because even though we have ensured that we've gotten an integer from the user, 43 00:02:18,060 --> 00:02:20,250 what if they give us a negative integer? 44 00:02:20,250 --> 00:02:23,310 It doesn't really make sense for someone to spend negative 15 45 00:02:23,310 --> 00:02:25,110 minutes in the shower. 46 00:02:25,110 --> 00:02:27,660 Let's take a look at the structure of a while loop. 47 00:02:27,660 --> 00:02:32,710 For a while loop, as long as the given condition of the while loop is true, 48 00:02:32,710 --> 00:02:36,540 then the body of that loop will continue to execute until that condition 49 00:02:36,540 --> 00:02:37,810 evaluates to false. 50 00:02:37,810 --> 00:02:42,130 And then we exit the loop and we continue along in our code. 51 00:02:42,130 --> 00:02:44,910 Now, let's take a look at a close relative of the while loop-- 52 00:02:44,910 --> 00:02:46,530 a do-while loop. 53 00:02:46,530 --> 00:02:51,030 A do-while loop is unique in that it executes the body of the loop at least 54 00:02:51,030 --> 00:02:52,030 once. 55 00:02:52,030 --> 00:02:56,820 So within the body of the do-while loop, that thing will happen once, 56 00:02:56,820 --> 00:03:00,330 and then we'll go to a regular while loop. 57 00:03:00,330 --> 00:03:05,310 As long as that condition is true, we'll repeat that process. 58 00:03:05,310 --> 00:03:09,930 The do-while loop is particularly useful when it comes to validating user input. 59 00:03:09,930 --> 00:03:14,230 That's because we know that we want to prompt the user at least once. 60 00:03:14,230 --> 00:03:17,040 So take a look at this snippet of code. 61 00:03:17,040 --> 00:03:22,050 Here I prompt the user for an integer asking them for a number. 62 00:03:22,050 --> 00:03:27,000 Now, I store that number in the integer n, the variable that I declared above. 63 00:03:27,000 --> 00:03:31,380 Now, if n is valid, then my condition will evaluate to false 64 00:03:31,380 --> 00:03:33,800 and so I can progress to the rest of my code. 65 00:03:33,800 --> 00:03:35,820 And I don't need to re prompt them. 66 00:03:35,820 --> 00:03:39,630 But if they give me an invalid number, then my loop 67 00:03:39,630 --> 00:03:43,140 will repeat, prompting them again and again 68 00:03:43,140 --> 00:03:45,810 until they give me a valid number. 69 00:03:45,810 --> 00:03:50,190 When you prompt the user, make sure that you read the problem specification 70 00:03:50,190 --> 00:03:54,690 very carefully and use the exact same string that we ask you for. 71 00:03:54,690 --> 00:03:58,650 That way your program will pass check 50. 72 00:03:58,650 --> 00:04:02,610 OK, now that we've prompted the user and validated their input, 73 00:04:02,610 --> 00:04:05,970 we have the minutes that they spent in the shower. 74 00:04:05,970 --> 00:04:11,630 From there, let's go on to calculating the equivalent number of water bottles. 75 00:04:11,630 --> 00:04:13,380 Now, you might be able to tell immediately 76 00:04:13,380 --> 00:04:17,640 how to convert between minutes spent in the shower to bottles of water used. 77 00:04:17,640 --> 00:04:19,589 But let's start getting into the practice 78 00:04:19,589 --> 00:04:21,959 of detecting these sorts of patterns. 79 00:04:21,959 --> 00:04:26,730 That way when they get more complicated, we've already dealt with them. 80 00:04:26,730 --> 00:04:29,940 So we have one minute spent in the shower being 81 00:04:29,940 --> 00:04:34,920 equivalent to 12 bottles of water, two minutes is equivalent to 24. 82 00:04:34,920 --> 00:04:38,820 Moving on, we have five minutes would be 60 bottles of water. 83 00:04:38,820 --> 00:04:44,430 So for any n minutes spent in the shower, how many bottles of water 84 00:04:44,430 --> 00:04:46,590 are used? 85 00:04:46,590 --> 00:04:50,460 In C, if you'd like to conduct some arithmetic operations 86 00:04:50,460 --> 00:04:54,690 then you have addition, subtraction, multiplication, and division using 87 00:04:54,690 --> 00:04:57,720 these symbols as shown on the slide. 88 00:04:57,720 --> 00:05:03,180 All right, so now we know how to convert between minutes spent in the shower 89 00:05:03,180 --> 00:05:04,350 and bottles used. 90 00:05:04,350 --> 00:05:09,060 The last thing to do is to print that number. 91 00:05:09,060 --> 00:05:12,510 Say I wanted to print the fact that I have 0 dogs. 92 00:05:12,510 --> 00:05:15,450 I would pass in the string, "I have 0 dogs." 93 00:05:15,450 --> 00:05:20,820 and you'll notice the /n there just prints a new line for nice formatting. 94 00:05:20,820 --> 00:05:24,130 Now, as a dog lover, the fact that I have 0 dogs is quite sad 95 00:05:24,130 --> 00:05:27,070 and so I know that that number is going to change. 96 00:05:27,070 --> 00:05:30,540 So instead of hard coding that number n I'm 97 00:05:30,540 --> 00:05:35,560 going to introduce, a place holder for an integer in that string. 98 00:05:35,560 --> 00:05:39,540 So if earlier on in my code I declared an integer n. 99 00:05:39,540 --> 00:05:47,640 Then later on in my print statement, I can print, "I have %i I dogs." 100 00:05:47,640 --> 00:05:51,960 And then pass in the variable that I want to be printed-- 101 00:05:51,960 --> 00:05:53,840 in this case n. 102 00:05:53,840 --> 00:05:58,680 And so when I adopted my dog Mayu, n incremented by 1. 103 00:05:58,680 --> 00:06:04,170 And so calling that print statement would print, "I have 1 dog." 104 00:06:04,170 --> 00:06:06,720 You've already done so much work, so let's make 105 00:06:06,720 --> 00:06:11,130 sure that your code passes check 50 by reading the specification 106 00:06:11,130 --> 00:06:15,660 carefully so that you know exactly what to output for those bottles 107 00:06:15,660 --> 00:06:19,260 so that the string matches the spec perfectly. 108 00:06:19,260 --> 00:06:21,850 And with that, you've completed the problem. 109 00:06:21,850 --> 00:06:26,030 My name is [? Zamila, ?] and this was water. 110 00:06:26,030 --> 00:06:27,377