c# - Overload Method Error with Switch statement -
the application shall perform selected menu math operation on 2 entered numbers , display answer.you must use methods each math operation , decision must used pick method should executed. application shall display error message when correct menu number not entered. decision 1-5 valid selections , else “invalid”. application shall not allow division zero. not perform divide operation if second number variable zero, instead display error message.
i have written code program showing following error: 1. no overload method addnumber takes 0 arguments. error showing math operation methods inside switch statements. 2. dividenumber method - not code paths return value
class program { static void main(string[] args) { double n1 = convert.todouble(console.readline()); //string n2 = ""; double n2 = convert.todouble(console.readline()); console.clear(); console.writeline("simple calculator"); console.writeline("\t 1) add"); console.writeline("\t 2) subtract"); console.writeline("\t 3) multiply"); console.writeline("\t 4) divide"); console.writeline("\t 5) quit"); console.write("enter selection: "); int menuselection =0; // double total; // menuselection = console.readline(); switch (menuselection) { case 1: // total = convert.toint32(addnumbers("results:")); console.writeline(addnumbers(n1,n2)); break; case 2: console.writeline(subtractnumber(n1,n2)); break; case 3: console.writeline(multiplynumber(n1,n2)); break; case 4: console.writeline(dividenumber(n1,n2)); break; case 5: break; default: console.writeline("invalid selection !"); console.readline(); return; } //ask user enter 2 numbers console.writeline(); console.write("enter number 1:", n1); // number1 = console.readline(); console.write("enter number 2:", n2); // number2 = console.readline(); } public static double addnumbers(double number1, double number2) { return number1 + number2; } public static double subtractnumber(double number1, double number2) { return number1 - number2; } public static double multiplynumber(double number1, double number2) { return number1 * number2; } public static double dividenumber(double number1, double number2) { if (number1 == 0 && number2 == 0) { console.writeline("cannot divide zero. try again"); return 0; } else { return number1 / number2; } }
1st mistake taking input after calling switch cases
2nd mistake function have 2 parameters pass them.
like dividenumber(number1,number2)
in switch case
hope understand
one thing notice taking input in string
, function takes double parameters
need convert string double.
give idea use double take input double n1 = convert.todouble(console.readline());
input console string.
Comments
Post a Comment