How to take user input in the same line?
Asked Answered
X

4

10

I'm bigener in C# programming So, I was just wondering about how to take user input in the same line? this is my code and also I want to print the output in the same line

using System;

namespace Wa2
{
  class BodyMassCalculation
  {
    public static void Main (string[] args)
    {
       Console.WriteLine ("BMI Calculator\n");

       double weight;
       Console.WriteLine ("Enter your weight in kilograms: ");
       weight = Convert.ToInt16(Console.ReadLine());

       double height;
       Console.WriteLine ("Enter your height in centimetres: ");
       height = Convert.ToInt16(Console.ReadLine());

       double meter;
       meter = height / 100;

       Double BMI;
       BMI = (weight) / (meter*meter);
       Console.WriteLine ("Your BMI is " , BMI);
       Console.WriteLine(BMI.ToString("00.00"));
    }
  }
}
Xenocryst answered 23/9, 2012 at 21:11 Comment(0)
Z
18

Try this:

Console.Write("Enter your input here: ");
string userinput = Console.ReadLine();

Just change Console.WriteLine to Console.Write.

Zaria answered 23/9, 2012 at 21:13 Comment(0)
M
2

Use Console.Write() instead of Console.WriteLine().

I think that's what you mean anyway, the question isn't very clear.

Merchantman answered 23/9, 2012 at 21:14 Comment(0)
I
0

I think you're asking if it's possible to read both height and weight at the same time:

// C equivalent
printf ("Enter height (cm) and weight (kg): ");
scanf ("%d %d\n", &h, &w);

Yes, there are several alternatives.

Arguably the easiest is use Console.ReadLine() (like you're doing) and parse the string.

You can also try multiple "Console.Read()" (one for each argument).

Innerdirected answered 23/9, 2012 at 21:17 Comment(0)
T
0

If you mean to take the input value on the same line or (different values on the same line) you can use the split() Method.

string[] inp = Console.ReadLine().Split(' ');
int a = int.Parse(inp[0]);
char c = char.Parse(inp[1]);
int b = int.Parse(inp[2]);
Console.WriteLine(a + b);

if you want to input different values use these spaces to avoid exception errors.

Te answered 17/11, 2023 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.