Alex Comiskey Alex Comiskey - 2 months ago 13
C# Question

Converting an integer to a double inside switch case

This program is for a payroll but i haven't got far yet

Console.WriteLine("What is your name");
string name = Console.ReadLine();

Console.WriteLine("What is your hourly rate\n1=6.53\n2=7.48\n3=8.89");
int hourlyRate = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("How many hours do you work?\n1=35\n2=38\n43");
int noOfHour = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("What is you National Insurance no.?\n1=JLS302\n2=KM8215\nPQ7316");
int niNumber = Convert.ToInt32(Console.ReadLine());

switch (hourlyRate)
{
case 1: hourlyRate == 6.53;
}


I'm not sure how exactly to convert hourlyRate which has the value of an integer to 6.43 which is a decimal

Answer

You are going to have to declare a variable as double to hold the actual rate.

Since you are using hourlyRate to hold the "selection" you'll probably need another variable to hold the actual rate: like this:

Console.WriteLine("What is your name");
string name = Console.ReadLine();

Console.WriteLine("What is your hourly rate\n1=6.53\n2=7.48\n3=8.89");
int hourlyRate = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("How many hours do you work?\n1=35\n2=38\n43");
int noOfHour = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("What is you National Insurance no.?\n1=JLS302\n2=KM8215\nPQ7316");
int niNumber = Convert.ToInt32(Console.ReadLine());

//ADDED this here:
double rate = 0.00;
switch (hourlyRate)
{
    case 1: {
        rate = 6.53;
        break;
    }
    case 2: {
        rate = 7.48;

        break;
    }
    case 3: {
        rate = 8.89;
        break;
    }       

}
Console.WriteLine("This is the rate: " + rate);
Comments