[SOLVED] [C#] Simple question regarding user input

Cookieman

BSOD Kernel Dump Senior Analyst
Joined
Jun 21, 2012
Posts
124
Location
Lincoln (UK)
Hi all

After starting eagerly to learn a little bit of C# as a hobby a year ago I managed to get distracted by a project that I got involved with which took me away from my programming goals. Now, (after many other interruptions!) I finally have my spare time back and I am just as eager to continue my learning.

Here is a very basic question regarding input though a console window. When I ask a user for the input of a value, the value is entered on the line beneath, for example, the following code...

Code:
Console.WriteLine("Input the subject number : ");
            subject = Convert.ToDecimal(Console.ReadLine());

Would look in the console like:

Input the subject number :

(input value would show here)

While what I am wanting is :
Input the subject number : (input value would show here)

I know in C++ it would be a simple case of using the following to gain the input on the same line,
Code:
cout << "Input subject number : ";
    cin >> subject;

where as if it was split it over two lines I would simply include << endl; and the end of line 1 (or use the escape sequence \n)

Cheers - Steve
 
Last edited:
Hello!

Console.ReadLine() simply reads the input from wherever the cursor position currently is. The problem is not here as it is not the culprit of the extra line break.

In actual fact, the problem lies in Console.WriteLine(). This function outputs the given text and adds a line break. That is where the line break is coming from. To avoid this, use Console.Write().

:)

Richard
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top