Search code examples
c#.netstringintreturn

(Hackerrank - Fair Rations) When return a string following error appears(C#). error CS0029: Cannot implicitly convert type string' to int


error CS0029: Cannot implicitly convert type string' to int

I'm trying to solve HackerRank's Fair Rations. I should return an Integer or string "NO". When I trying to return string "NO" the following error message appears

// Solution.cs(49,16): error CS0029: Cannot implicitly convert type `string' to `int'

Following is my code.

int temp = 0;
string s = "N0";

for(int i = 0; i < B.Length - 1; i++)
{  
   if(B[i] % 2 == 1)
   {
        B[i] = B[i] + 1;
        B[i + 1] = B[i + 1] + 1;
        temp += 2;
   }
}

for(int i = 0; i < B.Length; i++)
{
    if(B[i] % 2 == 1)
    {
        temp = 0;
    }
}

if(temp > 0)
{
    return temp;
}
else
{
    return s;
}

How I return "NO" ?. When I use `Console.WriteLine()' following error messege appears

//Solution.cs(18,16): error CS0161: `Solution.fairRations(int[])': not all code paths return a value

Solution

  • I think I understand why you got confused. If you go to the site, and choose to write an answer in C#, this is the template you get:

    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;
    
    class Solution {
    
        // Complete the fairRations function below.
        static int fairRations(int[] B) {
    
    
        }
    
        static void Main(string[] args) {
            TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
    
            int N = Convert.ToInt32(Console.ReadLine());
    
            int[] B = Array.ConvertAll(Console.ReadLine().Split(' '), BTemp => Convert.ToInt32(BTemp))
            ;
            int result = fairRations(B);
    
            textWriter.WriteLine(result);
    
            textWriter.Flush();
            textWriter.Close();
        }
    }
    
    

    But one of the requirements states:

    If it's not possible to do this, print NO.

    And the instructions say:

    // Complete the fairRations function below.

    That's a lousy template for a function that needs to also handle an additional condition of printing NO if the solution is not possible.

    You'll have to modify the signature of the fairRations function to handle the case where the solution is not possible. You're also allowed to modify the code in Main, which is clearly inadequate for fulfilling the requirements of the challenge as is.

    And if you need to write something other than the int result to the output, then you'll need to make a different textWriter.WriteLine call that outputs a string.

    I strongly recommend the pattern used in functions like int.TryParse or Dictionary<TKey,TValue>.TryGetValue. That pattern is based on these two principles:

    1. return a bool to indicate success/fail.
    2. deliver the payload for the successful case (in this case: an integer) by means of an out parameter.

    Modify the fairRations function like so:

    static bool fairRations(int[] B, out int result) {
    
       if (/* TODO: it is possible? */) {
          result = /* TODO: whatever the result is */;
          return true;
       } else {
          result = 0;
          return false;
       }
    }
    

    And you may want to modify the call in Main as follows:

    change...

            int result = fairRations(B);
    
            textWriter.WriteLine(result);
    

    to...

            if (fairRations(B, out int result)) {
                textWriter.WriteLine(result);
            } else {
                textWriter.WriteLine("NO");
            }