+ 1

C# Coding question

Hello, everyone. I'm new to C# and am happy to be learning the language. I'm teaching myself and running into an error with solving a practice problem. Can you tell me why the error is occurring? I can solve for basic output of text and conduct arithmetic operators but I don't understand this problem. Question: Write a C# Sharp program to print the output of multiplication of three numbers which will be entered by the user: Test Data: Input first number to multiply: 2 Input second number to multiply: 3 Input third number to multiply: 6 Expected Output: 2X3X6=36 Here is my code below to solve for it. I would really appreciate some help. Thank you. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void sayHi() { Console.WriteLine("Hello"); Console.WriteLine(-1+4*6); Console.WriteLine((35+5)%7); Console.WriteLine(14+-4*6/11); Console.WriteLine(2+15/6*1-7%2); int 2, 3, 6 Console.Write("2"); num2=Convert.ToInt32(Console.ReadLine()); Console.Write("3"); num3=Convert.ToInt32(Console.ReadLine()); Console.Write("6"); num6 = Convert.ToInt32(Console.ReadLine()); int result = 2 * 3 * 6; Console.WriteLine("Output: {0} x {1} x {2} = {3}", num1, num2, num3, result); } static void Main(string[] args) { sayHi();

20th Jul 2018, 12:19 PM
Alex
Alex - avatar
4 Answers
+ 5
Hey Alex, There are quite a few issues. I fixed up your code on my end, so compare/contrast it to see what you did wrong. If you have questions at that point, come back and ask; I'll explain anything you need me to. https://code.sololearn.com/cTLrYtkxoFS0/#cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void multiplyThreeInt() { int num1, num2, num3, product; Console.WriteLine("Please input your numbers: "); num1 = Convert.ToInt32(Console.ReadLine()); num2 = Convert.ToInt32(Console.ReadLine()); num3 = Convert.ToInt32(Console.ReadLine()); product = num1 * num2 * num3; Console.WriteLine("Output: {0} x {1} x {2} = {3}", num1, num2, num3, product); } static void Main(string[] args) { multiplyThreeInt(); } } }
20th Jul 2018, 12:41 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
@Alex Sorry about the delay. I'm at work right now. As Gevorg mentioned, you mis-pasted and there is half of a "main loop" toward the top that you need to remove. static void Main(string[] args) { Console.WriteLine("Hello World!"); ^Remove that from your code above so it reflects what I posted to you. Also, when you run the program and it pops up with that box, that's the box to enter your input so it can feed you back appropriate output of your program. Unfortunately, you must enter ALL of your required inputs at the same time in the beginning because of the nature of how it compiles it from their server and feeds you just the output. So in this scenario, you'll want to enter THREE numbers separated by a new line. EXAMPLE FOR INPUT BOX: 2 3 6 ^That'll give it all three of the required inputs. I want to point something out to you regarding this scenario because there is a lot of potential for the program itself to crash out. When you're receiving the user's input, you're also converting the input string to an int. What if the user gives invalid input values? When it goes to convert, it'll crash. This is where TRY/CATCH blocks come in handy; it'll catch the exception and allow you to address it instead of crashing. EXAMPLE: static void multiplyThreeInt() { int num1, num2, num3, product; Console.WriteLine("Please input your numbers: "); try { num1 = Convert.ToInt32(Console.ReadLine()); num2 = Convert.ToInt32(Console.ReadLine()); num3 = Convert.ToInt32(Console.ReadLine()); product = num1 * num2 * num3; Console.WriteLine("Output: {0} x {1} x {2} = {3}", num1, num2, num3, product); } catch { Console.WriteLine("ERROR: Please input valid numbers only."); } } https://code.sololearn.com/cTLrYtkxoFS0/#cs :::INPUT::: 2 F 6 :::OUTPUT::: Please input your numbers: ERROR: Please input valid numbers only.
20th Jul 2018, 3:14 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 1
Thanks, Fatal Err0r. I really appreciate your help and your patience. I'm very new to C#. So, I reset the console with SoloLearn and inputted your modified code and got a read line screen pop up. I inputted the number 2 and received an error compiling. I included everything below. I included the error received as well. What am I doing wrong? using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SoloLearn { class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); static void multiplyThreeInt() { int num1, num2, num3, product; Console.WriteLine("Please input your numbers: "); num1 = Convert.ToInt32(Console.ReadLine()); num2 = Convert.ToInt32(Console.ReadLine()); num3 = Convert.ToInt32(Console.ReadLine()); product = num1 * num2 * num3; Console.WriteLine("Output: {0} x {1} x {2} = {3}", num1, num2, num3, product); } static void Main(string[] args) { multiplyThreeInt(); } } } ..\Playground\(15,13): error CS0106: The modifier 'static' is not valid for this item ..\Playground\(26,9): error CS0106: The modifier 'static' is not valid for this item ..\Playground\(31,2): error CS1513: } expected
20th Jul 2018, 1:15 PM
Alex
Alex - avatar
+ 1
You have 2 "Main" methods in you code. When you run application, the Main method is starting point, you should define your function "multiplyThreeInt()" in "Program" class, and then call it from Main method "Fata1 Err0r" is wrote everything correct, watch carefully and don't be in a hurry
20th Jul 2018, 1:22 PM
Gevorg Baghdasaryan
Gevorg Baghdasaryan - avatar