Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C# Adding Program

  1. Oct 27, 2013 #1

    TheDemx27

    User Avatar
    Gold Member

    Just starting to learn C#. I just wrote this program to add two numbers and loops by looking up syntax etc. There HAS to be a more efficient way to do this.

    Code (Text):

    using System;

        public class Program
        {
            static void Main()
            {
                bool repeat = true;
                while (repeat == true)
                    {
                        string x;
                        string y;
                        int xint;
                        int yint;
                        string useranswer;


                        Console.WriteLine("Enter 'X':");
                        x = Console.ReadLine();
                        xint = int.Parse(x);
                        Console.Clear();

                        Console.WriteLine("Enter 'Y':");
                        y = Console.ReadLine();
                        yint = int.Parse(y);
                        Console.Clear();

                        int answer = xint + yint;

                        Console.WriteLine("{0} + {1} = {2}", x, y, answer);
                        Console.ReadLine();
                       
                        Console.WriteLine("Run again?: Y/N");
                        useranswer = Console.ReadLine();
                        Console.Clear();

                        if (useranswer == "y" || useranswer == "Y")
                        {
                            repeat = true;
                        }
                        else
                        {
                            repeat = false;
                        }
                    }

                }

        }
     
     
  2. jcsd
  3. Oct 27, 2013 #2

    jim mcnamara

    User Avatar

    Staff: Mentor

    You could pass X and Y from the command line.
     
  4. Oct 28, 2013 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    That's about as good as it gets, for reading input. C# doesn't have "easy to use" functions like scanf() in C or the << operator in C++. But those easy to use functions are not much use except for toy programs, because it's very hard to catch errors in the input.

    You could make the code a neater by "factoring out" the repeated code and writing your own input function, something like
    Code (Text):

    int getvalue(string prompt)
    {
      Console.WriteLine(prompt);
      string x = Console.ReadLine();
      Console.Clear();
      return int.Parse(x);
    }
    ...
    xint = getvalue("Enter X:");
    yint = getvalue("Enter Y:");
     
    (Note, I haven't actually tested that code!)

    Your test for repeating the loop is a bit longwinded. You could write
    Code (Text):

    repeat = (useranswer == "y" || useranswer == "Y");
     
    or even
    Code (Text):

    repeat = (useranswer.ToLower() == "y");
     
    You can also make your code shorter by declaring variables when you first use them, instead of by separate statements at the top of a function, as I did in
    Code (Text):

    string x = Console.ReadLine();
     
    or even
    Code (Text):

    var x = Console.ReadLine();
     
    where "var" means "the compiler can figure out what type of variable this is, so I don't really care".
     
    Last edited: Oct 28, 2013
  5. Oct 29, 2013 #4
    you could just use a forms app instead of a console app which is much more efficient or
    Console.WriteLine("{0} + {1} = {2}", x, y, xint+yint);
    maybe that will work its been awhile since ive done a console app. get visual studio it's so superior.
     
  6. Oct 30, 2013 #5

    harborsparrow

    User Avatar
    Gold Member

    It's advisable to error check all inputs. You can do it like this. Once you get an input, check to see if it really is an integer:


    try
    {
    Int.Parse(myinputstring);
    }
    catch (Exception ex)
    {
    Console.WriteLine("That was not an integer.");
    }


    And yes, it will probably be needful to use a goto statement, or else, use a flag to create a rather obscure loop, until you get all legal inputs.
     
  7. Oct 30, 2013 #6

    harborsparrow

    User Avatar
    Gold Member

    And never, ever use Int.Parse without enclosing it in a try/catch, because otherwise your program can throw an obscure error message to the user upon bad input. When I was teaching, that would earn you an instant failing grade.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C# Adding Program
  1. C program (Replies: 4)

  2. C program (Replies: 4)

  3. C Program (Replies: 3)

  4. C program (Replies: 0)

  5. C programming (Replies: 9)

Loading...