0

The problem comes in line 34: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
    {
        string FirstName;
        string LastName;
        int Hours = 0;
        int OvertimeHours = 0;
        int HourlyPay;
        int GrossPay;

        //float AveragePay;

        Console.WriteLine("Please enter your first name!");
        FirstName = Console.ReadLine();
        Console.WriteLine("Please enter your last name!");
        LastName = Console.ReadLine();
        Console.WriteLine("How many hours have you worked this week?");
        Hours = Int32.Parse(Console.ReadLine());
        Console.WriteLine("How many overtime hours did you work this week?");
        OvertimeHours = Int32.Parse(Console.ReadLine());
        Console.WriteLine("What is your hourly pay ?");
        HourlyPay = Int32.Parse(Console.ReadLine());

        if (Hours > 40)
            GrossPay = (OvertimeHours * HourlyPay *1.5) + (Hours * HourlyPay);
        else
            GrossPay = (Hours * HourlyPay);

            Console.ReadLine();
    }
}
}

I have no idea what to do right now, and what a double is at all. I'm still a very new programmer with only like a few weeks experience and any and all help would be appreciated, thanks.

Boogy
  • 23
  • 1
  • 3
    Welcome to Stack Overflow. This is not a tutorial site, I'm afraid. You can find many tutorials by using Google, and there are many books available at [Amazon](http://www.amazon.com). Or you could ask the teacher that gave you the assignment for help; they're being paid to provide you with what you need to complete it. – Ken White Sep 26 '17 at 02:42
  • The `double` in this case comes from the `1.5`. When you multiply by a decimal it assumes that you are using a double. Try making `GrossPay` a double. – Joshua Drake Sep 26 '17 at 02:44
  • 2
    You found SO via Google I assume, why not search for `double` the same way? –  Sep 26 '17 at 02:51
  • it's considered bad practice to use double / float for money. There is a much better type `decimal` – Keith Nicholas Sep 26 '17 at 03:18

2 Answers2

1

Numbers with decimal points are considered double. You declared GrossPay as int. When you *1.5, the number becomes a decimal number. Therefore, you face that error. Try changing int GrossPay to double GrossPay. Another tip for you, you can use double instead of int for Hours/HourlyPay as hours can be '1.5 hours' and HourlyPay can be '$15.50/hr'. Hope this helps you.

Do google thoroughly as all these answers can surely be found on google and tutorials :)

0

The bottom line is that when you do calculations like this

if (Hours > 40)
    GrossPay = (OvertimeHours * HourlyPay *1.5) + (Hours * HourlyPay);
else
    GrossPay = (Hours * HourlyPay);

The result of it is almost never int. You need to convert like this

GrossPay = Convert.ToInt32((OvertimeHours * HourlyPay *1.5) + (Hours * HourlyPay));

Bau then remember about roundings that come with it. May be int is wrong data type for this case

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 1
    If you think that you will lose precision, then set GrossPay as a double. – Borko Djurovic Sep 26 '17 at 03:01
  • Yeah, don't be converting a `double` to an `int` when you're dealing with money. $4.99 becomes $4. In fact, you should be using a `decimal` type when dealing with currency. – Rufus L Sep 26 '17 at 04:46
  • @RufusL You're right. And the answer mentions that `int` is not right type in this case. But OP mostly needed to understand that such math operations rarely return `int`, hence OP couldn't set the variable – T.S. Sep 26 '17 at 05:18