Entry Level4 Who is whoConsole readline

Two man talking in the kitchen

What is Wrong With This World?

I was about to make coffee when Tom came into the kitchen.
"Hi Teo, how are you?" he said, leaning against the counter.

I had seen that body language before, and was pretty sure this wasn't small talk. "Is this a 'what is happening in your life?' kind of how are you, or the 'just answer fine' how are you?" I asked.
"You're getting to know me," he said, "This is the 'what's going on in your life' one. I don't ask questions just for the sake of asking."

"Well," I began, "In the last few weeks, I was dragged forward through time by a former acquaintance, I'm fighting in a war that I don't fully understand, I don't know if I'll ever see my family again, and..." I paused for a breath before I got to the worst. I wanted my last point to come through loud and clear. "Do you know what pisses me off most of all?"

"What's that, Teo?"

"Milk, Tom! You have no Goddamn milk in the future! How am I supposed to drink coffee? Without milk? What is wrong with this world? I don't understand..."

"Sorry to hear all that, Teo," Tom interrupted. "Any chance we can rewind this conversation and change that 'how are you' to the 'just answer fine' one?"
"That's what I'm talking about!" I continued. "No one cares about my feelings here. Everything is predefined; I have no influence on what's going to happen. The same as programs that I write..." Now that I had aired my grievances, my mind began to settle down and float back to coding.

Wait a second, I thought. Predefined... as a matter of fact, my programs are predefined, and that's what's wrong with them. When I get up in the morning, I don't know the air temperature outside. Then I check it, and if the temperature is low, I wear a heavy jacket; otherwise, a lighter coat. But in the program I wrote, the temperature is predefined and ALWAYS -10 degrees. It works the same way in winter, summer, or spring. Why would you write a program that tells you what to wear if it always says the same thing?

"Tom," I asked, hoping he wasn't too put off at this point, "I need a way to provide some outside information to my program. I want to check the real temperature and have my program decide what I should wear based on that. Can you help me?""
"You are asking difficult questions, Teo. If it will make you feel more comfortable here, then yes, I know one command that will help. But let's keep it a secret from Ritchie, ok?"
I wasn't sure why this had to be kept from the leader, but I desperately wanted to learn more about coding. "Sure!" I said.

Talk is cheap meme

Talk to Your Program

Tom was clearly more comfortable with computer code than conversation. "There are many ways to provide info to your program, Teo. You can read info from a file stored on your computer or from any device connected to your computer or you can connect to the Internet and read info from there. You can also read data directly from the console .

"The same console that I'm writing to when I use a Console.WriteLine(...) command?"
"Yes, Teo. Exactly. I only know about Console input; for the more complicated methods involving files and the Internet, you'll have to ask someone else. However, those methods will be easier to understand if you learn the simpler Console input first."

"The console is a text area that we treat as a sequence of strings (or lines). When using the Console.WriteLine(...) command, you add one new line to the console. To read a line from the console, you use the Console.ReadLine() command. Are you following me?"

"Not really," I replied. " What lines does the Console.ReadLine() command read? For example, what if I have written two lines to the console?"
"Oh, you are funny, Teo. It doesn't work like that. There is no point in reading from the console what you have just written there from your code. You already know what it says! When you use the Console.ReadLine() command, the program execution stops and waits for the user to input data to the console. When the user finishes their input and presses Enter, Console.ReadLine() returns a string containing that data. Here's an example."

Console.WriteLine("Input your name, please.");
string name = Console.ReadLine();
Console.WriteLine($"Hi, {name}!");

"This is a basic program with both Console.WriteLine() and Console.ReadLine() commands. First, you tell the user what you want them to input. Then you read it in and do something with that data. Does it make more sense now?
"Aha, I see now." I said.

In the terminal where you solve tasks, there is an area called the Console input. We will use that to provide input to your program. When your code runs a Console.ReadLine() command, it will read one string from the Console Input window. Try it!

Console screenn

"Thanks Tom, those examples really brought it together!" I said, grateful that he hadn't left after my earlier ranting. "Now I can build programs that take in data from the outside. Is there anything else neat about the Console input you can show me?"
"Well," he said, "If you want to use the user's input as an integer number, you can manipulate their input string with int .Parse(someString), which is a function that takes a string as an input and returns an int (integer). Here is an example."

Console.WriteLine("Input your age, please.");
string stringAge = Console.ReadLine();
int age = int.Parse(stringAge);
... // Work with age as integer

And sometimes you can meet the short version of it:

int age = int.Parse(Console.ReadLine()); // Reads a string and converts it to int

"What does 'parse' mean?" I asked.
"It means to analyze a string of characters to associate groups of characters with some units like integer numbers, decimal numbers, etc. Let's say, for example, that our user inputs their age as 25. In the (string) variable stringAge, we have a value of '25', but it's just stored as two characters, similar to 'ab' or '7s'. The string '25' even takes up a different amount of memory than the integer '25'. We can't do math on characters, so if we want to work with '25' as a number for comparison, addition, and so on, we need to convert it to an integer. To do that, we use int.Parse(stringAge) to essentially convert the string to an integer."

Here is a problem for you to solve:

To speed up world salvation, in all future tasks, we are going to omit the part where you explicitly ask a user to input a value unless otherwise specified (for example, "Input a number" or "What is your name"). Your program should read an input value immediately, without first printing a prompt.

"Now Teo, with such a powerful tool in your hands, you should practice A LOT. Info about Console.ReadLine() is top secret here in the base. I'll share some practice tasks with you, but don't tell anyone about our lesson today. Good luck!"
Tom sent me a file with tasks. Later that day, I closed the door to my room, sat on the floor with my laptop, and opened the file. Here is what I found inside:

Study c sharp: top secret

The person who solves all the tasks from this document can open the door to the underground storage of the resistance base. For root users only.
To be sure that you are the leader of the resistance base, we protected the key to the underground storage. You will be given 7 tasks; if you solve them, the secret key to the storage room will be revealed. These tasks contain advanced operations of the C# language that only base leaders have access to. FOR YOUR EYES ONLY. Never tell anyone what you have seen in storage. Good luck!

C# learning: programming man

As soon as I finished the last task, the promised message appeared on my screen with the secret code: 145 . Great, I thought. Where to next?

Study programming: what next