Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics (Retired) Console I/O Formatting Output

Use Console.WriteLine to print the contents of the firstName variable to the screen

string firstName = "Bob"; Console.WriteLine("Your first name is: " + firstName);

CodeChallenge.cs
string firstName = "Bob";
System.Console.WriteLine("The name you entered is: " + firstName);

5 Answers

I've tried several different codes. The first one followed the previous question. Here's my original answer and it still gives me an error.

    Console.Write("Enter your name:  "); 
    string name = Console.ReadLine();
    string firstName = name;
    Console.WriteLine("Your name is : " + firstName);
Steven Parker
Steven Parker
231,248 Points

Well, do one task at a time. And for task one, it doesn't ask for a Write or a WriteLine. Always do only what the challenge asks for. Doing anything extra can prevent the code from passing.

And the two lines in between might work, but why not use the correct variable name the first time and avoid the second assignment?

So what is the answer? I've tried the following:

string firstName = name; Console.WriteLine("Your name is: " + firstName);

or this

Console.Write("Enter your name: "); string name = Console.ReadLine(); string firstName = name; Console.WriteLine("Your name is : " + firstName);

or this

string name = Console.ReadLine();
string firstName = name;
Console.WriteLine("Your name is : " + firstName);

or just this

string firstName = "Bob";
Console.WriteLine("Your name is : " + firstName);

funny thing is on the Workspace is works! The quiz has a bug!

Steven Parker
Steven Parker
231,248 Points

The challenge isn't just about writing code that works .. you need to write code that does the right thing.

Remember, I already reminded you that for task 1 there will be no "Write" and no "WriteLine". You will only need one line of code, which will create a variable and assign it using "ReadLine".

Kirk Friend
Kirk Friend
317 Points

Step 1:

string firstName = Console.ReadLine();

Step 2:

string firstName = Console.ReadLine();
Console.WriteLine(firstName);

Step 3:

string firstName = Console.ReadLine();
Console.WriteLine(firstName + " rocks!");
Steven Parker
Steven Parker
231,248 Points

:warning: FYI: According to a moderator, explicit answers without any explanation are strongly discouraged by Treehouse.

Kirk Friend
Kirk Friend
317 Points

Hi Steven,

I felt your explanation covered the problem nicely. I thought I would provide the original poster with the answer as they explicitly asked for it, in the hope they would not get frustrated or discouraged and give up but instead is able to see their mistake and learn from it.

Thanks for informing me though, I will be careful in my future responses.

Steven Parker
Steven Parker
231,248 Points

I'm not a fan of spoilers, but they are apparently allowed if accompanied by explanations.

Steven Parker
Steven Parker
231,248 Points

I'm wondering if the "View Challenge" button is pointing to the right thing? The code shown here is very different from what that challenge is asking for!

When I select the button, the very first task says, "Declare a variable named firstName and initialize it to the string returned from a call to Console.ReadLine()." Other than the same variable name, the rest of this code doesn't go with these instructions at all.