Jump to content

Can't figure out what's wrong (C#)

OopsThatIsMine

Hi, I'm trying to learn C# and I've tried making a single-input calculator (to calculate the odds of opening a knife in a CS:GO case when you open so many of them). 

I've got the errors "Cannot implicitly convert type 'string' to int'" (for line 10), "Cannot implicitly convert type 'double' to 'decimal'. An explicit conversion exists (are you missing a cast?)" (for line 11), and "Argument 1: cannot convert from 'decimal' to 'double'".

 

Does anyone know what's wrong and/or how to fix? Code is below. Thanks :)

 

using System;

 

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("How many cases will you open?");
            int NoOfCase = Console.ReadLine();
            decimal RawOdds = 100 - 0.44;
            decimal OddsNoKnife = Math.Pow(RawOdds, NoOfCase);
            decimal OddsKnife = 100 - OddsNoKnife;

 

            Console.WriteLine("You have a " + OddsKnife + "% chance of getting a knife.");
        }
    }
}

its called the solar system because our sun is called sol

Link to comment
Share on other sites

Link to post
Share on other sites

Math.Pow does not take decimals, but doubles.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, tikker said:

Math.Pow does not take decimals, but doubles.

Thank you! I got it going

its called the solar system because our sun is called sol

Link to comment
Share on other sites

Link to post
Share on other sites

Also, in c# console.readline () returns a string and needs to be stored to a string value. Then you will need to change it to whatever data type you need using parse or tryparse. I always use tryparse to check for good input as int.tryparse () returns a Boolean value. So it's easy to say for example

Int noOfCases;

String noOfCaseString = console.Readline ();

While (!int.tryParse(noOfCaseString, out noOfCases)){

//reprompt for good input

}

//do stuff

 

(I'm on a phone so syntax might be off here or there)

Link to comment
Share on other sites

Link to post
Share on other sites

On 5/5/2018 at 11:59 AM, OopsThatIsMine said:

Thank you! I got it going

On another note, if you absolutely needed the decimal data type, you could cast it to the appropriate data type and then cast it back, but this may have issues (there may be a loss in precision or something). But anyway, the code would look like:

decimal foobar = 0.0;
decimal A = 2.0;
decimal B = 2.0;
foobar = (decimal)Math.Pow((double)A,(double)B);

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×