C# Basic Calculator SharpDevelop Error

747 views Asked by At

I was aiming to create a simple code that takes an input and gives out the square of the number. I used another method for calculation but i think there is an error with the method setting. Please take a look.

using System;
namespace NumberM {
    class InputLocation {
        public static void Main (string [] args) {
            int PlsCall;
        begin:
            Console.Write ("Please specify a number : ");
            string NumInput = Console.ReadLine ();
            int NumNewValue = Int32.Parse (NumInput);
            Console.WriteLine("Is {0} the number you specified?", NumNewValue);
            string Ans = Console.ReadLine();

            if (Ans == "yes" || Ans == "Yes") {
                Console.WriteLine ("That means {0} is your number. All right. Calculating.");
                // disable once SuggestUseVarKeywordEvident
                CalcNumb InP = new CalcNumb();
                PlsCall = InP.Calculation (NumNewValue);
            } else {
                 Console.WriteLine ("That might be an issue. Taking back.");
                 goto begin;
            }

        }
    }
    class CalcNumb {
        public int Calculation (int number) {
            int Store = number * number;
            Console.WriteLine ("This is the square of your number : {0}" , Store);
            }
    }
}

I get an error at the line 35 and 26 for calling method. PlsCall = InP.Calculation (NumNewValue); public int Calculation (int number) {

I get something like this. I can't translate well, but it is Turkish, if you need the translation.

'NumberM.CalcNumb.Calculation(int)': tüm kod yolları değer döndürmez (CS0161)

Thanks in advance for the help.

4

There are 4 answers

0
a_user8891 On

The calculation method is an integer and you are not returning anything

  public int Calculation (int number) 
  {
     int Store = number * number;
     Console.WriteLine ("This is the square of your number : {0}" , Store);
     return Store; // Needed
  }
0
Xaruth On

You must return something !

public int Calculation (int number) 
{
    int Store = number * number;
    Console.WriteLine ("This is the square of your number : {0}" , Store);
    return Store;
}

Another error in this line :

Console.WriteLine ("That means {0} is your number. All right. Calculating.");

It should be :

Console.WriteLine ("That means {0} is your number. All right. Calculating.", NumNewValue);

And, please avoid goto in C# code ;)

0
StackOverlord On

your method Calculation is not returning a value

0
Drakosha On

Replace

class CalcNumb { public int Calculation (int number) { int Store = number * number; Console.WriteLine ("This is the square of your number : {0}" , Store); } }

to

class CalcNumb { public int Calculation (int number) { int Store = number * number; Console.WriteLine ("This is the square of your number : {0}" , Store); return Store; } }