this is basically a dumbed down version of what I want to accomplish.
I am trying to tell the program to pass the two methods from different functions to the final one but it says they do not exist in the current context even though I've tried to pass them in Num1 and Num2
static void Main(String[] args)
{
int Option;
DisplayMenu();
Option = GetUserOption();
while (Option != 0)
{
switch (Option)
{
case 1:
Num1();
break;
case 2:
Num2();
break;
case 3:
Overall(Num3, Num4);
break;
}
}
}
static void DisplayMenu()
{
Console.WriteLine("1. Num1 2.Num2 3.Overall");
}
static int GetUserOption()
{
int Option;
Console.WriteLine("Pick choice");
Option = Convert.ToInt32(Console.ReadLine());
return Option;
}
static int Num1()
{
int Num3;
Console.WriteLine("Enter your first number");
Num3 = Convert.ToInt32(Console.ReadLine());
return Num3;
}
static int Num2()
{
int Num4;
Console.WriteLine("Enter your second number");
Num4 = Convert.ToInt32(Console.ReadLine());
return Num4;
}
public static int Overall(int Num3, int Num4)
{
int Overall;
Console.WriteLine("This will add the two together");
Overall = Overall + Num3 + Num4;
Console.WriteLine(Overall);
return Overall;
}
It is still telling me that the params don't exist in the current context.
EDIT: Moved the params into Overall case but still the same error message.
I can see a handful of problems:
In Main, you haven't declared the variables Num3 and Num4, so the compiler is unhappy that you're trying to pass them to the Overall method. You should declare them before your loop (and initialise them to some default value).
iNum3 and iNum4 don't exist in the Overall method. You've named the arguments as Num3 and Num4.
You've declared a variable called Overall in the method Overall but haven't initialised it, so when you try to do Overall + iNum3 + iNum4, the variable doesn't yet have a value. It's simpler to write int Overall = Num3 + Num4.
First off thats not how you would do such a thing its very convoluted and doesnt make sense (took me a while to figure out what you are doing). I still dont exactly know if you are trying to do what i am thinking.
Anyways my guess is you want to add two numbers together and give the user a option at every step if he wants to enter number 1, number 2 or show the current result? Here is a solution where i tryed to keep your style(incase you are learing about switch and function) while maintaining the code. Its still way to "complex" for whats it doing
static void Main(String[] args)
{
int option = 1;
int num1 = 0;
int num2 = 0;
int sum = 0;
DisplayMenu();
while (option != 0)
{
option = GetUserOption();
switch (option)
{
case 1:
num1 = getNum(option);
break;
case 2:
num2 = getNum(option);
break;
case 3:
Overall(num1, num2, sum);
break;
}
}
}
static void DisplayMenu()
{
Console.WriteLine("1: Num1, 2: Num2, 3: Overall, 0: Exit");
}
static int GetUserOption()
{
Console.WriteLine("Pick choice");
return Convert.ToInt32(Console.ReadLine());
}
static int getNum(int option)
{
if (option == 1)
Console.WriteLine("Enter your first number");
else
Console.WriteLine("Enter your second number");
return Convert.ToInt32(Console.ReadLine());
}
static int Overall(int num1, int num2, int sum)
{
Console.WriteLine("This will add the two together");
sum = num1 + num2;
Console.WriteLine(sum);
return sum;
}
The problem lies here where the compiler does not know which Overall do you mean:
static int Overall(int Num3, int Num4)
{
int Overall;
Console.WriteLine("This will add the two together");
Overall = Overall + iNum3 + iNum4;
Console.WriteLine(Overall);
return overall;
}
make the inner-scoped Overall in small case overall
static int Overall(int Num3, int Num4)
{
int overall;
Console.WriteLine("This will add the two together");
overall= overall + iNum3 + iNum4;
Console.WriteLine(overall);
return Overall;
}
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(String[] args)
{
int Option;
int Num3, Num4;
Num3=1;
Num4 = 3;
DisplayMenu();
Option = GetUserOption();
while (Option != 0)
{
switch (Option)
{
case 1:
Num1();
break;
case 2:
Num2();
break;
case 3:
Overall(Num3, Num4);
break;
}
}
}
static void DisplayMenu()
{
Console.WriteLine("1. Num1 2.Num2 3.Overall");
}
static int GetUserOption()
{
int Option;
Console.WriteLine("Pick choice");
Option = Convert.ToInt32(Console.ReadLine());
return Option;
}
static int Num1()
{
int Num3;
Console.WriteLine("Enter your first number");
Num3 = Convert.ToInt32(Console.ReadLine());
return Num3;
}
static int Num2()
{
int Num4;
Console.WriteLine("Enter your second number");
Num4 = Convert.ToInt32(Console.ReadLine());
return Num4;
}
public static int Overall(int Num3, int Num4)
{
int Overall=0;
Console.WriteLine("This will add the two together");
Overall = Overall + Num3 + Num4;
Console.WriteLine(Overall);
return Overall;
}
}
}
Related
I'm relatively new to programming and I'm working on various projects to get better at it. One is a calculator that I want to be able to solve more than just basic addition and subtraction. Currently, all I have is the basics with the quadratic formula. But my design weighs on user input to decide what to do. It prompts the user with "What would you like to do? Add: Sub: Div: Mul: Quad Equation:" After typic in "Add" the code operates normally but if I type in anything else like "Sub" or "Div". it does nothing. doesn't even spit back an error. As far as I can tell, my code is fine (well it's terrible code but it should work nonetheless) But I just do not know how to proceed.
using System;
namespace Better_Calculator
{
class Program
{
static void Main(string[] args)
{
double num01;
double num02;
double a;
double b;
double c;
System.Console.WriteLine("Welcome to how you are going to cheat through math lol");
System.Console.WriteLine("What would you like to do? \nAdd: \nSub: \nDiv: \nMul: \nQuad Equation: ");
if (Console.ReadLine() == "Add")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Add(num01, num02);
}
else if (System.Console.ReadLine() == "Sub")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Sub(num01, num02);
}
else if (System.Console.ReadLine() == "Mul")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Mul(num01, num02);
}
else if (System.Console.ReadLine() == "Div")
{
System.Console.WriteLine("what is the first number?");
num01 = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is the second number?");
num02 = Convert.ToInt32(Console.ReadLine());
Div(num01, num02);
}
else if (System.Console.ReadLine() == "Quad Equation")
{
System.Console.WriteLine("what is a?");
a = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is b");
b = Convert.ToInt32(Console.ReadLine());
System.Console.WriteLine("what is c");
c = Convert.ToInt32(Console.ReadLine());
Quad(a, b, c);
}
static void Add(double num01, double num02)
{
string answer = Convert.ToString(num01 + num02);
System.Console.WriteLine(answer);
}
static void Sub(double num01, double num02)
{
string answer = Convert.ToString(num01 - num02);
System.Console.WriteLine(answer);
}
static void Mul(double num01, double num02)
{
string answer = Convert.ToString(num01 * num02);
System.Console.WriteLine(answer);
}
static void Div(double num01, double num02)
{
string answer = Convert.ToString(num01 / num02);
System.Console.WriteLine(answer);
}
static void Quad(double a, double b, double c)
{
double bNeg = b * -1;
double bSqr = b * b;
double SqR = Math.Sqrt(bSqr - (4 * a * c));
double solvedAdd = (bNeg + SqR) / (2 * a);
double solvedSub = (bNeg - SqR) / (2 * a);
string answer = Convert.ToString(solvedAdd) + " or " + Convert.ToString(solvedSub);
System.Console.WriteLine(answer);
}
}
}
}
With every ´if´, you call Console.ReadLine() again, so if you enter "Quad Equation", your code has passed 5 Readlines until you reach the code that does Quad Equation.
Solution: Do only one Console.ReadLine() and put its result into a variable:
var userInput = Console.ReadLine();
and then, test against userInput in your if statements (ex.)e:
else if (userInput == "Sub")
You only need to do a ReadLine once and store the value, then use that to compare. For example:
var operation = Console.ReadLine();
if(operation == "Add")
{
// Do add stuff
}
But you should consider using a switch statement instead:
switch(operation)
{
case "Add":
// do Add Stuff
break;
case "Sub":
// do Add Stuff
break;
case "Mul":
// do Add Stuff
break;
// etc...
}
When we get input from console application from user may be they given as
CAPS, Small or combination of both
. In that situation you can use the following approach,
string value=Console.ReadLine();
if(string.Equals(value, "Add", StringComparison.CurrentCultureIgnoreCase))
{
// Do add stuff
}
else if(string.Equals(value, "Sub", StringComparison.CurrentCultureIgnoreCase))
{
// Do add stuff
}
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.
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'm new to c# and am trying to make a simple calculator.
It works fine until it goes back to the start to take a new number.
When taking the new number it says it cant convert the user input to a integer.
using System;
namespace simpleCalculator
{
class MainClass
{
public static void Main(string[] args)
{
start:
Console.Clear();
Console.WriteLine("Enter first number");
int x = Convert.ToConsole.ReadLine();
Console.WriteLine("Would you like to\n1. Add\n2. Multiply\n3. Devide");
string o = Console.ReadLine();
if (o == "1")
{
Console.WriteLine("Enter second number\n");
int y = Convert.ToInt32(Console.ReadLine());
add(temp, y);
goto start;
Console.Clear();
goto start;
}
}
public static void add(int num01, int num02)
{
Console.Clear();
Console.WriteLine((num01 + num02) + "\nPress enter to contiue.");
Console.Read();
}
}
}
Use TryParse so if the parsing fails, you will not get an exception.
var enteredValue = Console.ReadLine();
var parsedValue;
if (!int.TryParse(enteredValue, out parsedValue))
{
// parse failed do whatever you want
}
Do that for both entries and if both of them pass, then call your add method.
You're looking for int.Parse(). Be careful to validate your input. You should probably create an escape condition.
Edited to show an alternative solution
Edited to be more explicit on how to handle some input
class Program
{
public static void Main(string[] args)
{
string input = String.Empty;
int x = 0, y = 0;
while (true)
{
try
{
Console.WriteLine("Enter first number");
x = int.Parse(Console.ReadLine());
Console.WriteLine("Would you like to\n1. Add\n2. Multiply\n3. Divide");
input = Console.ReadLine();
Console.WriteLine("Please enter a second number");
y = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("Invalid input");
continue;
}
switch (input)
{
case "1":
Console.WriteLine($"{x} + {y} = " + add(x, y));
break;
case "2":
//TODO implement multiply case
break;
case "3":
//TODO implement divide case
break;
default:
Console.WriteLine("Invalid input");
break;
}
}
}
public static int add(int x, int y) => x + y;
}
Try this:
int numbers = Convert.ToInt32("1234");
using System;
namespace Myprog
{
class Myprog
{
int num1;
int num2;
public void Getdata()
{
Console.WriteLine("enter two numbers");
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
}
public void Showdata()
{
Console.WriteLine("First value {0} Second value {1}",num1,num2);
}
static void Main(string[] args)
{
Myprog C=new Myprog();
C.Getdata();
C.Showdata();
}
}
}
=>in this program always assign value 0, please give me suggestion how to solve this program and I want to run to this program in this manner.
Thank you.
Your num1 and num2 as a fields are completely different your num1 and num2 as a local variables.
You can assign those variables into your fields in your Getdata method like;
this.num1 = num1;
this.num2 = num2;
like;
int num1;
int num2;
public void Getdata()
{
Console.WriteLine("enter two numbers");
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
this.num1 = num1;
this.num2 = num2;
}
public void Showdata()
{
Console.WriteLine("First vale {0} Second value {1}",num1,num2);
}
static void Main(string[] args)
{
Myprog C=new Myprog();
C.Getdata();
C.Showdata();
}
In the Getdata() method you define new local variables named num1 and num2 and assign values to them.
Remove the int from each line to assign values to fields num1 and num2 instead of creating locals.
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());