5

I would like to ask the user for his/her weight; for example;

Console.Write("Please enter your weight: {I want it to be inserted here} kg");

Here the input will be inserted after the kg, although I want it to be just before "kg" so that the user may know that a kg value is being expected, and that further details are not necessary (such as kg/lbs..)

Thanks in advance.

oneNiceFriend
  • 6,691
  • 7
  • 29
  • 39
Mohamed
  • 71
  • 2
  • 2
    I guess this is not possible. You could write "Please enter your weight (in kg):" to make it clear in which unit he has to enter his weight. – Mighty Badaboom Feb 28 '17 at 13:07
  • 2
    The Console input doesn't do pretty formats, just do a ReadLine() and process the string. – H H Feb 28 '17 at 13:15

4 Answers4

3

You will have to read every single key the user inputs and then set the cursorposition to the location you want the users weight to be displayed. Afterwards you write it to the console.

static void Main(string[] args)
{
    string prompt = "Please enter your weight: ";
    Console.Write(prompt + " kg");
    ConsoleKeyInfo keyInfo;
    string weightInput = string.Empty;
    while ((keyInfo = Console.ReadKey()).Key != ConsoleKey.Enter)
    {
        //set position of the cursor to the point where the user inputs wight
        Console.SetCursorPosition(prompt.Length, 0);
        //if a wrong value is entered the user can remove it
        if (keyInfo.Key.Equals(ConsoleKey.Backspace) && weightInput.Length > 0)
        {
            weightInput = weightInput.Substring(0, weightInput.Length - 1);
        }
        else
        {
            //append typed char to the input before writing it
            weightInput += keyInfo.KeyChar.ToString();
        }
        Console.Write(weightInput + " kg ");
    }

    //process weightInput here
}
lunardoggo
  • 120
  • 1
  • 3
  • 10
  • 2
    I would prefer this solution, because you can check every input for a digit. So it will not be possible to write `abc kg`. If you wonder for for the space after kg, it's for clearing the last character, other wise it would be `70 kgggg` after a while – Matt Feb 28 '17 at 14:45
2

I've found a simple answer:

Console.Write("enter weight =   kg");
Console.SetCursorPosition(0, 8);
metric.weightKgs = byte.Parse(Console.ReadLine());

It all comes down to cursor positioning and playing around with it finding the exact location.

Mohamed
  • 71
  • 2
1

You need to write to the console your question (I would do it like this)

Console.WriteLine("Please enter your weight (kg):");

Then wait for the value to come back.

This will wait for the user

string userInput = Console.ReadLine();

Using a dollar sign on a string allows for string interpolation. Depending on your version of C#, this may not work.

Console.WriteLine($"Your weight is {userInput}kg.");
Colin
  • 624
  • 8
  • 27
  • If that doesn't work because of the string interpolation using the dollar sign, just do the following: Console.Write("Your weight is " + userInput + "kg"); I would also recommend Console.WriteLine instead of Console.Write for this particular instance. https://msdn.microsoft.com/en-us/library/zdf6yhx5(v=vs.110).aspx – Colin Feb 28 '17 at 13:13
  • Be careful with maximelian 1986's answer. you can't store the result of Console.ReadLine in an integer. – Colin Feb 28 '17 at 13:17
-1
int weight = Console.ReadLine();
if (weight != null) 
Console.WriteLine(string.format("Please enter your weight: {1} kg", weight));

This is unchecked code. But should be something like that.

maximelian1986
  • 2,308
  • 1
  • 18
  • 32
  • 1
    I don't understand how an integer can be null. And in the string.Format, it must be {0} here, not {1}... – Kzryzstof Feb 28 '17 at 14:09