0

so the sample code of what I've done is here:

        char regular = 'r';
        char premium = 'p';
        Console.WriteLine("Please enter your service (r or p:");

I am trying to get the user to input either r or p for the service they are have, but I don't know what to put under the "Console.WriteLine" to make it work with the char data types I entered. How do I fix this?

Also, specifically for the entering your service, it has to be in char, so says the problem that I'm working on.

edit*: I have tried using something like...

Console.Writeline("Please enter your service(r or p):")'
r= Convert.ToInt32(Console.ReadLine());

but because it's in char, I have a problem, I'm just not fully sure how to implement this in a way where I can get the user to type r or p

Thanks!

  • I don't get it, what have you tried under the "Console.WriteLine" ? In general you can use `char` with string, for example `"foo" + regular`. But i don't know what you're trying to achieve here. – Tim Schmelter Sep 19 '18 at 14:44
  • Or `char c = Console.ReadLine()[0];` – 001 Sep 19 '18 at 14:46
  • You could use this loop: `char[] allowedChars = {regular, premium}; char selected; while (!allowedChars.Contains(selected = Console.ReadKey().KeyChar)) Console.WriteLine("\nPlease enter either r for regular or p for premium:");` – Tim Schmelter Sep 19 '18 at 14:53
  • The question is asking about two different services, and having the user input either regular or premium, and it has to be in char. I've tried using an integer variable, but then because it's not converted, I get an error, eventually, down the road I have to add what the service cost is for each and with that I'll need to be using integers. – Masha J. Karabinovich Sep 19 '18 at 16:48
  • this is what the problem is asking: – Masha J. Karabinovich Sep 19 '18 at 16:49
  • The program prompts the user to enter an account number, a service code (of type char), and the number of minutes the service was used. A service code r (or R) means regular service; while code p (or P) means premium service. If the service is premium (code p or P), the customer may be using the service during both the day and night. Therefore, the program must ask the user to input the number of minutes used during daytime and nighttime in separate prompts. – Masha J. Karabinovich Sep 19 '18 at 16:49

2 Answers2

0

You can ask for a single character of input from the user with Console.ReadKey()

This returns a ConsoleKeyInfo object which has a KeyChar property that will give you a character

char regular = 'r';
char premium = 'p';
Console.WriteLine("Please enter your service (r or p:");

var keyEntered = Console.ReadKey();
Console.WriteLine("\nYou chose " + keyEntered.KeyChar);
Zeph
  • 1,728
  • 15
  • 29
0

I would suggest to use string instead of char. Something like:

string regular = "r";
string premium = "p";
Console.WriteLine("Please enter your service (r or p:");
var input = Console.ReadLine();
if (input == regular )
{
    //your reg logic here
}
else if (input == premium)
{
   // your premium logic here
}
else
{
   //handle case that none of above entered
}
apomene
  • 14,282
  • 9
  • 46
  • 72