Expected class, delegate, enum, interface or struct - c#

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();
}

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

Running this calculator in loop in console

I am trying to make this console calculator running in loop until the user want to exit the application. How can i use a for loop with if else statements, is it possible?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Oppgave3Lesson1
{
class Program
{
static void Main(string[] args)
{
double sum = 0;
double num1;
Console.WriteLine("First number: ");
num1 = Convert.ToInt32(Console.ReadLine());
double num2;
Console.WriteLine("Second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Addisjon eller Subtraksjon?");
string regneoperasjon = Console.ReadLine();
if(regneoperasjon == "Subtraksjon")
{
Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 - num2));
}
else if (regneoperasjon == "Addisjon")
{
Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 + num2));
}
Console.WriteLine("Do you want to do an another math operation?");
Console.ReadLine();
}
}
}
You can add a question for user via Console.ReadKey method and then check his answer:
do
{
double sum = 0;
double num1;
Console.WriteLine("First number: ");
num1 = Convert.ToInt32(Console.ReadLine());
double num2;
Console.WriteLine("Second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Addisjon eller Subtraksjon?");
string regneoperasjon = Console.ReadLine();
if(regneoperasjon == "Subtraksjon")
{
Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 - num2));
}
else if (regneoperasjon == "Addisjon")
{
Console.WriteLine("Svaret til denne regneoperasjonen er: " + (sum = num1 + num2));
}
Console.WriteLine("Calculate the next operation (Y/N)?");
}
while(Console.ReadKey(true).Key == ConsoleKey.Y);
So you get the output like in the screenshot below:
Try following. I made the writlines a write :
namespace Oppgave3Lesson1
{
class Program
{
static void Main(string[] args)
{
while(true)
{
double sum = 0;
double num1;
Console.Write("First number: (or Exit)");
string firstNumStr = Console.ReadLine();
if(firstNumStr == "Exit") break;
num1 = Convert.ToInt32(firstNumStr);
double num2;
Console.Write("Second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.Write("Addisjon eller Subtraksjon?");
string regneoperasjon = Console.ReadLine();
if(regneoperasjon == "Subtraksjon")
{
Console.Write("Svaret til denne regneoperasjonen er: " + (sum = num1 - num2));
}
else if (regneoperasjon == "Addisjon")
{
Console.Write("Svaret til denne regneoperasjonen er: " + (sum = num1 + num2));
}
}
A for loop is used when you know the (maximum) number of iterations upfront.
In your scenario, a while loop is more apropriate.
do
{
// Perform your logic here
Console.WriteLine("Press any key to continue; Q to quit");
var key = Console.ReadKey();
}while( key != ConsoleKey.Q );
or, use an endless while - loop and break from the loop on a certain condition:
while( true )
{
// Perform your logic here
Console.WriteLine("Press any key to continue; Q to quit");
var key = Console.ReadKey();
if( key == ConsoleKey.Q )
{
break;
}
}

c# Simple score system

I'm making a simple game. I'm trying to give the player a +1 score when he has a answer correct, but It keeps saying the same score 1. I want to have the score constantly updated when your answer is correct.
So if you have two answers correct, the score should update to 2, but it doesn't and keeps saying 1...
start:
Random numbergenerator = new Random ();
int num1 = numbergenerator.Next(1,11);
int num2 = numbergenerator.Next(1,11);
int score = 0; // THIS IS THE SCORE
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if ( answer == num1 * num2) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
}
goto start;
}
}
}
1) From C# specification (MSDN)
Furthermore, a variable initializer in a local variable declaration corresponds exactly to an assignment statement that is inserted immediately after the declaration.
In other words every time your program goes to start assignment statement will init score with 0
i suggest to move the initialization before start (it will be better to move numbergenerator initialization too. ) :
Random numbergenerator = new Random ();
int score = 0; // THIS IS THE SCORE
start:
int num1 = numbergenerator.Next(1,11);
int num2 = numbergenerator.Next(1,11);
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if ( answer == num1 * num2) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
}
goto start;
2) Don't use goto. Because
it makes the code confusing
reduces readability
this article about GO TO can be interesting for you.
i suggest to replace
start with while (true){
goto start; with }
it should looks something like this:
Random numbergenerator = new Random ();
int score = 0; // THIS IS THE SCORE
while(true)
{
int num1 = numbergenerator.Next(1,11);
int num2 = numbergenerator.Next(1,11);
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if ( answer == num1 * num2) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
}
}
3) Extract method, it is not critical, but i suggest to improve readability by extracting a method. This method will contain loop body. The code should looks something like this:
public void TryToGuessMultiplication_GameStep(int num1, int num2)
{
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if ( answer == num1 * num2) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
}
}
...
Random numbergenerator = new Random ();
int score = 0; // THIS IS THE SCORE
while(true)
{
int num1 = numbergenerator.Next(1,11);
int num2 = numbergenerator.Next(1,11);
TryToGuessMultiplication_GameStep(int num1, int num2);
}
4) Your code contains bug If you want to increase score only if aswer is correct you should remove ++score from else block.
5) Do not duplicate the code As you can see the last 2 statements in both if block and else block are the same. I suggest to move them out from if else operator:
public void TryToGuessMultiplication_GameStep(int num1, int num2)
{
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if ( answer == num1 * num2) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
++score; // Gives score
} else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
}
Console.ResetColor();
Console.WriteLine("Your score: " + score);
}
It is not all. Now you can see that
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
is quite similar (but not the same!!!) to
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
and we can't improve our code by moving them out of if else operator. But there is a trick - we can extract method with help of which we will reduce lines of code:
public void PrintUserMessage(ConsoleColor color, string message)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
}
public void TryToGuessMultiplication_GameStep(int num1, int num2)
{
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if ( answer == num1 * num2){
PrintUserMessage( ConsoleColor.Green, "Thats the correct answer!");
++score; // Gives score
}else
PrintUserMessage( ConsoleColor.Red,"Bummer, try again!");
Console.ResetColor();
Console.WriteLine("Your score: " + score);
}
Your score is set to 0 after every jump goto start;. Put your score declaration above start: like below:
int score = 0; // THIS IS THE SCORE
start:
...other instructions
Remember, no matter if you are using goto or while, if variable must store state during a loop, declare and initialize it outside that loop.
You're setting score to 0 every time you goto start.
Can you please stop using goto? It hurts my eyes, and I believe many other eyes as well. Use while instead.
If you use while by the way, the problem will disappear as score will not be reset every iteration.
Don't use goto it is bad practice, you should use loop instead.
Your code should look like:
Random numbergenerator = new Random();
int score = 0; // THIS IS THE SCORE
while (true)
{
int num1 = numbergenerator.Next(1, 11);
int num2 = numbergenerator.Next(1, 11);
Console.WriteLine("Whats " + num1 + " times " + num2 + "?");
var answer = Convert.ToInt32(Console.ReadLine());
if (answer == num1 * num2)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Thats the correct answer!");
Console.ResetColor();
++score; // Gives score
Console.WriteLine("Your score: " + score);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Bummer, try again!");
Console.ResetColor();
Console.WriteLine("Your score: " + score);
}
}
Adding to the other answers, since you only want to increase the score when the user gets the correct answer, you should remove the ++score; line from your else statement, as the way you currently have it, the score will increase either way, regardless if the answer is correct or not.

How do I use Readline input to write a IF/ELSE statement?

I am trying to write a program that accepts users numerical input then output predetermined messages. My question is after I convert the user input from a string to int, how do I use their input in a IF/ELSE statement.
Here's what I have so far:
string UserInput;
Console.Write ("Enter a random number? ");
UserInput =Console.ReadLine();
int x = Convert.ToInt32 (UserInput);
Console.WriteLine (" You entered: " + UserInput);
int x;
if (x < 0)
{
Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
}
{ else if (x >100)
Console.WriteLine (" You are above average");
}
{ else if (x == 100)
Console.WriteLine (" You are average");
}
{
else if (x < 100)
Console.WriteLine (" Sorry but you are below average");
}
The opening parentheses for the else statements should be on the line after the else, not before it. Also your initial if statement needs to check for > 200 too, according to the message printed.
You can also add error checking by using the Int32.TryParse method like this.
string UserInput;
int x;
Console.Write ("Enter a random number? ");
UserInput =Console.ReadLine();
Console.WriteLine (" You entered: " + UserInput);
if (! Int32.TryParse(UserInput, out x))
{
Console.WriteLine ("Invalid data input");
}
else if ((x < 0) || (x>200))
{
Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
}
else if (x >100)
{
Console.WriteLine (" You are above average");
}
else if (x == 100)
{
Console.WriteLine (" You are average");
}
else if (x < 100)
{
Console.WriteLine (" Sorry but you are below average");
}
Let me suggest one way. This way is cleaner and contains error handling. I think you should pass a C# tutorial. Cheers.
public static void Main(string[] args)
{
Console.Write("Enter a random number? ");
string userInput = Console.ReadLine();
Console.WriteLine(" You entered: " + userInput);
try
{
int input = int.Parse(userInput);
PrintMessage(input);
}
catch (Exception)
{
Console.WriteLine(" Error message: Your input is not a number");
}
}
private static void PrintMessage(int input)
{
if (input < 0)
{
Console.WriteLine(" Error message: Out of range: Enter a number between 0 and 200");
}
else if (input > 100)
{
Console.WriteLine(" You are above average");
}
else if (input == 100)
{
Console.WriteLine(" You are average");
}
else
{
Console.WriteLine(" Sorry but you are below average");
}
}
Did you try to input directly in to the function :
if (Convert.ToInt32 (UserInput) < 0)
{
Console.WriteLine (" Error message: Out of range: Enter a number between 0 and 200");
}
else if Convert.ToInt32 (UserInput) >100) {
Console.WriteLine (" You are above average");
}
else if (Convert.ToInt32 (UserInput) == 100) {
Console.WriteLine (" You are average");
}
else if (Convert.ToInt32 (UserInput) < 100) {
Console.WriteLine (" Sorry but you are below average");
}

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.

Categories

Resources