I Can't call the methods in C# am using [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
This is my code
Basic Calculator Using 2 methods
1 is the main method
2 is the operator
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int num1;
int num2; //Variables for equation
string op;
//int Answer;
Console.Write("Enter the first number : ");
num1 = Convert.ToInt32(Console.ReadLine());
//User input for equation
Console.Write("Now enter your second number : ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Ok now enter your operation ( x , / , +, -) ");
op = Console.ReadLine();
Methods1(); // This is the error, The error says
// There's no argumen given that corresponds
}
static int Methods1(int num1, int num2, string op)
{
if (op == "+")
{
return num1 + num2;
}
else if (op == "-")
{
return num1 - num2;
}
else if (op == "/")
{
return num1 / num2;
}
else if (op == "*")
{
return num1 * num2;
}
else
{
Console.WriteLine("Invalid Operator");
return 0;
}
}
}
}

You have called the Methods1() method without arguments. Change the code to the following:
using System;
namespace Test_app_1
{
class Program
{
static void Main(string[] args)
{
int num1;
int num2; //Variables for equation
string op;
int Answer;
Console.Write("Enter the first number : ");
num1 = Convert.ToInt32(Console.ReadLine());
//User input for equation
Console.Write("Now enter your second number : ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Ok now enter your operation ( * , / , +, -) ");
op = Console.ReadLine();
Answer = Methods1(num1, num2, op); // Now no error...
Console.WriteLine(Answer + "\nPress any key to exit..."); //You forgot to add Console.Writeline
Console.Read(); //Also forgot Console.Read
}
static int Methods1(int num1, int num2, string op)
{
if (op == "+")
{
return num1 + num2;
}
else if (op == "-")
{
return num1 - num2;
}
else if (op == "/")
{
return num1 / num2;
}
else if (op == "*")
{
return num1 * num2;
}
else
{
Console.WriteLine("Invalid Operator");
return 0;
}
}
}
}

Related

How do I make my C# program start again from the top after every time it finishes

guys, I'm a beginner at this, after trying to build this calculator I just wanted to learn how to make it start again I tried using a while loop but unfortunately, I wasn't able to figure it out please help thanks.
using System;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter a number");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Add an operator");
string op = Console.ReadLine();
Console.Write("Enter another number");
double num2 = Convert.ToDouble(Console.ReadLine());
if (op == "+")
{
Console.WriteLine(num1 + num2);
}
else if (op == "-")
{
Console.WriteLine(num1 - num2);
}
else if (op == "/")
{
Console.WriteLine(num1 / num2);
}
else if (op == "*")
{
Console.WriteLine(num1 * num2);
}
else
{
Console.WriteLine("Invalid operator");
}
}
}
}
To keep it running, you can add a while(true) to wrap your logic code. this will repeatedly execute the block of code. In case there is a condition where you want to exit the loop, you can achieve this via break
static void Main(string[] args)
{
while (true){
Console.Write("Enter a number");
double num1 = Convert.ToDouble(Console.ReadLine());
Console.Write("Add an operator");
string op = Console.ReadLine();
Console.Write("Enter another number");
double num2 = Convert.ToDouble(Console.ReadLine());
if (op == "+")
{
Console.WriteLine(num1 + num2);
}
else if (op == "-")
{
Console.WriteLine(num1 - num2);
}
else if (op == "/")
{
Console.WriteLine(num1 / num2);
}
else if (op == "*")
{
Console.WriteLine(num1 * num2);
}
else
{
Console.WriteLine("Invalid operator");
}
if (//condition is ok and you want to break out of the loop){
break;
}
}
}
If it is console application,
you could do something like this at the end of your app -
System.Diagnostics.Process.Start("your_prog_name.exe"); Environment.Exit(0);
It should terminate previous process and start it again.
Console window won't disappear.

C# How to loop back to my first line of code in my calculator?

Hello im trying to code a scientific calculator, im new to c# and im having some problems with trying to implement an option where at the end of a calculation I can ask the user whether they want to restart the calculator.
This is what i've got currently:
namespace Scientific_Calculator_Project
{
class SuperCalculator
{
static void Main(string[] args)
{
// Declaring my variables
double num1, num2, answer;
string operation;
string exitOption;
// Asking the user for the operation, to call a function to perform that operation
Console.Write("Please enter an operation you wish to perform (+, -, /, *, ^, ^1/2, !, f-1): ");
operation = Console.ReadLine();
// Prompting the input of the first number
Console.Write("Please enter your first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
// Prompting the input of the second number
Console.Write("Please enter your second number (if no other number is needed please enter ): ");
num2 = Convert.ToDouble(Console.ReadLine());
// Using if statements to decide which function to call based on the operation, as well as an error message for an invalid value
if (operation == "+")
{
Console.WriteLine(Sum(num1, num2));
}
else if (operation == "-")
{
Console.WriteLine(Minus(num1, num2));
}
else if (operation == "/")
{
Console.WriteLine(Divide(num1, num2));
}
else if (operation == "*")
{
Console.WriteLine(Multi(num1, num2));
}
else if (operation == "^")
{
Console.WriteLine(ToPower(num1, num2));
}
else if (operation == "^1/2")
{
Console.WriteLine(Sqroot(num1));
}
else if (operation == "!")
{
Console.WriteLine(Factorial(num1));
}
else if (operation == "f-1")
{
Console.WriteLine(ToInverse(num1));
}
else if (operation == "log")
{
Console.WriteLine(ToLog(num1));
}
else if (operation == "rad")
{
Console.WriteLine(ToRadian(num1));
}
else
{
Console.WriteLine("Invalid operation");
}
Console.WriteLine("Would you like to continue? (Yes/No)");
exitOption = Console.ReadLine();
Console.ReadLine();
// Function for addition (Sum)
static double Sum(double num1, double num2)
{
double resultofSum = num1 + num2;
return resultofSum;
}
// Function for subtraction (Minus)
static double Minus(double num1, double num2)
{
double resultofMinus = num1 - num2;
return resultofMinus;
}
// Function for division (Divide)
static double Divide(double num1, double num2)
{
double resultofDivide = num1 / num2;
return resultofDivide;
}
// Function for multiplication (Multi)
static double Multi(double num1, double num2)
{
double resultofMulti = num1 * num2;
return resultofMulti;
}
// Function for raising x (num1) to the power of y (num2)
static double ToPower(double num1, double num2)
{
double resultofToPower = Math.Pow(num1, num2);
return resultofToPower;
}
// Function for square root of x (num1)
static double Sqroot(double num1)
{
double resultofSqroot = Math.Sqrt(num1);
return resultofSqroot;
}
// Function for finding factorial of x (num1),
static double Factorial(double num1)
{
double factorial = 1;
if (num1 < 0)
{
Console.WriteLine("Error: Can't find factorial of a negative number");
return 0;
}
else if (num1 <= 1)
{
return 1;
}
else
{
for (double i = 1; i <= num1; i++)
{
factorial = factorial * i;
}
Console.WriteLine("{0}! = {1}", num1, factorial);
return factorial;
}
}
// Function for obtaining the inverse of x (1/num1)
static double ToInverse(double num1)
{
double ToInverse = 1 / num1;
return ToInverse;
}
// Function for obtaining the base 10 log of x (num1)
static double ToLog(double num1)
{
double Tolog = Math.Log10(num1);
return Tolog;
}
// Function for converting an angle x (num1) from degrees to radians
static double ToRadian(double num1)
{
double Toradian = (num1 * (Math.PI)) / 180;
return Toradian;
}
}
}
}
Lots of ways, but here is an example of one way. The code below will loop each time the user hits a key, unless they hit the "Enter" key, in which case it will drop out and end:
static void Main(string[] args)
{
ConsoleKeyInfo keyInfo;
bool loop = true;
while (loop)
{
//do your stuff here
//...
//then do this:
keyInfo = Console.ReadKey();
if(keyInfo.Key == ConsoleKey.Enter)
{
loop = false;
}
else
{
loop = true;
}
}
}
Please see the changes below
using System;
namespace Scientific_Calculator_Project
{
class SuperCalculator
{
static void Main(string[] args)
{
// Declaring my variables
double num1, num2, answer;
string operation;
string exitOption;
while (true)
{
// Asking the user for the operation, to call a function to perform that operation
Console.Write("Please enter an operation you wish to perform (+, -, /, *, ^, ^1/2, !, f-1): ");
operation = Console.ReadLine();
// Prompting the input of the first number
Console.Write("Please enter your first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
// Prompting the input of the second number
Console.Write("Please enter your second number (if no other number is needed please enter ): ");
num2 = Convert.ToDouble(Console.ReadLine());
// Using if statements to decide which function to call based on the operation, as well as an error message for an invalid value
if (operation == "+")
{
Console.WriteLine(Sum(num1, num2));
}
else if (operation == "-")
{
Console.WriteLine(Minus(num1, num2));
}
else if (operation == "/")
{
Console.WriteLine(Divide(num1, num2));
}
else if (operation == "*")
{
Console.WriteLine(Multi(num1, num2));
}
else if (operation == "^")
{
Console.WriteLine(ToPower(num1, num2));
}
else if (operation == "^1/2")
{
Console.WriteLine(Sqroot(num1));
}
else if (operation == "!")
{
Console.WriteLine(Factorial(num1));
}
else if (operation == "f-1")
{
Console.WriteLine(ToInverse(num1));
}
else if (operation == "log")
{
Console.WriteLine(ToLog(num1));
}
else if (operation == "rad")
{
Console.WriteLine(ToRadian(num1));
}
else
{
Console.WriteLine("Invalid operation");
}
Console.WriteLine("Would you like to continue? (Yes/No)");
exitOption = Console.ReadLine();
if(exitOption.ToLower() == "no")
{
break;
}
// Function for addition (Sum)
static double Sum(double num1, double num2)
{
double resultofSum = num1 + num2;
return resultofSum;
}
// Function for subtraction (Minus)
static double Minus(double num1, double num2)
{
double resultofMinus = num1 - num2;
return resultofMinus;
}
// Function for division (Divide)
static double Divide(double num1, double num2)
{
double resultofDivide = num1 / num2;
return resultofDivide;
}
// Function for multiplication (Multi)
static double Multi(double num1, double num2)
{
double resultofMulti = num1 * num2;
return resultofMulti;
}
// Function for raising x (num1) to the power of y (num2)
static double ToPower(double num1, double num2)
{
double resultofToPower = Math.Pow(num1, num2);
return resultofToPower;
}
// Function for square root of x (num1)
static double Sqroot(double num1)
{
double resultofSqroot = Math.Sqrt(num1);
return resultofSqroot;
}
// Function for finding factorial of x (num1),
static double Factorial(double num1)
{
double factorial = 1;
if (num1 < 0)
{
Console.WriteLine("Error: Can't find factorial of a negative number");
return 0;
}
else if (num1 <= 1)
{
return 1;
}
else
{
for (double i = 1; i <= num1; i++)
{
factorial = factorial * i;
}
Console.WriteLine("{0}! = {1}", num1, factorial);
return factorial;
}
}
// Function for obtaining the inverse of x (1/num1)
static double ToInverse(double num1)
{
double ToInverse = 1 / num1;
return ToInverse;
}
// Function for obtaining the base 10 log of x (num1)
static double ToLog(double num1)
{
double Tolog = Math.Log10(num1);
return Tolog;
}
// Function for converting an angle x (num1) from degrees to radians
static double ToRadian(double num1)
{
double Toradian = (num1 * (Math.PI)) / 180;
return Toradian;
}
}
}
}
}

Why isn't the char changing the way I Intend it too?

I am new here so please excuse me for the poor formatting,
I wrote a simple calculator in c# but it seems my multiplication and division are not working correctly.
when running the code it works fine until I try to output the answer and then it outputs "Error, Unknown operator" which is what I told it to output when it doesn't identify the operator stored in the operation variable.
here is the code (sorry for dumping so much code, I am not sure what is relevant and what is not):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class MainClass
{
public static void Main(string[] args) // method called "main", when the program starts, this runs
{
start:
// variable declarations
double Num1;
double Num2;
int operationId;
string operationName = "1";
string operationName2 = "1";
Char operation;
double answer;
// choosing an operator
Console.WriteLine("select an operation from the list and type it's associated number:");
Console.WriteLine("1 - sum \n" + "2 - subtraction \n" + "3 - division\n" + "4 - multipication \n");
operationId = Convert.ToInt32(Console.ReadLine());
// checking which operation has been chosen
if (operationId == 1)
{
operationName = "added to";
operationName2 = "added";
operation = '+';
} else if (operationId == 2)
{
operationName = "subtracted from";
operationName2 = "subtracted";
operation = '-';
} else if (operationId == 3)
{
operationName = "divided";
operationName2 = "divided by";
operation = '/';
} else if (operationId == 4)
{
operationName = "multiplied";
operationName2 = "multiplied by";
operation = '*';
} else
{
Console.WriteLine("Invalid option");
goto start;
}
// receving user input
Console.WriteLine("Insert a number to be " + operationName + ":");
Num1 = Convert.ToDouble (Console.ReadLine());
Console.WriteLine("Insert a number to be " + operationName2);
Num2 = Convert.ToDouble(Console.ReadLine());
//calculating answer
if (operation == '+')
{
answer = Num1 + Num2;
}
else if (operation == '-')
{
answer = Num1 - Num2;
}
else if (operationId == '/')
{
answer = Num1 / Num2;
}
else if (operationId == '*')
{
answer = Num1 + Num2;
} else
{
answer = 0000;
Console.WriteLine("Error, Unknown operator \n");
goto start;
}
Console.WriteLine();
Console.WriteLine("The result is:");
Console.WriteLine(answer);
Console.ReadKey();
Console.WriteLine();
goto start;
}
}
}
Your first 2 ifs check operation, the second 2 check operationId. Change the multiply and divide ones to also check operation.
//...
else if (operation == '/')
{
answer = Num1 / Num2;
}
else if (operation == '*')
{
answer = Num1 * Num2; //<-- Change to this from Num1 + Num2
}
//...
And by the way, your multiply block is adding the numbers, not multiplying. I've fixed that in my block above.
I guess the problem is with - 2 conditions are checking operation and 2 conditions are checking operationId. Perhaps you may want to change all the checks to be either with operation or with operationId.
if (operation == '+')
{
answer = Num1 + Num2;
}
else if (operation == '-')
{
answer = Num1 - Num2;
}
else if (operationId == '/')
{
answer = Num1 / Num2;
}
else if (operationId == '*')
{
answer = Num1 + Num2;
} else
{
answer = 0000;
Console.WriteLine("Error, Unknown operator \n");
goto start;
}

Using multiple methods to create a simple calculator

I wrote out a simple calculator that uses multiple methods.
This is what I wrote out, but is there a better/more efficient way to write this code?
Suggestions would be helpful.
I want to use multiple methods and have everything visible in the main method. I know it looks silly and can be done in a much more simple way, but I'm trying to explain this to people who are new to C# and are beginning to learn about methods.
{
Console.WriteLine("Please input two numbers");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
int answer1 = Sum(num1, num2);
int answer2 = Sub(num1, num2);
int answer3 = Mult(num1, num2);
double answer4 = Div(num1, num2);
Console.WriteLine("Sum: {0}\n"+"Difference: {1} \n"+"Multiplication: {2} \n"+"Division: {3} \n", answer1, answer2, answer3, answer4);
}
public static int Sum(int num1, int num2)
{
return num1 + num2;
}
public static int Sub(int num1, int num2)
{
return num1 - num2;
}
public static int Mult(int num1, int num2)
{
return num1 * num2;
}
public static double Div(int num1, int num2)
{
return num1 / num2;
}
}
You can create a small menu to choose what you want to do.
The following code is a code example, and you can refer to it.
class Program
{
static void Main(string[] args)
{
string a = string.Empty;
do
{
Console.WriteLine("Please input two numbers");
int num1 = Convert.ToInt32(Console.ReadLine());
int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please choose the following you want to do");
Console.WriteLine("1. Add");
Console.WriteLine("2. Minus");
Console.WriteLine("3. Mult");
Console.WriteLine("4. Div");
int number = Convert.ToInt32(Console.ReadLine());
switch (number)
{
case 1:
Console.WriteLine("Your result is" + Add(num1, num2));
break;
case 2:
Console.WriteLine("Your result is" + Minus(num1, num2));
break;
case 3:
Console.WriteLine("Your result is" + Mult(num1, num2));
break;
case 4:
Console.WriteLine("Your result is" + Div(num1, num2));
break;
default:
Console.WriteLine("Please input again");
break;
}
a = Console.ReadLine();
}
while (a == "y");
}
public static int Add(int num1, int num2)
{
return num1 + num2;
}
public static int Minus(int num1, int num2)
{
return num1 - num2;
}
public static int Mult(int num1, int num2)
{
return num1 * num2;
}
public static double Div(int num1, int num2)
{
return num1 / num2;
}
}
The only possible improovement I could think off, is making those operators. But the existing Operators for Int alraedy cover all those cases and it only works for your self-created types anyway.
As you are on the console. A do...while loop with a swtich/case as menu might be usefull for the programm flow. But not anything else I can think off.
Math is even one of the rare examples where static methods are not missplaced/overused.
Only a small terminology nitpick: Those are functions, not methods:
functions return a value
methods do not
However I have not see a langauge that actually differentaites between the two in ages. Delphi or VB might have this Keyword distinction.

why 2 is automatically converted to 299?

I'm new:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
private static void Main()
{
Console.WriteLine("Welcome to my calculator");
Console.WriteLine("Calculator only supports -,+,/,*");
Console.WriteLine("Calculator V1.20 alpha");
Console.WriteLine("Enter your first number");
int num1 = Console.ReadKey().KeyChar;
Console.WriteLine("Enter your operator");
Console.WriteLine();
char operation = Console.ReadKey().KeyChar;
Console.WriteLine("Enter your second number");
Console.WriteLine();
int num2 = Console.ReadKey().KeyChar;
//the answer variables
int answersubtract = num1 - num2;
int answeradd = num1 + num2;
int answermulti = num1 * num2;
int answerdiv = num1 / num2;
if (operation == '-')
{
Console.WriteLine(answersubtract);
}
else if (operation == '+')
{
Console.WriteLine(answeradd);
}
else
{
if (operation == '*')
{
Console.WriteLine(answermulti);
}
else
{
if (operation == '/')
{
Console.WriteLine(answerdiv);
}
}
}
Console.ReadKey();
}
}
}
Edit:
The input I am sending to the program is:
1+2
int num1 = Console.ReadKey().KeyChar;
You are reading a character, not a number. Review the docs for ConsoleKeyInfo.KeyChar.
Allow the user to enter real numbers:
int num1 = int.Parse(Console.ReadLine());
Focus on error handling and the switch statement in version 2 of your program.
Because when you enter 2 its represented as a char which its value is 229.

Categories