Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Let's update our Ask method to return a value.
- Return
Console.ReadLine
fromAsk
- Update return type to
string
- Get rid of
answer
variable - Use
return
instead
- Update return type to
static string Ask(string question)
{
Console.WriteLine(question);
return Console.ReadLine();
}
static void Main()
{
Console.WriteLine("Welcome to the cat food store!");
string entry = Ask("How many cans are you ordering?");
Console.WriteLine(entry);
}
- Right now the user types their answer on a separate line from the question.
- That's because we print the question using
Console.WriteLine
, which prints a string and then skips to the next line. - It might look a little neater if the typed answer appeared on the same line as the question.
- We can do this with the
Console.Write
method, which prints a string and then stays on the same line:Console.Write(question);
- That's because we print the question using
If we go to the terminal and run dotnet run
, everything works!
We've completed a second feature for our program! We ask the user the quantity of cans they want, call the gets
method to get their keyboard entry, and store the return value from gets
in a variable.
Cat Food Store Features
Display welcome messageAsk for quantity- Calculate total
- Discount for large orders
There's just one minor issue with the program. When we ask the user how many cans they want to buy, our question runs right up against the place their keyboard input is shown. That looks a little less than professional. To fix this, we're going to need to learn more about strings.
Just like most other programming languages, C# uses strings to store text. We'll learn all about them in the next stage. See you there!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up