1 00:00:00,000 --> 00:00:00,470 2 00:00:00,470 --> 00:00:02,640 >> DAVID J. MALAN: Let's implement a program that takes advantage of the 3 00:00:02,640 --> 00:00:06,280 CS50 library, using a function called get int, whose purpose in life is to 4 00:00:06,280 --> 00:00:09,960 do just that, to getting an int, or an integer, from the user. 5 00:00:09,960 --> 00:00:14,350 Well, to use this function we first need to include the CS50 library's 6 00:00:14,350 --> 00:00:17,480 header file, which we can do with the sharp include statement. 7 00:00:17,480 --> 00:00:22,470 >> Let's next, as we often do, include standard io.h so that we have access 8 00:00:22,470 --> 00:00:24,410 to a function like printf. 9 00:00:24,410 --> 00:00:29,450 Let's now declare main, itself, with int main void, open curly brace, and 10 00:00:29,450 --> 00:00:31,780 preemptively close curly brace. 11 00:00:31,780 --> 00:00:35,740 And let's now proceed to prompt the user for two integers, and let's call 12 00:00:35,740 --> 00:00:38,210 them, for the sake of discussion, x and y. 13 00:00:38,210 --> 00:00:42,140 And let's finally add those two values, x and y, together so as to 14 00:00:42,140 --> 00:00:45,060 implement the very simplest of calculators. 15 00:00:45,060 --> 00:00:50,080 >> printf, please give me an int. 16 00:00:50,080 --> 00:00:52,780 And now we need to actually get that int from the user. 17 00:00:52,780 --> 00:00:57,670 To do this, I'm going to declare a variable called x, thereby allocating 18 00:00:57,670 --> 00:01:01,390 some memory in the computer for this variable, x. 19 00:01:01,390 --> 00:01:05,740 And now let me assign, using the assignment operator, the return value, 20 00:01:05,740 --> 00:01:07,800 so to speak, of get int. 21 00:01:07,800 --> 00:01:11,440 In other words, on the right hand side of this expression, let's call get 22 00:01:11,440 --> 00:01:16,630 int, which is a function declared in CS50.h, and allow get int to do the 23 00:01:16,630 --> 00:01:20,490 heavy lifting of getting an int somehow from the user, returning it, 24 00:01:20,490 --> 00:01:24,210 so to speak, and then storing it from the right hand side of this expression 25 00:01:24,210 --> 00:01:26,680 into the left hand side of this expression. 26 00:01:26,680 --> 00:01:29,570 >> Let's next do the same thing, this time getting a value for a 27 00:01:29,570 --> 00:01:31,440 variable called y. 28 00:01:31,440 --> 00:01:36,760 printf, please give me another int. 29 00:01:36,760 --> 00:01:40,220 int y, thereby declaring a new variable, this time called 30 00:01:40,220 --> 00:01:43,660 y, equals get int. 31 00:01:43,660 --> 00:01:47,010 In other words, I can call get int multiple times, and each time it's 32 00:01:47,010 --> 00:01:50,690 going to return whatever integer the user has actually typed. 33 00:01:50,690 --> 00:01:53,920 >> Finally, let's add x and y together. 34 00:01:53,920 --> 00:02:02,620 printf, this sum of %d and %d, so in other words, I'm going to plug in the 35 00:02:02,620 --> 00:02:06,400 values of x and y for those placeholder values. 36 00:02:06,400 --> 00:02:10,720 %d is, well, %d. 37 00:02:10,720 --> 00:02:13,710 In other words, if I add one int to another int, I'm going 38 00:02:13,710 --> 00:02:14,810 to get a third int. 39 00:02:14,810 --> 00:02:18,750 Therefore, I'm going to use a third place holder for an int. 40 00:02:18,750 --> 00:02:22,540 >> Period, backslash n, just to put a new line on the screen so as to move the 41 00:02:22,540 --> 00:02:25,160 cursor down neatly, close quote. 42 00:02:25,160 --> 00:02:28,490 >> Now, printf, in this case, is going to have to take some additional 43 00:02:28,490 --> 00:02:30,200 arguments, three, in fact. 44 00:02:30,200 --> 00:02:33,580 Because in that first argument, I've specified three place holders. 45 00:02:33,580 --> 00:02:36,470 So I'm going to separate these arguments, as always, with commas. 46 00:02:36,470 --> 00:02:40,200 The first such argument is going to be x, the second such argument is going 47 00:02:40,200 --> 00:02:43,950 to be y, and the third such argument is going to be, well, just an 48 00:02:43,950 --> 00:02:46,910 arithmetic expression, x plus y. 49 00:02:46,910 --> 00:02:51,730 I'm going to close my argument list with a parentheses, semicolon, Save my 50 00:02:51,730 --> 00:02:54,140 file, and now compile this program. 51 00:02:54,140 --> 00:02:56,730 >> Make adder Enter. 52 00:02:56,730 --> 00:02:58,300 I'm back at my blinking prompt. 53 00:02:58,300 --> 00:03:02,040 Let's now run it-- dot slash adder Enter. 54 00:03:02,040 --> 00:03:03,250 Please give me an int. 55 00:03:03,250 --> 00:03:04,820 Let's start with 1. 56 00:03:04,820 --> 00:03:06,160 Please give me another int. 57 00:03:06,160 --> 00:03:07,460 Let's go with 2. 58 00:03:07,460 --> 00:03:11,060 And hopefully, the sum of 1 and 2 is 3. 59 00:03:11,060 --> 00:03:12,980