FYI I made this after a day of getting back to coding (I was a beginner when I stopped coding)

Huyu "The coder"

New member
Joined
Jul 15, 2024
Messages
1
Programming Experience
Beginner
C#:
using System;

namespace tryingcalculator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("------------");
            Console.WriteLine(" Calculator");
            Console.WriteLine("------------");
            Console.WriteLine("");

            Console.WriteLine("Enter your username");
            Console.WriteLine("");

            String username = Console.ReadLine();
            Console.WriteLine("");

            Console.WriteLine("hello " + username);
            Console.WriteLine("");

            Console.WriteLine("do you want to use to use the calculator?");
            Console.WriteLine("");

            String use = Console.ReadLine();
            if (use== "yes") {
                Console.WriteLine("");

                Console.WriteLine("What is the first number of your equation? ");
                Console.WriteLine("");

                int num1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("");

                Console.WriteLine("What is the second number of your equation? ");
              
                Console.WriteLine("");
                int num2 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Choose from this options");
                Console.WriteLine("+");
                Console.WriteLine("-");
                Console.WriteLine("*");
                Console.WriteLine("/");
                String option = Console.ReadLine();
                if (option=="+") {
                    Console.WriteLine(num1 + num2);
                }
              
                Console.WriteLine("");

                if (option == "-")
                {
                    Console.WriteLine(num1 - num2);
                }
                if (option == "*")
                {
                    Console.WriteLine(num1 * num2);
                }
                if (option == "/")
                {                 
                    Console.WriteLine(num1 / num2);
                }
              
                if(use == "no")
                {
                    Console.WriteLine("So why tf did you open the app?!");
                }           
            }
            
            Console.ReadKey();
        }
    }
}
this is coolllllll
 
Last edited by a moderator:
I have formatted your code for you. Please don't post unformatted code snippets as they are too hard to read. I have also removed a bunch of useless empty lines. You should endeavour to make your code as readable as possible.
 
Does it seem reasonable to have an if statement that checks whether use is "no" inside an if block that will only be executed if use is "yes"? That seems to suggest that your testing has been less than complete. Also, did you intend for the user to get no feedback at all if they enter something other than "yes" or "no" with that specific casing? Your app will also crash if the user enters non-numeric data where a number is expected.

It's good that you are excited by seeing something happen on the screen and knowing that the code you wrote made it happen, but there's a bit of work to do on that code before it really warrants being shared. By all means, post back when that work's done.
 
Hello, @Huyu "The coder",
In addition to what @jmcilhinney already told you, you might write some methods to ensure you get what is expected so there wouldn't be an Exception in runtime.
For example, the following method GetNumber() waits for a number (as a string) until you enter one.
GetNumber() method for console App.:
        private static int GetNumber()
        {
            int result;
            bool isValid = true;
            string? entry;

            do
            {
                if (isValid == false)
                    Console.WriteLine("The entered text is not a number. Try again.");
                entry = Console.ReadLine();
                isValid = int.TryParse(entry, out result);
            } while (isValid == false);

            return result;
        }

You can change the return type to decimal to make the calculator more credible.
Another thing I see is that the
if (use == "no")
block is inside the
if (use == "yes")
block, so it will never be run. You may use else or else if, and place that block below the first one.
Anyways, you will get much more coolness if you try the calculator with Windows Forms. But maybe you want to learn more about console app before.
Regards
Pablo
 
Back
Top Bottom