JOEY TANG HCI
JOEY TANG HCI

Reputation: 11

Subtraction in c#?

I cannot subtract items form my dictionary in c#

This is the code, i am trying to get the fruits.Count as an int.

public class test
{
    public static void Main()
    {
        int totalStock = 10;`

        Dictionary<string, int> fruits = new Dictionary<string, int>();
        fruits.Add("Apples", 3);
        fruits.Add("pears", 4);
        int fruitsCount = fruits["Apples" + "pears"];
        if(fruitsCount > totalStock){
            Console.WriteLine("You have too many fruits! Please get rid of " + fruitsCount - totalStock + " fruits");
        }
        else if(fruitsCount = totalStock){
            Console.WriteLine("You have just the right amount of fruits!");
        }
        else{
            Console.WriteLine("You can fit " + totalStock - fruitsCount + " fruits");
        }

    }
}

But im getting errors:

exit status 1 main.cs(14,21): error CS0019: Operator `-' cannot be applied to operands of type 'string' and 'int'

main.cs(16,10): error CS0029: Cannot implicitly convert type 'int' to 'bool'

main.cs(20,21): error CS0019: Operator '-' cannot be applied to operands of type 'string' and 'int'

Upvotes: 1

Views: 1222

Answers (7)

Georgi Georgiev
Georgi Georgiev

Reputation: 3964

int fruitsCount = fruits["Apples" + "pears"];

is not valid C#. You can use

int fruitsCount = fruits["Apples"] + fruits["pears"];

or if you want to use LINQ

int fruitsCount = fruits.Values.Sum()

else if(fruitsCount = totalStock){ 

should be

else if(fruitsCount == totalStock){

otherwise you are doing an assignment which you can not do in a if condition.

To make your last subtraction right you need

Console.WriteLine($"You can fit { totalStock - fruitsCount } fruits");

Upvotes: 1

Eyad Kelleh
Eyad Kelleh

Reputation: 7

Frist you should learn more about Dictionary, you can find a good info hier: https://www.dotnetperls.com/dictionary

I had used list to get all the values of the integer of the fruits as fruitsValuesCount. Than i put the list in sum as fruitsValuesSum

Hier you can find the right answer:

using System;
using System.Collections.Generic;
using System.Linq;
namespace Test01
{
    class Program
    {
        static void Main(string[] args)
        {
            int totalStock = 10;

            var fruits = new Dictionary<string, int>
            {
                {"Apples", 3},
                {"pears", 4}
            };
            var fruitsValuesCount = new List<int>(fruits.Values);
            var fruitsValuesSum = fruitsValuesCount.Sum();
            int totalFruits = totalStock - fruitsValuesSum;
            if (fruitsValuesSum > totalStock)
            {
                Console.WriteLine("You have too many fruits! Please get rid of " + totalFruits + " fruits");
            }
            else if (fruitsValuesSum == totalStock)
            {
                Console.WriteLine("You have just the right amount of fruits!");
            }
            else
            {
                Console.WriteLine("You can fit " + totalFruits + " fruits");
            }

        }
    }
}

Upvotes: 0

Mikayel Davtyan
Mikayel Davtyan

Reputation: 1

Try this

   static void Main(string[] args)
        {
            int totalStock = 10;
            Dictionary<string, int> fruits = new Dictionary<string, int>();
            fruits.Add("Apples", 3);
            fruits.Add("pears", 4);
            int fruitsCount = fruits["Apples"]+ fruits["pears"];
            if (fruitsCount > totalStock)
            {
                Console.WriteLine($"You have too many fruits! Please get rid of {fruitsCount - totalStock} fruits");
            }
            else if (fruitsCount == totalStock)
            {
                Console.WriteLine("You have just the right amount of fruits!");
            }
            else
            {
                Console.WriteLine($"You can fit { totalStock - fruitsCount } fruits");
            }

        }

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

You don't have "Apples" + "pears" == "Applespears" fruit, that's why

fruits["Apples" + "pears"];

is wrong. Put it as fruits["Apples"] + fruits["pears"];:

public static void Main() {
  int totalStock = 10;

  Dictionary<string, int> fruits = new Dictionary<string, int>() {
    {"Apples", 3},       
    {"pears", 4},
  };

  // Either Apples + pears
  int fruitsCount = fruits["Apples"] + fruits["pears"];
  // ... Or sum of all the fruits
  // int fruitsCount = fruits.Values.Sum();

  if (fruitsCount > totalStock){
    Console.WriteLine($"You have too many fruits! Please get rid of {fruitsCount - totalStock} fruits");
  }
  else if(fruitsCount == totalStock) { // should be comparison "==" not assignement "="
    Console.WriteLine("You have just the right amount of fruits!");
  }
  else {
    Console.WriteLine($"You can fit {totalStock - fruitsCount} fruits");
  }

Be careful with strings: we can't subtract string but integers; in your case string interpolation is the solution (we subtract integers within the string):

  $"You have too many fruits! Please get rid of {fruitsCount - totalStock} fruits"

  $"You can fit {totalStock - fruitsCount} fruits"

Upvotes: 2

Malior
Malior

Reputation: 1341

try

 Console.WriteLine($"You can fit {totalStock - fruitsCount} fruits");

Upvotes: 1

Sweeper
Sweeper

Reputation: 271050

You have two main errors.

  1. fruits["Apples" + "pears"] evaluates to fruits["Applespears"]. You probably meant fruits["Apples"] + fruits["pears"].

  2. Use == instead of = for equality comparison in the else if branch.

Full code:

int totalStock = 10;`

Dictionary<string, int> fruits = new Dictionary<string, int>();
fruits.Add("Apples", 3);
fruits.Add("pears", 4);
int fruitsCount = fruits["Apples"] + fruits["pears"];
if(fruitsCount > totalStock){
    // note the added brackets
    Console.WriteLine("You have too many fruits! Please get rid of " + (fruitsCount - totalStock) + " fruits");
}
else if(fruitsCount = totalStock){
    Console.WriteLine("You have just the right amount of fruits!");
}
else{
    Console.WriteLine("You can fit " + (totalStock - fruitsCount) + " fruits");
}

Upvotes: 0

Serhat Oz
Serhat Oz

Reputation: 798

int fruitsCount = fruits["Apples" + "pears"];

is wrong. You can access dictionary values by writing:

int fruitsCount = fruits["Apples"] + fruits["pears"];

Upvotes: 1

Related Questions