1 00:00:00,000 --> 00:00:00,360 2 00:00:00,360 --> 00:00:01,240 BRIAN: Hello, world. 3 00:00:01,240 --> 00:00:04,410 My name is Brian, and I'll be your walk-through lead in CS50. 4 00:00:04,410 --> 00:00:07,050 For each problem, I'll go through what's required, 5 00:00:07,050 --> 00:00:09,780 as well as giving you a high level overview of how you might 6 00:00:09,780 --> 00:00:11,850 solve each of the course's problems. 7 00:00:11,850 --> 00:00:13,840 And I'll give you some hints along the way. 8 00:00:13,840 --> 00:00:16,230 So let's begin with Hello. 9 00:00:16,230 --> 00:00:19,230 Inside of hello.c, right now, we have this code, 10 00:00:19,230 --> 00:00:22,590 which is some code inside of a main function that prints out "Hello, world" 11 00:00:22,590 --> 00:00:23,850 to the screen. 12 00:00:23,850 --> 00:00:25,770 How can you actually run this code? 13 00:00:25,770 --> 00:00:28,980 Well, if you go to your terminal and run make_hello, 14 00:00:28,980 --> 00:00:31,350 this will compile your program. 15 00:00:31,350 --> 00:00:34,320 Once you've compiled your program, you can run your program by running 16 00:00:34,320 --> 00:00:37,860 ./hello, at which point you should see the words "Hello, world" 17 00:00:37,860 --> 00:00:39,570 printed to the screen. 18 00:00:39,570 --> 00:00:44,460 Your task in Hello is to modify this program to make it more interactive. 19 00:00:44,460 --> 00:00:48,120 What we're going to have your program do instead is, when you run ./hello, 20 00:00:48,120 --> 00:00:51,180 the program should prompt by asking, "What is your name?" 21 00:00:51,180 --> 00:00:54,210 The user will then type in their name, like Brian, for example, 22 00:00:54,210 --> 00:00:56,610 and the program will say "Hello, Brian." 23 00:00:56,610 --> 00:00:58,320 So what do you have to do? 24 00:00:58,320 --> 00:01:01,680 You'll, first, want to prompt the user for their name by asking for their name 25 00:01:01,680 --> 00:01:03,720 and letting them type it in, and then, you'll 26 00:01:03,720 --> 00:01:08,560 want to say hello to that user, giving their name in your printout statement. 27 00:01:08,560 --> 00:01:10,270 So how might you actually do that? 28 00:01:10,270 --> 00:01:12,240 Well, to get input from the user, you can 29 00:01:12,240 --> 00:01:15,030 take advantage of the get_string function, defined 30 00:01:15,030 --> 00:01:17,700 in the CS50 library in CS50.h. 31 00:01:17,700 --> 00:01:21,180 To use get_string, you'll write get_string, and get_string 32 00:01:21,180 --> 00:01:24,000 takes an argument, inside of parentheses, 33 00:01:24,000 --> 00:01:26,610 which will be the prompt, what's displayed to the user 34 00:01:26,610 --> 00:01:29,580 before they have to type in some_string, for example. 35 00:01:29,580 --> 00:01:31,500 So our prompt, in this case, can be "What 36 00:01:31,500 --> 00:01:34,650 is your name," followed by that backslash n character, 37 00:01:34,650 --> 00:01:37,140 which you might recall represents a new line. 38 00:01:37,140 --> 00:01:39,300 This line needs to end with a semicolon, and we 39 00:01:39,300 --> 00:01:43,530 want to take the result of get_string and save it inside of a variable. 40 00:01:43,530 --> 00:01:45,780 And we're going to save it inside of a variable called 41 00:01:45,780 --> 00:01:48,520 name whose type is string. 42 00:01:48,520 --> 00:01:51,750 So a line like this will prompt the user by saying, "What is your name?" 43 00:01:51,750 --> 00:01:54,660 The user will type in their name, and when they press Return, 44 00:01:54,660 --> 00:01:58,588 their name will be stored inside of that variable called name. 45 00:01:58,588 --> 00:02:01,380 Once the user has typed in their name, the next thing we need to do 46 00:02:01,380 --> 00:02:04,050 is actually say "Hello" to that user. 47 00:02:04,050 --> 00:02:05,307 How might we do that? 48 00:02:05,307 --> 00:02:07,140 Well, we already have code that says "Hello, 49 00:02:07,140 --> 00:02:10,080 world," that looks something like this, printf prints 50 00:02:10,080 --> 00:02:13,200 out some string to the terminal screen, and the string 51 00:02:13,200 --> 00:02:15,810 "Hello, world" is what ultimately gets printed. 52 00:02:15,810 --> 00:02:17,940 But instead of printing "Hello, world," we instead 53 00:02:17,940 --> 00:02:20,490 want to print "Hello, Brian," for example, or "Hello" 54 00:02:20,490 --> 00:02:23,740 to whatever user's name was actually typed in. 55 00:02:23,740 --> 00:02:26,880 So what we might do here, instead, is use a placeholder. 56 00:02:26,880 --> 00:02:30,560 Here, instead of "Hello, world," we're saying "Hello, %s," 57 00:02:30,560 --> 00:02:35,280 where %s is a place holder for a string, meaning we're going to plug in some 58 00:02:35,280 --> 00:02:38,140 string into this point of the string. 59 00:02:38,140 --> 00:02:40,020 Now, what string are we going to plug in? 60 00:02:40,020 --> 00:02:42,150 We're going to plug in the string "name," 61 00:02:42,150 --> 00:02:45,930 so we provide the variable "name" as an additional argument to printf. 62 00:02:45,930 --> 00:02:48,510 This whole line, now, is going to say "Hello comma" 63 00:02:48,510 --> 00:02:53,250 and then plugging in whatever the value of the string "name" happens to be. 64 00:02:53,250 --> 00:02:56,550 Once you do that, you should be able to test your program by running it. 65 00:02:56,550 --> 00:03:02,040 First, compile it by running make_hello, then after it's compiled, run ./hello, 66 00:03:02,040 --> 00:03:04,320 and then, that should prompt you to type in your name. 67 00:03:04,320 --> 00:03:08,200 Type in your name, and then your program should say "Hello" to you. 68 00:03:08,200 --> 00:03:11,630 My name is Brian, and this was Hello. 69 00:03:11,630 --> 00:03:12,816