data_type
data_type

Reputation: 27

I'm having an issue with User Input

So, I'm a new programmer with no experience and I'm learning C# with something called SoloLearn. It's teaching me very well, but when I got to something called User Input, it went wrong. The problem is when I open the Console Application, nothing shows up. Here is my code https://i.sstatic.net/SJD4x.png. Keep in mind, everything with the program was working fine until I added the 'age' part of the code, then that's when everything went blank on Console when I open it in Visual Studio. Please help, thanks!

Upvotes: 0

Views: 1665

Answers (3)

Sach
Sach

Reputation: 10393

Most answer here are correct in the sense it will fix the immediate problem, but the underlying question here, imo, is that the OP doesn't have a clear understanding of basic programming. Which is fine, they've stated that they are a beginner, so let me take a crack at this in a little different angle.

First I'll post the code here. @IcyJake, in the future you should post code here instead of linking to images of code.

static void Main(string[] args)
{
    string yourName;
    int yourAge = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("What's your name?");
    yourName = Console.ReadLine();

    Console.WriteLine("Hello, {0}\n", yourName);

    Console.WriteLine("How old are you?");

    Console.WriteLine("Oh, {1} is a cool number");

    Console.ReadLine();
}

The objective of the program is obviously to ask the user for their name and age, and write that information back to the Console. So what should you do in order to implement that behavior? It will be something like the following.

  1. Ask the user to input age.
  2. Get the user input age into a variable, and hold it for future use.
  3. Ask the user to input name.
  4. Get the user input name into a variable, and hold it for future use.
  5. Use the two saved values to output a message.

First, please try to understand the usage of the method Console.ReadLine(). What it does is, when the user types something in the Console and presses ENTER key, it grabs whatever was typed and returns that value. So if you were to write something like this in your program:

string input = Console.ReadLine();

That will take whatever the user typed and save it into the string variable named input. Conversely, Console.WriteLine() displays something on the Console.

Now, let's take your program and see where it's different form the order of things I've mentioned above.

First, you take an input from the Console and converts it into an integer (ALL Console input is in the string format, and to use it as a number you need to convert it), and saves in the yourAge variable. This is why your program appears to go blank as you put it. The Console is simply waiting for you to type something and hit ENTER. If you simply typed a number and hit enter, the program will proceed to the next stage. Then you ask for the name, and saves it into yourName variable. Then you ask for the age, but does not get an input from the user. Then you print the final message.

So the order in your program (in terms of the stuff I mentioned above that must be done), is this:

2.

3.

4.

1.

5.

As you can see, clearly there's a problem. To fix it, what you need to do is rearrange your code in the order I mentioned. That is;

  1. Use Console.WriteLine() and print a message asking for age.
  2. Use Console.ReadLine() to grab that value and save into yourAge variable.
    • Here, you need to do what is generally called 'error handling'. That is, you need to protect against the user entering a non-numeric value when asked for age. Please refer to the link provided by @Rafa in a different answer here.
  3. Use Console.WriteLine() and print a message asking for name.
  4. Use Console.ReadLine() to grab that value and save into the yourName variable.
  5. Use Console.WriteLine() to print back the input values.

I'm deliberately not giving you the code that works as you are a new programmer who's learning, and it's best to try yourself and figure out for yourself what you're doing.


Additional Info:

The usage of curly brackets in Console.WriteLine() is to print variable content to the Console. Let's say you have an integer variable named myVar1 in your program, of which the value is 5.

So if you were to write

Console.WriteLine("Value = {0}", myVar1);

It will replace the {0} with the value myVar1 is holding. Thus your output would look like:

Value = 5

Now what if you have two variables in your program, say myVar1 and myVar2, and you want both of them to be written in the same line? Say, you want the output to look like this: (Let's assume value of myVar1 is 5 and value of myVar2 is 10.)

Values = 5 and 10

What do we do now? Easy, you write your Console.WriteLine() with two curly brackets like so:

Console.WriteLine("Values = {0} and {1}", myVar1, myVar2);

What this does is, replace {0} with the contents of myVar1, and replace {1} with the contents of myVar2. That is, first variable after the comma goes in place of {0} and the second variable after the comma goes in place of {1}. So your output will look like the above expected output.

You can experiment and understand the behavior. For example, if you want to interchange the two values, you could do this:

Console.WriteLine("Values = {0} and {1}", myVar2, myVar1);

Now, your output will look like this:

Values = 10 and 5

Hope this helps you get going. Please find more tutorials and keep programming and you will get good at it. And also debugging using break-points is a great way to figure out when you encounter problems.

Finall, regarding Stack Overflow. Usually when you post a question, copy-paste code here directly instead of uploading images. Also, people here usually don't like to write code for you (with good reason), but are glad to help you. So give it a try, and ask only if you can't find the answer here in SO or generally after Googling. And read this guide on How to create a Minimal, Complete, and Verifiable example which will help you a lot on not getting downvoted.

Good luck!

Upvotes: 2

Rafa
Rafa

Reputation: 1

https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

int inputNumber;
bool isNumber = Int32.TryParse(value, out inputNumber);
if(isNumber){ Console.WriteLine $"Your age is {inputNumber}"}
else { Console.WriteLine $"Fail Cast!!!"}

Upvotes: -1

Olexiy Sadovnikov
Olexiy Sadovnikov

Reputation: 1091

Put

int yourAge = Convert.ToInt32(Console.ReadLine());

after

Console.WriteLine("How old are you?");

Upvotes: 0

Related Questions