Hello:)
This program is a maths calculator that should return to the menu so you can go round again when you have done one calculation. After great despair I think I have somehow got most of it to work?
However if I input something Invalid, say the number 8 it just asks to input the first number for a calculation again. I want it to do whatever is appropriate for default. I have text in there now but it doesn't output! I have been studying Java basics and am now starting on C# but don't seem to be the sharpest knife in the drawer where coding is concerned despite alot of effort so would be grateful for any help, advice, knowledge, and probably pschiatric care if I don't improve at this coding business soon. Here's the code - thanks for looking.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mathsApp_Task_1._1
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            int calChoi = 0;


            while (calChoi != 5)
            {//start while loop
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Hello, please choose from the following calculations");
                Console.WriteLine("(1) Add  (2) Subtract (3) Multiply (4) Divide or (5) Exit");
                Console.WriteLine();
                Console.WriteLine();
                calChoi = int.Parse(Console.ReadLine());
                counter += 1;//increment loop by one


                if (calChoi == 5)
                {
                    Console.WriteLine(" Goodbye  and thanks for using this program - see you next time");
                    Console.WriteLine();
                    Console.WriteLine();
                }

                else
                {//start else - don't know if the loop should be in here? But it seems to work

                    Console.WriteLine("Please enter the first number ");
                    float num1 = float.Parse(Console.ReadLine());

                    Console.WriteLine("Please enter the second number");
                    float num2 = float.Parse(Console.ReadLine());

                    MathsCalculator MUtil;
                    MUtil = new MathsCalculator();

                    float answer = 0;

                    switch (calChoi)
                    {
                        case 1:
                            answer = MUtil.Added(num1, num2);
                            Console.Write(num1 + " added to " + num2 + " is " + answer);
                            break;
                        case 2:
                            answer = MUtil.Subtracted(num1, num2);
                            Console.Write(num1 + " taken from " + num2 + " is " + answer);
                            break;
                        case 3:
                            answer = MUtil.Multiplied(num1, num2);
                            Console.Write(num1 + " multiplied by " + num2 + " is " + answer);
                            break;
                        case 4:
                            answer = MUtil.Divided(num1, num2);
                            Console.Write(num1 + " divided by " + num2 + " is " + answer);
                            break;

                        default:

                            Console.WriteLine(" Invalid input I'm afraid- please choose 1 - 5 ");
                            break;


                    }


                }//Finish while loop
            }//end else after the if statement up there


            Console.ReadLine();

        }
                class MathsCalculator
                {
                    //This class contains four methods/functions
                    //Add, Subtract, Multiply and Divide

                    //Add

                    public float Added(float x, float y)
                    {
                        return x + y;
                    }
                    public float Subtracted(float x, float y)
                    {
                        return x - y;
                    }
                    public float Multiplied(float x, float y)
                    {
                        return x * y;
                    }
                    public float Divided(float x, float y)
                    {
                        return x / y;
                    }

        }

    }
}

Recommended Answers

All 4 Replies

First, the closing tag has a / as the first character (for your code block) :)

Second, when I run the code and tell it I want to do calculation 8 I get the Invalid input line. So I guess I don't understand what problem you are having :)

commented: Helpfull - thanks +0
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            int calChoi = 0;


            while (calChoi != 5  )
            {//start while loop
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Hello, please choose from the following calculations");
                Console.WriteLine("(1) Add (2) Subtract (3) Multiply (4) Divide or (5) Exit");
                Console.WriteLine();
                Console.WriteLine();
                calChoi = int.Parse(Console.ReadLine());
                counter += 1;//increment loop by one


                if (calChoi == 5)
                {
                    Console.WriteLine(" Goodbye and thanks for using this program - see you next time");
                    Console.WriteLine();
                    Console.WriteLine();
                }

                else if((calChoi<5) &&(calChoi>0))
                {//start else - don't know if the loop should be in here? But it seems to work

                    Console.WriteLine("Please enter the first number ");
                    float num1 = float.Parse(Console.ReadLine());

                    Console.WriteLine("Please enter the second number");
                    float num2 = float.Parse(Console.ReadLine());

                    MathsCalculator MUtil;
                    MUtil = new MathsCalculator();

                    float answer = 0;

                    switch (calChoi)
                    {
                        case 1:
                            answer = MUtil.Added(num1, num2);
                            Console.Write(num1 + " added to " + num2 + " is " + answer);
                            break;
                        case 2:
                            answer = MUtil.Subtracted(num1, num2);
                            Console.Write(num1 + " taken from " + num2 + " is " + answer);
                            break;
                        case 3:
                            answer = MUtil.Multiplied(num1, num2);
                            Console.Write(num1 + " multiplied by " + num2 + " is " + answer);
                            break;
                        case 4:
                            answer = MUtil.Divided(num1, num2);
                            Console.Write(num1 + " divided by " + num2 + " is " + answer);
                            break;

                        //default:

                        //    Console.WriteLine(" Invalid input I'm afraid- please choose 1 - 5 ");
                        //    break;


                    }


                }//Finish while loop
                else Console.WriteLine(" Invalid input I'm afraid- please choose 1 - 5 ");
            }//end else after the if statement up there


            Console.ReadLine();

        }
        class MathsCalculator
        {
            //This class contains four methods/functions
            //Add, Subtract, Multiply and Divide

            //Add

            public float Added(float x, float y)
            {
                return x + y;
            }
            public float Subtracted(float x, float y)
            {
                return x - y;
            }
            public float Multiplied(float x, float y)
            {
                return x * y;
            }
            public float Divided(float x, float y)
            {
                return x / y;
            }


        }
    }
}
commented: Very useful - thank you +0

First, the closing tag has a / as the first character (for your code block) :)

Second, when I run the code and tell it I want to do calculation 8 I get the Invalid input line. So I guess I don't understand what problem you are having :)

Thank you I appreciate that- I just have to work how to get back in and edit the code block again

I'm not sure why it worked for you ... Thanks again John.

start quote:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {
            int counter = 0;
            int calChoi = 0;


            while (calChoi != 5  )
            {//start while loop
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine("Hello, please choose from the following calculations");
                Console.WriteLine("(1) Add (2) Subtract (3) Multiply (4) Divide or (5) Exit");
                Console.WriteLine();
                Console.WriteLine();
                calChoi = int.Parse(Console.ReadLine());
                counter += 1;//increment loop by one


                if (calChoi == 5)
                {
                    Console.WriteLine(" Goodbye and thanks for using this program - see you next time");
                    Console.WriteLine();
                    Console.WriteLine();
                }

                else if((calChoi<5) &&(calChoi>0))
                {//start else - don't know if the loop should be in here? But it seems to work

                    Console.WriteLine("Please enter the first number ");
                    float num1 = float.Parse(Console.ReadLine());

                    Console.WriteLine("Please enter the second number");
                    float num2 = float.Parse(Console.ReadLine());

                    MathsCalculator MUtil;
                    MUtil = new MathsCalculator();

                    float answer = 0;

                    switch (calChoi)
                    {
                        case 1:
                            answer = MUtil.Added(num1, num2);
                            Console.Write(num1 + " added to " + num2 + " is " + answer);
                            break;
                        case 2:
                            answer = MUtil.Subtracted(num1, num2);
                            Console.Write(num1 + " taken from " + num2 + " is " + answer);
                            break;
                        case 3:
                            answer = MUtil.Multiplied(num1, num2);
                            Console.Write(num1 + " multiplied by " + num2 + " is " + answer);
                            break;
                        case 4:
                            answer = MUtil.Divided(num1, num2);
                            Console.Write(num1 + " divided by " + num2 + " is " + answer);
                            break;

                        //default:

                        //    Console.WriteLine(" Invalid input I'm afraid- please choose 1 - 5 ");
                        //    break;


                    }


                }//Finish while loop
                else Console.WriteLine(" Invalid input I'm afraid- please choose 1 - 5 ");
            }//end else after the if statement up there


            Console.ReadLine();

        }
        class MathsCalculator
        {
            //This class contains four methods/functions
            //Add, Subtract, Multiply and Divide

            //Add

            public float Added(float x, float y)
            {
                return x + y;
            }
            public float Subtracted(float x, float y)
            {
                return x - y;
            }
            public float Multiplied(float x, float y)
            {
                return x * y;
            }
            public float Divided(float x, float y)
            {
                return x / y;
            }


        }
    }
} 

end quote.

Ahhhhh heheheheheheeeee - a few days ago I was trying to do something similar to this else if ((calChoi<5) && (calChoi>0)) - only just else, not as clever, and I forgot the outer brackets and then gave up as it wouldn't let me compile. Now I know why.

Thank you very much:icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.