1 00:00:00,000 --> 00:00:00,330 2 00:00:00,330 --> 00:00:03,230 >> SPEAKER: Let's implement a program that prompts the user for an integer 3 00:00:03,230 --> 00:00:05,850 but continues prompting them for another integer until 4 00:00:05,850 --> 00:00:07,590 that integer is positive. 5 00:00:07,590 --> 00:00:16,760 Well, let's write this, include cs50.h, include standard io dot h, int 6 00:00:16,760 --> 00:00:18,010 main[void]. 7 00:00:18,010 --> 00:00:20,000 8 00:00:20,000 --> 00:00:23,200 Now let's actually begin to prompt the user for this integer while keeping in 9 00:00:23,200 --> 00:00:25,570 mind that we want to reprompt them until that 10 00:00:25,570 --> 00:00:26,860 number is actually positive. 11 00:00:26,860 --> 00:00:30,160 >> So I'll want some kind of looping construct, but a loop that executes at 12 00:00:30,160 --> 00:00:31,200 least once. 13 00:00:31,200 --> 00:00:34,810 And recall the do-while loop allows us to do exactly that. 14 00:00:34,810 --> 00:00:36,140 Let's code this up. 15 00:00:36,140 --> 00:00:39,210 >> Let's first declare an int-- we'll call it n-- in which to store the 16 00:00:39,210 --> 00:00:41,030 user's integer. 17 00:00:41,030 --> 00:00:44,250 Let's now do the following. 18 00:00:44,250 --> 00:00:51,292 Printf, please give me a positive int. 19 00:00:51,292 --> 00:00:54,470 Let's now get that int using the CS50 library's GetInt 20 00:00:54,470 --> 00:00:59,110 function, n gets GetInt. 21 00:00:59,110 --> 00:01:02,610 And now we're done doing something, but we're going to want to do this 22 00:01:02,610 --> 00:01:04,580 while n is-- 23 00:01:04,580 --> 00:01:05,480 what?-- 24 00:01:05,480 --> 00:01:09,370 less than 1, if we indeed want a positive integer. 25 00:01:09,370 --> 00:01:11,670 >> Let's go ahead now and print out. 26 00:01:11,670 --> 00:01:17,590 If all is well, thanks for the positive int, 27 00:01:17,590 --> 00:01:19,360 semicolon, save the file. 28 00:01:19,360 --> 00:01:21,480 Let's now compile and run. 29 00:01:21,480 --> 00:01:25,550 >> Make positive dot slash positive. 30 00:01:25,550 --> 00:01:27,470 And now I'm being prompted for a positive int. 31 00:01:27,470 --> 00:01:29,630 Let me cooperate with 1. 32 00:01:29,630 --> 00:01:31,330 Thanks for the positive int. 33 00:01:31,330 --> 00:01:33,650 >> Let's now rerun this program and not cooperate. 34 00:01:33,650 --> 00:01:35,260 Please give me a positive int. 35 00:01:35,260 --> 00:01:37,300 How about negative 1? 36 00:01:37,300 --> 00:01:37,910 Interesting. 37 00:01:37,910 --> 00:01:40,460 The program is already reprompted me for an int. 38 00:01:40,460 --> 00:01:43,170 >> Let's be slightly more difficult with 0. 39 00:01:43,170 --> 00:01:46,310 Still not a positive int, so let's cooperate and this time 40 00:01:46,310 --> 00:01:48,280 provide, again, 1. 41 00:01:48,280 --> 00:01:49,990 Thanks for the positive int. 42 00:01:49,990 --> 00:01:54,300 >> Now it's important to note that I declared my variable n on line 6, 43 00:01:54,300 --> 00:01:56,510 outside of the do-while loop. 44 00:01:56,510 --> 00:02:01,030 Had I instead declared n inside of my do-while loop, specifically on, say, 45 00:02:01,030 --> 00:02:06,230 line 10, where I also get the int from the user, that int n would have only 46 00:02:06,230 --> 00:02:09,904 existed within the confines of the curly braces that are currently on 47 00:02:09,904 --> 00:02:12,320 line 8 and on line 11. 48 00:02:12,320 --> 00:02:16,570 >> In other words, n would be scoped to the inside of that do-while loop. 49 00:02:16,570 --> 00:02:20,390 But the problem is that we also want to use n in line 12 50 00:02:20,390 --> 00:02:21,900 inside of the condition. 51 00:02:21,900 --> 00:02:26,120 So even though it doesn't look so pretty, it's very much deliberate that 52 00:02:26,120 --> 00:02:28,210 n is declared outside this do-while loop. 53 00:02:28,210 --> 00:02:29,838