How to send invaild user input when using a split command - c#

My program works correctly. The only thing I am trying to figure out is how to send an error for invalid user input. If a user types in strings or numbers without using spaces, "a * b", "12*5", how can I send an error in a split.
using System;
namespace Calculator1
{
class Program
{
static void Main(string[] args)
{
double answer;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine(" For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
}
else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
}
else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
}
else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response == "Yes");
}
}
}
}
Calculator
Math Operations:
Multiplication: *
Addition: +
Subtraction: -
Division: /
Enter your equation below:
For example: 5 + 5
5*5 //I want to send an error here
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(ReadOnlySpan`1 value, NumberStyles options, NumberFormatInfo numfmt)
at System.Double.Parse(String s)
at Calculator1.Program.Main(String[] args)
Press any key to continue . . .
Calculator
Math Operations:
Multiplication: *
Addition: +
Subtraction: -
Division: /
Enter your equation below:
For example: 5 + 5
a * b //I also want to send an error here
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.ParseDouble(ReadOnlySpan`1 value, NumberStyles options, NumberFormatInfo numfmt)
at System.Double.Parse(String s)
at Calculator1.Program.Main(String[] args)
Press any key to continue . . .

This is probably what you need:
public static void Main(string[] args)
{
double answer;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine(" For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
try{
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
switch(operation){
case "*":
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
break;
case "/":
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
break;
case "+":
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
break;
case "-":
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
break;
default:
Console.WriteLine("Sorry that is not correct format! Please restart!");
break;
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response == "Yes");
}
catch(FormatException ex){
Console.WriteLine("You entered a bad operation, try another one");
}
}
}
All you have to do is to catch a format exception that caused by a parse of a double (that isn't really a double). If you catch it, then you print an error message to the user.

I don't know what you mean by "send an error in a split". Split just splits a string.
Validate the input the do a Console.Writeline with your error message.
For validating the user input I suggest using TryParse instead of Parse. This avoids the exceptions and you can check if the input was valid.
And yes. Use a switch statement instead of your if else version. Ctrl-. should offer a refactoring for this.
Kind regards
Bernd

See this solution, please:
// Your codes
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine(" For example: 5 + 5 ");
string str = Console.ReadLine().Replace(" ", ""); // Remove all spaces
string[] values = Regex.Split(str, #"([\+\-\*\/])"); // Split numbers and operator and also keep operator
if (values.Length != 3)
{
Console.WriteLine("Expresion is not correct.");
}
else
{
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
switch (operation)
{
case "*":
answer = firstNum * secondNum;
break;
case "/":
answer = firstNum / secondNum;
break;
case "+":
answer = firstNum + secondNum;
break;
case "-":
answer = firstNum - secondNum;
break;
default:
Console.WriteLine("Sorry that is not correct format! Please restart!");
break;
}
Console.WriteLine($"{firstNum} {operation} {secondNum} = {answer}");
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response.ToUpper() == "YES" || response.ToUpper() == "Y");
}
}
// Your codes

Related

Choosing a Option Based On What you Type

I just need a little bit of help. I'm pretty new to coding and I watched some videos and managed to get this far without following exact words from videos. I'm just trying to Make a Calculator in a console window that when you type "Multiply" Or "Divide" it will jump to that section and then you can multiply or divide there. Then when you finish it will close the console.
I thought it worked fine at first but since Divide is the second function even if you type "Divide" it won't do anything because it still technically you just went over multiply, you will have to type "Divide" again for it to start the function.
Any help would be appreciated.
using System;
namespace SelfTeaching
{
class Program
{
static void Main(string[] args) //Meathod Aka "Main," This will get called when program Starts
{
int num01;
int num02;
Console.WriteLine("Multiply Or Divide");
if (Console.ReadLine() == "Multiply")
{
Console.Write("Type Number 1: ");
num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("Type Number 2: ");
num02 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("The Answer Is; " + num01 * num02);
Console.ReadKey();
Environment.Exit(0);
}
else if (Console.ReadLine() == "Divide")
{
Console.Write("Type Number 1: ");
num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("Type Number 2: ");
num02 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num01 + " Divided by " + num02 + " Equals: " + num01 / num02);
Console.ReadKey();
Environment..Exit(0);
}
}
}
}
To get Multiply Or Divide from single input, you have to take that string from input and then to use if-else block to follow user's choice.
Also you need to use double instead int, if you need precise result with decimal when you use divide.
class Program
{
static void Main(string[] args)
{
double num01;
double num02;
Console.Write("Multiply Or Divide ");
string userInput = Console.ReadLine();
if (userInput.Equals("Multiply", StringComparison.OrdinalIgnoreCase))
{
Console.Write("Type Number 1: ");
num01 = Convert.ToDouble(Console.ReadLine());
Console.Write("Type Number 2: ");
num02 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("The Answer Is; " + num01 * num02);
Console.ReadKey();
Environment.Exit(0);
}
else if (userInput.Equals("Divide", StringComparison.OrdinalIgnoreCase))
{
Console.Write("Type Number 1: ");
num01 = Convert.ToDouble(Console.ReadLine());
Console.Write("Type Number 2: ");
num02 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(num01 + " Divided by " + num02 + " Equals: " + num01 / num02);
Console.ReadKey();
Environment.Exit(0);
}
}
}
Its a good practice to use DRY principle (Don't Repeat YourSelf)
This is example of better code:
class Program
{
static void Main(string[] args)
{
double num01;
double num02;
Console.Write("Multiply Or Divide ");
string userInput = Console.ReadLine();
Console.Write("Type Number 1: ");
num01 = Convert.ToDouble(Console.ReadLine());
Console.Write("Type Number 2: ");
num02 = Convert.ToDouble(Console.ReadLine());
if (userInput == "Multiply")
{
Console.WriteLine("The Answer Is: " + num01 * num02);
}
else if (userInput == "Divide")
{
Console.WriteLine(num01 + " Divided by " + num02 + " Equals: " + num01 / num02);
}
Console.ReadKey();
Environment.Exit(0);
}
}
You could make a function called Divide first off
static int Divide(int num1, int num2)
{
int num3;
num1 / num2 = num3;
return num3;
}
then you can call that function when they want to divide if Readline is a particular value ( e.g Multiply or Divide ). Now, when they access this function, you can make a boolean var also called Divide that is true when a user has divided for instance:
if ( userinput = "divide" )
{
divide:
// enter num1
// enter num2
Divide(num1, num2): // call function
Console.Write(num3); // display num3
bool divide = true;
if ( divide == true; )
{
goto Divide;
}
I know people advise against using gotos but...
anyways, this would also effectively put people in a constant division loop. You should make a switch statement like #vasily.sib said, and probably with a while loop that constantly checks for keys being pressed so people can break out of the goto statement is what I would do. But anyways, its what you asked for :D

How do I keep the previous equations from being displayed

I have a project where the user can insert a math equation in a single line using the split command. In the first loop the equation get solved correctly and displays the equation with the answer. The problem is with the second loop. It solves the equation and displays the equation with the answer but it also displays the equation from before.
using System;
namespace Calculator1
{
class Program
{
static void Main(string[] args)
{
double answer;
double answer1;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine("For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
while (Continue)
{
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
}
else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
}
else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
}
else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes or No:");
string response = Console.ReadLine();
if (response == "Yes")
{
Console.WriteLine("\nEnter your equation below:");
string[] values1 = Console.ReadLine().Split(' ');
double firstNum1 = double.Parse(values1[0]);
string operation1 = (values1[1]);
double secondNum1 = double.Parse(values1[2]);
if (operation1 == "*")
{
answer1 = firstNum1 * secondNum1;
Console.WriteLine("\n" + firstNum1 + " * " + secondNum1 + " = " + answer1);
}
else if (operation1 == "/")
{
answer1 = firstNum1 / secondNum1;
Console.WriteLine("\n" + firstNum1 + " / " + secondNum1 + " = " + answer1);
}
else if (operation1 == "+")
{
answer1 = firstNum1 + secondNum1;
Console.WriteLine("\n" + firstNum1 + " + " + secondNum1 + " = " + answer1);
}
else if (operation1 == "-")
{
answer1 = firstNum1 - secondNum1;
Console.WriteLine("\n" + firstNum1 + " - " + secondNum1 + " = " + answer1);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
}
else
{
Continue = false;
}
}
}
}
}
Output:
Math Operations:
--------------------
Multiplication: *
Addition: +
Subtraction: -
Division: /
Enter your equation below:
For example: 5 + 5
5 + 5
5 + 5 = 10
Do you want to continue?
Type in Yes or No:
Yes
Enter your equation below:
10 + 10
10 + 10 = 20
5 + 5 = 10 \\ I don't want this to appear
Do you want to continue?
Type in Yes or No:
All you need to do is put the code that gets the user input into the while loop, and then let the loop continue if the user answers "Yes" at the end of the loop:
private static void Main()
{
double answer;
double answer1;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine("For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
}
else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
}
else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
}
else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
}
else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes or No:");
string response = Console.ReadLine();
if (response != "Yes") Continue = false;
}
}
Your Issue:
Its because you have the output code twice in the loop and both of them are displaying different variables.
Solution:
namespace Calculator1
{
class Program
{
static void Main(string[] args)
{
double answer;
double answer1;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
{
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine("For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
if (operation == "*")
{
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
} else if (operation == "/")
{
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
} else if (operation == "+")
{
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
} else if (operation == "-")
{
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
} else
{
Console.WriteLine("Sorry that is not correct format! Please restart!");
}
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes or No:");
string response = Console.ReadLine();
Continue = (response == "Yes");
}
}
}
}

Repeat a function if the output is undefined

Okay so I am new to C# and I just wanted to create a simple calculator with the console. My question is how could I make the user chose different values for num1 and num2 when they chose to divide zero, or divide by zero to prevent the program from crashing due to an undefined result. I figured I could use a while loop to check and see if answer results as an int, and if not repeat the entire program. How could I check answer to verify the result is an int?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
int num1;
int num2;
string operand;
float answer;
while ()
{
Console.WriteLine("Please enter the first number.");
num1 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Please enter which operator you would like to use(+,-,*,/)");
operand = Console.ReadLine();
Console.WriteLine("Please enter the second number.");
num2 = Convert.ToInt32(Console.ReadLine());
switch (operand)
{
case "+":
answer = num1 + num2;
Console.WriteLine(num1 + " + " + num2 + " = " + answer);
Console.ReadKey();
break;
case "-":
answer = num1 - num2;
Console.WriteLine(num1 + " - " + num2 + " = " + answer);
Console.ReadKey();
break;
case "*":
answer = num1 * num2;
Console.WriteLine(num1 + " * " + num2 + " = " + answer);
Console.ReadKey();
break;
case "/":
if (num2 == 0)
{
Console.WriteLine("You cannot devide by zero, please select a different number.");
num2 = Convert.ToInt32(Console.ReadLine());
}
else if (num1 == 0)
{
Console.WriteLine("You cannot devide zero, please select a different number.");
num1 = Convert.ToInt32(Console.ReadLine());
}
else
{
answer = num1 / num2;
Console.WriteLine(num1 + " / " + num2 + " = " + answer);
Console.ReadKey();
}
break;
}
}
}
}
}
you really only need to check the second value (num2). You'd want to evaluate immediately after the read. How you handle it is up to you. You could loop with a while indefinitely, but you'd also want validate (as with any of these inputs), and give the user an option to quit if they can't understand the directions to input.
A possible solution is using while, see below:
case "/":
while (num2 == 0)
{
Console.WriteLine("You cannot devide by zero, please select a different number.");
num2 = Convert.ToInt32(Console.ReadLine());
}
answer = num1 / num2;
Console.WriteLine(num1 + " / " + num2 + " = " + answer);
Console.ReadKey();
break;
However, you should think about change to use int.TryParse method (https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx), as if the user type something that is not a number, Convert.ToInt32 is going to throw a FormatException (https://msdn.microsoft.com/en-us/library/sf1aw27b%28v=vs.110%29.aspx).
I think the cleanset sollution is to extract some methods (select the code and right click with mouse->refactoring->extractMethod).
one of the functions should be "getSecondNumber"
inside that function you start with "enter second number or click "q" to quit.
end you end with if Int.TryParse results with false, or number==0 call your function again (and you'll come to the starting position of "enter second number..."
good luck

Errors appearing in code [closed]

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 7 years ago.
Improve this question
Help, I don't know what this error is
Console.WriteLine (num1 + " + " + num2 " = " answer);
This returns four errors (im using monodevelop)
) expected
; expected
; expected (yes, it appear twice)
Invalid expression term ')'
And three more similar lines got the same errors
Here is the whole code
using System;
namespace CMD_test
{
class MainClass
{
public static void Main (string[] args)
{
Start:
double num1;
double num2;
double answer;
Console.WriteLine ("What operation shall we use? Type:");
Console.WriteLine ("A - for addition");
Console.WriteLine ("S - for subtraction");
Console.WriteLine ("M - for multiplication");
Console.WriteLine ("D - for division");
KeyPress:
string key = Console.Read ();
if (key == "A") {
Console.WriteLine ("You chose addition");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 + num2;
Console.WriteLine (num1 + " + " + num2 " = " answer);
}
if (key == "S") {
Console.WriteLine ("You chose subtraction");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 - num2;
Console.WriteLine (num1 + " - " + num2 " = " answer);
}
if (key == "M") {
Console.WriteLine ("You chose multiplication");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 * num2;
Console.WriteLine (num1 + " * " + num2 " = " answer);
} else if (key == "D") {
Console.WriteLine ("You chose division");
Console.Write ("What is the first number? ");
num1 = Convert.ToDouble (Console.ReadLine ());
Console.Write ("What is the second number? ");
num2 = Convert.ToDouble (Console.ReadLine ());
answer = num1 / num2;
Console.WriteLine (num1 + " / " + num2 " = " answer);
} else {
Console.WriteLine ("You pressed a wrong button! Please retry.");
goto KeyPress;
}
Console.Write ("Do you want to do another operation? Y/N: ");
string restart = Console.ReadLine ();
if (restart == "Y") {
Console.Clear ();
goto Start;
} else if (restart == "N") {
Console.WriteLine ("Press any key to exit");
Console.ReadKey ();
}
}
}
}
and maybe some more other errors i dont know
Your expression
Console.WriteLine (num1 + " + " + num2 " = " answer);
isn't valid as you have missed a couple of +'s off your expression. Change it to:
Console.WriteLine (num1 + " + " + num2 + " = " + answer);
and it'll compile correctly. Better yet, do this and you'll avoid such typos:
Console.WriteLine ("{0} + {1} = {2}", num1, num2, answer);
Oh and as an aside, as and when you switch to C# 6, you'll be able to do this, which simplifies things even more:
Console.WriteLine ("{num1} + {num2} = {answer}");
Console.WriteLine (num1 + " + " + num2 " = " answer);
should actually be
Console.WriteLine (num1 + " + " + num2 + " = " + answer);
You missed two plus signs. That's why it is generating a error.
Your IDE has told you where the problem actually is.
Moreover, it is not a good practice to concatenate strings this way. Console.WriteLine lets you use string formatting:
Console.WriteLine ("{0} + {1} = {2}", num1, num2, answer);
Read about composite formatting here:
https://msdn.microsoft.com/en-us/library/txafckwd(v=vs.110).aspx
Here is an example of what you tried to do.

Expected class, delegate, enum, interface or struct

Queston is answered thanks for the help :)
I made a code with multiple methods in a class but when I try to run it it says
Expected class, delegate, enum, interface, or struct
on the two methods that are not the main methods. I read around and found that someone had the same problem and the case was that the methods weren’t in the class. But couldn't figure out how to fix that. Any tips?
PS: I’m pretty new to coding ;)
using System;
namespace Testing
{
public class Calculator
{
public static void Main (string[] args )
{
string answer;
Console.WriteLine ("Would you like to divide or multiply? Type your choice.");
Start:
answer = (Console.ReadLine ());
if (answer == "multiply")
{
MultiplyingMethod;
}
{
else if (answer == "divide")
{
DividingMethod;
}
}
else
{
Console.WriteLine ("Please type multiply or divide.");
goto Start;
}
}
}
public static void DividingMethod ()
{
double num01;
double num02;
Console.Write ("Enter a number to be divided");
num01 = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Enter a number to divide by");
num02 = Convert.ToInt32 (Console.ReadLine ());
Console.WriteLine (num01 + " divided by " + num02 + " is equal to " + num01 / num02);
Console.WriteLine ("");
Console.ReadKey ();
}
public static void MultiplyingMethod ()
{
double num01;
double num02;
Console.Write ("Enter a number to be multiplied");
num01 = Convert.ToInt32 (Console.ReadLine ());
Console.Write ("Enter a numeber to multiply by");
num02 = Convert.ToInt32 (Console.ReadLine ());
Console.WriteLine (num01 + " multiplied by " + num02 + " equals " + num01 * num02);
Console.WriteLine ("");
Console.ReadKey ();
}
}
}
Working version:
public static void Main(string[] args)
{
string answer;
Console.WriteLine("Would you like to divide or multiply? Type your choice.");
while (true)
{
answer = (Console.ReadLine());
if (answer == "multiply")
{
MultiplyingMethod();
break;
}
else if (answer == "divide")
{
DividingMethod();
break;
}
else
{
Console.WriteLine("Please type multiply or divide.");
}
}
}
public static void DividingMethod()
{
Console.Write("Enter a number to be divided");
double num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a number to divide by");
double num02 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num01 + " divided by " + num02 + " is equal to " + num01/num02);
Console.WriteLine("");
Console.ReadKey();
}
public static void MultiplyingMethod()
{
double num01;
double num02;
Console.Write("Enter a number to be multiplied");
num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a numeber to multiply by");
num02 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num01 + " multiplied by " + num02 + " equals " + num01*num02);
Console.WriteLine("");
Console.ReadKey();
}
Call a method with no parameters like MultiplyingMethod(); instead of MultiplyingMethod;. Thats no valid C#
Please don't use goto. It makes your code messy. Take a look at loops
You should call the methods with ().
Write MultiplyingMethod(); instead of MultiplyingMethod(); and
DividingMethod(); instead of DividingMethod;
public static void Main(string[] args)
{
string answer;
Console.WriteLine("Would you like to divide or multiply? Type your choice.");
Start:
answer = (Console.ReadLine());
if (answer == "multiply")
{
MultiplyingMethod();
}
else if (answer == "divide")
{
DividingMethod();
}
else
{
Console.WriteLine("Please type multiply or divide.");
goto Start;
}
}
public static void DividingMethod()
{
double num01;
double num02;
Console.Write("Enter a number to be divided");
num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a number to divide by");
num02 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num01 + " divided by " + num02 + " is equal to " + num01 / num02);
Console.WriteLine("");
Console.ReadKey();
}
public static void MultiplyingMethod()
{
double num01;
double num02;
Console.Write("Enter a number to be multiplied");
num01 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter a numeber to multiply by");
num02 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(num01 + " multiplied by " + num02 + " equals " + num01 * num02);
Console.WriteLine("");
Console.ReadKey();
}
In addition, never use goto, it is not good!
There are several issues with this code:
Calling functions should be done with the syntax: funcname(params). So DividingMethod; becomes DividingMethod();
Try not to use goto (it sounds easy but makes your code really hard to read and debug)
Too many brackets
if (answer == "multiply")
{
MultiplyingMethod;
}
{
else if (answer == "divide")
{
DividingMethod;
}
}
Should be:
if (answer == "multiply")
{
MultiplyingMethod();
}
else if (answer == "divide")
{
DividingMethod();
}

Categories

Resources