CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2014
    Posts
    17

    Lightbulb C# Console App - Error Check Calculator [Finished]

    Just came back with a finishing update on the error check calculator. Feel free to use the code below or to use as a reference. By the way if anyone can shorten my prototype programming with arrays or anything, share the knowledge, any at all is appreciated.

    Code:
    static void Main()
            {
                string first_input;
                string second_input;
                float inputvalue1 = 0f;
                float inputvalue2 = 0f;
                bool exc1 = true;
                bool exc2 = true;
    
                Console.WriteLine("<This Is A Calulator That Takes 2 Numbered Inputs, Real Or Rational>");
                Console.WriteLine("<It Also Checks If A User Entered A Correct Input>");
                Console.WriteLine("<Press Enter After Entering A Number To Proceed Calculation>");
                Console.WriteLine("\n");
    
                while (exc1)
                {
                    Console.WriteLine("Enter A Number:");
                    first_input = Console.ReadLine();
    
                    if (true == float.TryParse(first_input, out inputvalue1))
                    {
                        exc1 = false;
                    }
                    else
                    {
                        exc1 = true;
                        Console.WriteLine("Error --> You Did Not Enter The First Number Correctly! Try Again");
                        Console.WriteLine("\n");
                    }
                }
    
                while (exc2)
                {
                    Console.WriteLine("Enter The Second Number:");
                    second_input = Console.ReadLine();
    
                    if (true == float.TryParse(second_input, out inputvalue2))
                    {
                        exc2 = false;
                        Console.WriteLine("\n");
                    }
                    else
                    {
                        exc2 = true;
                        Console.WriteLine("Error --> You Did Not Enter The Second Number Correctly! Try Again");
                        Console.WriteLine("\n");
                    }
                }
    
                Console.WriteLine("The 4 Basic Operations Calulate Your Numbers Out As:");
                Console.WriteLine(inputvalue1 * inputvalue2 + " = " + inputvalue1 + " * " + inputvalue2 + " <Multiplied>");
                Console.WriteLine(inputvalue1 / inputvalue2 + " = " + inputvalue1 + " / " + inputvalue2 + " <Divided>");
                Console.WriteLine(inputvalue1 + inputvalue2 + " = " + inputvalue1 + " + " + inputvalue2 + " <Added>");
                Console.WriteLine(inputvalue1 - inputvalue2 + " = " + inputvalue1 + " - " + inputvalue2 + " <Subtracted>");
                Console.WriteLine("\n");
    
                Console.WriteLine("<Press Enter To End Program>");
                Console.Read();
            }
    Last edited by Dylan3dx; July 18th, 2014 at 04:43 PM.

  2. #2
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: C# Console App - Error Check Calculator [Finished]

    looks good

    this is the change i made i took the two while loops and condensed that using
    a method that generalized what was being done, (this shrinks our code further)
    now in this way we make a utility method that we ourselves can reuse for other things

    Code:
        class Program
        {
            static void Main()
            {
                float inputvalue1 = 0f;
                float inputvalue2 = 0f;
    
                Console.WriteLine("<This Is A Calulator That Takes 2 Numbered Inputs, Real Or Rational>");
                Console.WriteLine("<It Also Checks If A User Entered A Correct Input>");
                Console.WriteLine("<Press Enter After Entering A Number To Proceed Calculation>");
                Console.WriteLine("\n");
    
                Console.WriteLine("\n"+"Enter The First Number:");
                while (false == ParseUsersNumericConsoleInput(out inputvalue1))
                {
                    Console.WriteLine("You Did Not Enter The First Number Correctly! Try Again \n");
                }
    
                Console.WriteLine("\n"+"Enter The Second Number:");
                while (false == ParseUsersNumericConsoleInput(out inputvalue2))
                {
                    Console.WriteLine("You Did Not Enter The Second Number Correctly! Try Again \n");
                }
    
                ResultsToConsole(inputvalue1,inputvalue2);
    
                Console.WriteLine("<Press Enter To End Program>");
                Console.Read();
            }
    
            public static bool ParseUsersNumericConsoleInput(out float variabletoparse)
            {
                Console.WriteLine("Enter A Number:");
                string userinput = Console.ReadLine();
                //
                if (float.TryParse(userinput, out variabletoparse))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    
            public static void ResultsToConsole(float inputvalue1,float inputvalue2)
            {
                Console.WriteLine("The 4 Basic Operations Calulate Your Numbers Out As:");
                Console.WriteLine(inputvalue1 * inputvalue2 + " = " + inputvalue1 + " * " + inputvalue2 + " <Multiplied>");
                Console.WriteLine(inputvalue1 / inputvalue2 + " = " + inputvalue1 + " / " + inputvalue2 + " <Divided>");
                Console.WriteLine(inputvalue1 + inputvalue2 + " = " + inputvalue1 + " + " + inputvalue2 + " <Added>");
                Console.WriteLine(inputvalue1 - inputvalue2 + " = " + inputvalue1 + " - " + inputvalue2 + " <Subtracted>");
                Console.WriteLine("\n");
            }
        }
    when you make methods especially helpers ones that qualify to be in your own little
    static class or project
    you can save them and add them later to other projects you make i.e. you can reuse them
    the smaller the better but even if they grow a bit its ok you can break them down further later

    the usefulness of them is in your main function, were the highest program logic flow's
    it makes your main function much easier to read,
    this in turn allows you to make it do more, yet still be readable

    for example
    ive altered it again to show how you add more functionality in main
    with very little code here we now can press q at any time to quit
    after were done we can press r to restart and add numbers or just enter and quit

    Code:
        class Program
        {
            static void Main()
            {
                float inputvalue1 = 0f;
                float inputvalue2 = 0f;
                bool useriscanceling = false;
    
                ShowStartingMsg();
    
                while(useriscanceling == false)
                {
                    Console.WriteLine("\n" + "Enter The First Number:");
                    if(false == useriscanceling)
                    {
                        inputvalue1 = GetUsersNumericConsoleInput(out useriscanceling);
                    }
    
                    Console.WriteLine("\n" + "Enter The Second Number:");
                    if(false == useriscanceling)
                    {
                        inputvalue2 = GetUsersNumericConsoleInput(out useriscanceling);
                    }
    
                    if(false == useriscanceling)
                    {
                        ResultsToConsole(inputvalue1, inputvalue2);
                        Console.WriteLine("<Press Enter To End Program or R to restart>");
                        useriscanceling = true;
                        string userinput = Console.ReadLine();
                        if (userinput.Contains('r') || userinput.Contains('R'))
                        {
                            useriscanceling = false;
                        }
                    }
                }      
            }
            public static float GetUsersNumericConsoleInput(out bool userpressedcancel)
            {
                string userinput = Console.ReadLine();
                float variabletoparse = 0f;
                bool userenteredquit = false;
                if (userinput.Contains('q'))
                {
                    userenteredquit = true;
                }
                //
                while (false == float.TryParse(userinput, out variabletoparse) && userenteredquit == false)
                {
                    Console.WriteLine("That is Not a Number Re-Enter A Number:");
                    userinput = Console.ReadLine();
                    if (userinput.Contains('q'))
                    {
                        userenteredquit = true;
                    }
                }
                userpressedcancel = userenteredquit;
                return variabletoparse;
            }
            public static void ShowStartingMsg()
            {
                Console.WriteLine("<This Is A Calulator That Takes 2 Numbered Inputs, Real Or Rational>");
                Console.WriteLine("<It Also Checks If A User Entered A Correct Input>");
                Console.WriteLine("<Press Enter After Entering A Number To Proceed Calculation>");
                Console.WriteLine("<Press q to quit at anytime>");
                Console.WriteLine("\n");
            }
            public static void ResultsToConsole(float inputvalue1,float inputvalue2)
            {
                Console.WriteLine("The 4 Basic Operations Calulate Your Numbers Out As:");
                Console.WriteLine(inputvalue1 * inputvalue2 + " = " + inputvalue1 + " * " + inputvalue2 + " <Multiplied>");
                Console.WriteLine(inputvalue1 / inputvalue2 + " = " + inputvalue1 + " / " + inputvalue2 + " <Divided>");
                Console.WriteLine(inputvalue1 + inputvalue2 + " = " + inputvalue1 + " + " + inputvalue2 + " <Added>");
                Console.WriteLine(inputvalue1 - inputvalue2 + " = " + inputvalue1 + " - " + inputvalue2 + " <Subtracted>");
                Console.WriteLine("\n");
            }
       }
    this is in essence the real idea and power that drives object oriented programing
    it exists so that the structural flow at the highest level of logic maintains coherency
    as your program grows in complexity
    Last edited by willmotil; July 18th, 2014 at 06:12 PM.

  3. #3
    Join Date
    Jun 2014
    Posts
    17

    Re: C# Console App - Error Check Calculator [Finished]

    Quote Originally Posted by willmotil View Post
    looks good

    this is the change i made i took the two while loops and condensed that using
    a method that generalized what was being done, (this shrinks our code further)
    now in this way we make a utility method that we ourselves can reuse for other things
    Okay, Nice I see what your doing. Seems a little more advanced then the basic stuff I do but i like the challenge.

    I understand the new methods you made and what it is interpreting, the only problem is I seem to be very confused when trying to figure out how and when to use new methods.
    Last edited by Dylan3dx; July 18th, 2014 at 06:21 PM.

  4. #4
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: C# Console App - Error Check Calculator [Finished]

    remember even main is a method, so your almost always in a method anyways
    anyways there are signs to when your not doing it and you really should typically
    when you feel like some method is too large bloated or ugly ect...
    but in general...
    you want do it in a class or project when you recognize that particular class will be
    repeatedly doing the same thing over and over and a simple general method call
    is easier then rewriting it 10 times , you might want to do that as well because
    it might end up being a candidate for a helper method you might reuse in other projects

    essentially you do so when
    its useful or solves a problem
    makes your code cleaner clear ,eliminates substantial duplicate code
    isolates complex code
    generalizes code into parameter input to a routine to get a checked or controlled result
    which is essentially simplifying complexity as well
    Last edited by willmotil; July 18th, 2014 at 08:09 PM.

  5. #5
    Join Date
    Jun 2014
    Posts
    17

    Re: C# Console App - Error Check Calculator [Finished]

    Quote Originally Posted by willmotil View Post
    remember even main is a method, so your almost always in a method anyways
    anyways there are signs to when your not doing it and you really should typically
    when you feel like some method is too large bloated or ugly ect...
    but in general...
    So could i use new classes instead of repeating or using way to many methods in the main class program. I think its just the order of the methods that confuse me. Like determining if a variable was used before, in, or after the method. I always thought you could take methods and make them global to use in a specific order. That is kind of what I need to do. In depth of the situation I would like to use methods in an ordered way say like method1() shouldnt be after method2() it should be before. Or like the code you had above, public static void ShowStartingMsg() should be first near the top and not towards the end the class program. I think that would make it easier to read for me anyway. Like reading a book, chronological order. Is there a way I can substantiate methods in order?

  6. #6
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: C# Console App - Error Check Calculator [Finished]

    you dont have to use methods your example is perfectly fine
    these are all just extra tools for you to use when you need them.
    even the idea's themselves are tools, you can look up "c# programing patterns" on you tube
    that will find a ton of strategys on how to structure code to handle really complicated tasks.

    I would like to use methods in an ordered way say like method1() shouldnt be after method2() it should be before. Or like the code you had above, public static void ShowStartingMsg() should be first near the top and not towards the end the class program
    that was a just choice you can simply cut and paste the methods in the class
    to the position you find them most visually appealing
    it doesn't affect the order they are called within main, that is... from the top of main down

    to say if i make a method in my program class
    public static void SomeDummyMethod(){...} but...
    never call it in main(){ } or from within any other method
    it will never actually be used or entered

    I think its just the order of the methods that confuse me. Like determining if a variable was used before, in, or after the method. I always thought you could take methods and make them global to use in a specific order
    you can make the methods to explicitly handle each thing as it occurs you can have method1 2 3 4 ect...
    for some circumstances you may do this for example ShowStartingMsg() is pretty specific to the class
    in fact you could say it probably shouldn't be called in someone else's class
    it might be considered sort of private to just to this class of yours, its your call on that

    later on you might find however one method can be generalized
    to say one method just accepts different parameters in order
    you intentionally want any class to be able to call it, pass it parameters and work.
    you want it to be public, maybe even, static public for any class to use.

    consider the following Example
    of the Console.Writeline("string") method that we are using above a lot over an over

    its nice that we can do this the messege is not the same but the way we write it is writeline(...);
    main()
    {
    Console.WriteLine("messege1");
    Console.WriteLine("messege2");
    }

    but you really wouldn't want to have to do what ill show below all the time
    it might be desired for clarity in specific case's but... most of the time i.e. in general probably not
    main()
    {
    Console.WriteLine1("messege1");
    Console.WriteLine2("messege2");
    }

    now the console class could do this and you can too, however if it did...
    somewere in the console class there would have to be something like this
    public static class Console
    {
    public static void WriteLine(string s){....}
    public static void WriteLine0(string s){....}
    public static void WriteLine1(string s){....}
    public static void WriteLine2(string s){....}
    // ect....
    }

    that might be overkill and maybe pointless but technically it will work
    so i hope that is a good example
    im getting a little tired and i dont feel im conveying these ideas well
    but i hope it helps
    Last edited by willmotil; July 18th, 2014 at 10:05 PM.

  7. #7
    Join Date
    Jun 2014
    Posts
    17

    Re: C# Console App - Error Check Calculator [Finished]

    Quote Originally Posted by willmotil View Post
    you dont have to use methods your example is perfectly fine
    these are all just extra tools for you to use when you need them.
    Hey thanks, the example is excellent. I appreciate it alot. New Methods and classes are my downfall to programming and I know I need to use it and practice until my eyeballs fall out lol. This calculator was basically all of the knowledge i had of using loops, parsing, and hard-coding. Thanks again for showing me better ways of doing it and ill keep it in mind.
    Last edited by Dylan3dx; July 20th, 2014 at 02:29 PM.

  8. #8
    Join Date
    Jun 2014
    Posts
    17

    Red face Re: C# Console App - Error Check Calculator [Finished]

    Hey. Just rewrote the code a little from your previous code and made a few changes without the quit and restart. I also decided to keep main at the top as an initialization method. Here it is

    Code:
    class Program
        {
            static void Main()    //---Main Method Is Used First For Initialization Of Methods Below---//
            {
                StartingMsg();
                inputCheck();
                Console.Read();
            }
    
    
            private static void StartingMsg()
            {
                Console.WriteLine("<This Is A Calulator That Takes 2 Numbered Inputs, Real Or Rational>");
                Console.WriteLine("<It Also Checks If A User Entered A Correct Input>");
                Console.WriteLine("<Press Enter After Entering A Number To Proceed Calculation>\n");
            }
            
            
            private static bool ParseUserInput(out float varToParse)
            {
                string userinput = Console.ReadLine();
    
                if (float.TryParse(userinput, out varToParse))
                {
                    return true;
                }
                else 
                {
                    return false;
                }
            }
    
    
            private static void inputCheck()
            {
                float inputvalue1 = 0f;
                float inputvalue2 = 0f;
                
                Console.WriteLine("Enter A Real Or Rational Number");
                while (false == ParseUserInput(out inputvalue1))
                {
                    Console.WriteLine("Invalid Input Re-Enter A Number!");
                }
    
                Console.WriteLine("Enter Another Real Or Rational Number");
                while (false == ParseUserInput(out inputvalue2))
                {
                    Console.WriteLine("Invalid Input Re-Enter A Number!");
                }
    
                Console.WriteLine("\n");
                ResultsToConsole(inputvalue1, inputvalue2);
            }
    
    
            public static void ResultsToConsole(float inputvalue1, float inputvalue2)
            {
                Console.WriteLine("The 4 Basic Operations Calulate Your Numbers Out As:");
                Console.WriteLine("{0} * {1} = {2}", inputvalue1, inputvalue2, inputvalue1 * inputvalue2);
                Console.WriteLine("{0} / {1} = {2}", inputvalue1, inputvalue2, inputvalue1 / inputvalue2);
                Console.WriteLine("{0} + {1} = {2}", inputvalue1, inputvalue2, inputvalue1 + inputvalue2);
                Console.WriteLine("{0} - {1} = {2}", inputvalue1, inputvalue2, inputvalue1 - inputvalue2);
                Console.WriteLine("<Press Any Key to Exit>");
            }
        }
    Last edited by Dylan3dx; July 22nd, 2014 at 09:44 PM.

  9. #9
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    94

    Re: C# Console App - Error Check Calculator [Finished]

    Hey thanks, the example is excellent
    well im glad it helps

    your second example is good too
    so basically now you have two examples that do the same thing
    the only difference is the idea or strategy you used for the task
    often as you progress understanding that idea is actually more important
    as it gives you the ability to teach yourself to handle to new problems you encounter

    I know I need to use it and practice until my eyeballs fall out
    after you program for awhile, what i've found, as strange as it may sound to say,
    is this..

    The best time to learn a new thing is, right when you get stuck on something.
    or when you start to get feeling's about your code you write.

    its sort of like you think... "humm this is getting to complicated"
    or
    "im spending to much time trying to follow how my own code is working"
    and or then
    when you feel curious about "how else could i do this"
    that's actually a great time to take a break and watch a video
    maybe about something in programing, you heard about but
    never really took the time to look into.
    often its great to google simple question like why did they invent properties ect...
    why should i bother to use a class, why not just write one big main method ect...
    also you should take break's often. especially when your starting out.

    take a constructive break from time to time
    watch someone else talk about something new to you.
    i think video's are great for those time's when you get tired confused or pushed yourself to much
    its a good time to see what others are doing or have done, that sound interesting to you.
    to look at your old code and see other ways you could do it.

    later on this gives you choices maybe what they call a sense of taste in coding.
    to say when you see a problem to code you not only know how to solve it
    you can choose how to do so from a number of different approaches
    Last edited by willmotil; July 22nd, 2014 at 11:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured