/ cannot be applied to rand of type 'string' and 'int'

Hi,

I am new to the community and getting stuck on the following:

For an exercise I need to provide a few things, it will be an coin converter

  1. To find the maximum number of gold coins that “fit” into the amount (e.g. one gold coin fits into 11 cents):
  • Divide the amount by the value of a gold coin
  • Round down to the nearest integer
  • Store the result in a double variable called goldCoins

But continuously I got the error "Operator ‘/’ cannot be applied to rands of type ‘string’ and ‘int’"

The code I have;

using System;

namespace MoneyMaker
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      int gold = 10;
      int silver = 5;

      Console.WriteLine("Welcome to Money Maker!");

      Console.WriteLine("Enter an amount to convert to coins:");
      string userInput = Console.ReadLine();
      double userInputConvert = Convert.ToDouble(userInput);
      Console.WriteLine($"{userInput} cents is equal to ...");

      double goldCoins = Math.Floor(userInput / gold);
      
    }
  }
}

The error causes because of the last line of code;

double goldCoins = Math.Floor(userInput / gold);

If I change the variables in the () to numbers it will work, but this needs to calculate from the user input. That is why I think there should be a dividing between variables.

The error is pretty helpful? the division operator (/) can not be applied to rands of type string (userInput) and int (gold)

i mean, you convert the user input to a double, but you don’t end up using this double when doing the calculation.

Hi stetim94,

You are right about the error which is very helpful, but I didn’t quite understand it.

But what you’ve mentioned (you convert the user input to a double, but you don’t end up using this double when doing the calculation), made me thinking in the right direction.

The line of code which I had to change was:

      double goldCoins = Math.Floor(userInputConvert/ gold);

Thanks for your help!

Kind regards,
Oguzhan Kevek

Next time, you could look at the error, print the variable and types used in the division. These kind of steps can help you at getting better with handling errors.

that certainly gave me a clearer picture, putting variable names in the errors.

Thanks!