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.
Related
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.
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;
}
}
}
}
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;
}
I am very new to code. Can anyone in a simple way explain why I cant use the goto statement like this, to make the code start over again? Or, how this could have been done in the correct way? And also, why I get an error message on the use of "static".
**
"No such label "Start" within the scope of the goto statmenet"
"The modifier static is not valid for this item"
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Start:
Random numberGenerator = new Random();
int num1 = numberGenerator.Next(1,11);
int num2 = numberGenerator.Next(1, 4);
Console.WriteLine("What is " + num1 + " times " + num2 + "?");
int svar = Convert.ToInt32(Console.ReadLine());
if (svar == num1 * num2)
{
Console.WriteLine("well done!");
}
else
{
int responseIndex = numberGenerator.Next(1, 4);
switch (responseIndex)
{
case 1:
Console.WriteLine("Wrong, try again? [Y or N]");
AskUser();
break;
case 2:
Console.WriteLine("The answer was incorrect");
AskUser();
break;
default:
Console.WriteLine("You can do better than that");
AskUser();
break;
}
static void AskUser() {
string jaellernei = Console.ReadLine().ToUpper();
if (jaellernei == "Y")
{
goto Start;
} else
{
return;
} }
}
}
}
}
Firstly, your AskUser method is incorrectly nested inside the other method - move it out.
Secondly: goto is only valid within a single method; you can jump around a single stack frame - you cannot jump between stack frames.
Thirdly: the number of times you should be using goto... well, it isn't quite zero, but it asymptotically approaches zero.
Don't use goto unless you MUST to !
and As #Marc Gravell said, it's valid within a single method.
Alternatively : you can make a method of the code u used in the Main method, and then call it from both, main method and the other method where you used goto statement.
Like :
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
someFunction();
}
static void someFunction()
{
Random numberGenerator = new Random();
int num1 = numberGenerator.Next(1, 11);
int num2 = numberGenerator.Next(1, 4);
Console.WriteLine("What is " + num1 + " times " + num2 + "?");
int svar = Convert.ToInt32(Console.ReadLine());
if (svar == num1 * num2)
{
Console.WriteLine("well done!");
}
else
{
int responseIndex = numberGenerator.Next(1, 4);
switch (responseIndex)
{
case 1:
Console.WriteLine("Wrong, try again? [Y or N]");
AskUser();
break;
case 2:
Console.WriteLine("The answer was incorrect");
AskUser();
break;
default:
Console.WriteLine("You can do better than that");
AskUser();
break;
}
}
}
static void AskUser()
{
string jaellernei = Console.ReadLine().ToUpper();
if (jaellernei == "Y")
{
someFunction();
}
else
{
return;
}
}
}
}
You could do it like this
public static Random randd = new Random();
public static void FlachCards()
{
Start:
if (AskAUser() == "Y")
{
goto Start;
}
}
public static String AskAUser()
{
Console.WriteLine("Enter Y to play again");
return Console.ReadLine();
}
I am trying to go back to start in c# console program, and i am somehow able to do it, but the problem is that i am not able to do it using the Y/N statement. mean if i type the Y (yes) from keyboard then the program start from it's starting (beginning) point if i type N then it will terminate the program.
My Console code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace simple_calculation
{
class Program
{
static void Main(string[] args)
{
bool bktop = true;
string option;
while (bktop)
{
Console.WriteLine("Please enter the values");
int val = int.Parse(Console.ReadLine());
int val1 = int.Parse(Console.ReadLine());
int res = val + val1;
int red = val / val1;
Console.WriteLine("Please enter the operator");
string oper=Console.ReadLine();
if (oper == "+")
{
Console.WriteLine("sum is:" + res);
}
else if (oper == "/")
{
Console.WriteLine("division is:" + red);
}
else
{
Console.WriteLine("do you want to continue? enter Y/N");
option = Console.ReadLine();
if(option=="Y")
{
bktop = false;
}
else
{
bktop = true;
}
}
Console.ReadLine();
}
}
}
}
What I want: i want that when the program reaches to the else condition then there is a text appear in the console "do you want to continue? enter Y/N" if the user enter Y then the program start again and if the user enter N then the program will terminate.
Any help will be appreciated.
class Program
{
static void Main(string[] args)
{
// Change 1
bool bktop = true;
string option;
while (bktop)
{
bktop = true;
Console.WriteLine("Please enter the values");
int val = int.Parse(Console.ReadLine());
int val1 = int.Parse(Console.ReadLine());
int res = val + val1;
int red = val / val1;
Console.WriteLine("Please enter the operator");
string oper = Console.ReadLine();
if (oper == "+")
{
Console.WriteLine("sum is:" + res);
}
else if (oper == "/")
{
Console.WriteLine("division is:" + red);
}
else
{
Console.WriteLine("do you want to continue? enter Y/N");
option = Console.ReadLine();
// Change 2
if (option.ToUpper() != "Y")
{
bktop = false;
}
}
}
}
}