BRIAN: Hello, world.
My name is Brian, and I'll be your walk-through lead in CS50.
For each problem, I'll go through what's required,
as well as giving you a high level overview of how you might
solve each of the course's problems.
And I'll give you some hints along the way.
So let's begin with Hello.
Inside of hello.c, right now, we have this code,
which is some code inside of a main function that prints out "Hello, world"
to the screen.
How can you actually run this code?
Well, if you go to your terminal and run make_hello,
this will compile your program.
Once you've compiled your program, you can run your program by running
./hello, at which point you should see the words "Hello, world"
printed to the screen.
Your task in Hello is to modify this program to make it more interactive.
What we're going to have your program do instead is, when you run ./hello,
the program should prompt by asking, "What is your name?"
The user will then type in their name, like Brian, for example,
and the program will say "Hello, Brian."
So what do you have to do?
You'll, first, want to prompt the user for their name by asking for their name
and letting them type it in, and then, you'll
want to say hello to that user, giving their name in your printout statement.
So how might you actually do that?
Well, to get input from the user, you can
take advantage of the get_string function, defined
in the CS50 library in CS50.h.
To use get_string, you'll write get_string, and get_string
takes an argument, inside of parentheses,
which will be the prompt, what's displayed to the user
before they have to type in some_string, for example.
So our prompt, in this case, can be "What
is your name," followed by that backslash n character,
which you might recall represents a new line.
This line needs to end with a semicolon, and we
want to take the result of get_string and save it inside of a variable.
And we're going to save it inside of a variable called
name whose type is string.
So a line like this will prompt the user by saying, "What is your name?"
The user will type in their name, and when they press Return,
their name will be stored inside of that variable called name.
Once the user has typed in their name, the next thing we need to do
is actually say "Hello" to that user.
How might we do that?
Well, we already have code that says "Hello,
world," that looks something like this, printf prints
out some string to the terminal screen, and the string
"Hello, world" is what ultimately gets printed.
But instead of printing "Hello, world," we instead
want to print "Hello, Brian," for example, or "Hello"
to whatever user's name was actually typed in.
So what we might do here, instead, is use a placeholder.
Here, instead of "Hello, world," we're saying "Hello, %s,"
where %s is a place holder for a string, meaning we're going to plug in some
string into this point of the string.
Now, what string are we going to plug in?
We're going to plug in the string "name,"
so we provide the variable "name" as an additional argument to printf.
This whole line, now, is going to say "Hello comma"
and then plugging in whatever the value of the string "name" happens to be.
Once you do that, you should be able to test your program by running it.
First, compile it by running make_hello, then after it's compiled, run ./hello,
and then, that should prompt you to type in your name.
Type in your name, and then your program should say "Hello" to you.
My name is Brian, and this was Hello.