Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System.Linq.Expressions;
using System;

namespace MyFirstProgram
{
    class Program
    {
        static void Main(string[] args)
        {

            bool again = true;

            while (again == true)
            {
                Console.WriteLine("Type plus, minus, division or into");
                var b = (Console.ReadLine());

                if (b == ("plus") || b == ("Plus") || b == ("PLUS"))
                {
                    try
                    {
                        Console.Write("Type number 1: ");
                        int p1 = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Type number 2: ");
                        int p2 = Convert.ToInt32(Console.ReadLine());

                        Console.Write("The answer is: ");
                        Console.WriteLine(p1 + p2);
                    }
                    catch { Exception e; };
                    {
                        Console.WriteLine("You cant do math with words");
                    }
                }
                else if (b == ("minus") || b == ("Minus") || b == ("MINUS"))
                {
                    try
                    {
                        Console.Write("Type number 1: ");
                        int m1 = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Type number 2: ");
                        int m2 = Convert.ToInt32(Console.ReadLine());

                        Console.Write("The answer is: ");
                        Console.WriteLine(m1 - m2);
                    }
                    catch{FormatException e;}
                    {
                        Console.WriteLine("You cant do math with words");
                    }
                    catch{DivideByZeroException e;}
                    {
                        Console.WriteLine("You can't divide with zero");
                    }
                }
                else if (b == ("division") || b == ("Division") || b == ("DIVISION"))
                {
                    try
                    {
                        Console.Write("Type number 1: ");
                        int d1 = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Type number 2: ");
                        int d2 = Convert.ToInt32(Console.ReadLine());

                        Console.Write("The answer is: ");
                        Console.WriteLine(d1 / d2);
                        Console.Write("The remainder is: ");
                        Console.WriteLine(d1 % d2);
                    }
                    catch{ Exception e;}
                    {
                        Console.WriteLine("Something went wrong");
                    }
                    
                }
                else if (b == ("Into") || b == ("into") || b == ("INTO"))
                {
                    try
                    {
                        Console.Write("Type number 1: ");
                        int mi1 = Convert.ToInt32(Console.ReadLine());
                        Console.Write("Type number 2: ");
                        int mi2 = Convert.ToInt32(Console.ReadLine());

                        Console.Write("The answer is: ");
                        Console.WriteLine(mi2 * mi1);
                    }
                    catch { FormatException e; }
                    {
                        Console.WriteLine("You cant do math with words");
                    }
                }
                else
                {
                    again = true;
                }
                Console.ReadKey();
            }
        }
    }
}


What I have tried:

I don't see any missing curly brackets. please help me.
Posted
Updated 10-Jun-23 6:20am
v2

here:
catch { Exception e; };

too many semicolons.

For all, change to:
catch (Exception e)
 
Share this answer
 
Here:
C#
catch { Exception e; };
Should be:
C#
catch ( Exception e )
You'll have to do the same for all your other catch blocks.

But ... You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 
v2
Comments
mahamuda shanta 11-Jun-23 9:00am    
THANK YOU SO MUCH I APPRECIATE IT!!!!!
OriginalGriff 11-Jun-23 9:33am    
You're welcome!
Why do you have parenthesis around every string in your if condition expressions? That does nothing and is not required.

Instead of checking for three possible capitalizations of each word you're looking for, just convert the entered string to lowercase and check against the lowercase version of the string.
C#
Console.WriteLine("Type plus, minus, division or into");
var b = Console.ReadLine();
b = b.ToLower();

if (b == "plus")
...
 
Share this answer
 
Comments
OriginalGriff 10-Jun-23 13:47pm    
I take it Mr "I AM NOT A TEACHER" has been stalking you as well ... countered here.
Dave Kreskowiak 11-Jun-23 1:22am    
Thanks. If he's "not a teacher", why is he answering questions? I don't really want to know. It's just a curiosity.
mahamuda shanta 11-Jun-23 9:03am    
Thanks for this. I also appreciate this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900