CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2015
    Posts
    4

    Question Newb trying to learn C# / stuck

    Hello there,

    I've just started getting into C# and of course, the first thing I wanted to make was a calculator.. aaaaand I'm stuck..

    This is what I have:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Calculator
    {
        class Program
        {
            static void Main(string[] args)
            {
            Start:
                string nr1;
                float nr2;
                float nr3;
    
                Console.Write("Enter a calculation operation symbol which you'd like to use: ");
                nr1 = Console.ReadLine();
                Console.Write("Enter the first number:  ");
                nr2 = Console.ReadLine();
                Console.Write("Enter the second number:  ");
                nr3 = Console.ReadLine();
    
                if (nr1 == "+")
                {
                    goto Addition;
                }
                else if (nr1 == "-")
                {
                    goto Subtraction;
                }
                else if (nr1 == "/")
                {
                    goto Dividing;
                }
                else if (nr1 == "*")
                {
                    goto Multiplying;
                }
                else Console.Write("Incorrect Entry, try again!");
                goto Start;
            Addition:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 + nr3));
                Console.ReadKey();
                Console.Clear();
                goto Start;
            Subtraction:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 - nr3));
                Console.ReadKey();
                Console.Clear();
                goto Start;
            Dividing:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 * nr3));
                Console.ReadKey();
                Console.Clear();
                goto Start;
            Multiplying:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 / nr3));
                Console.ReadKey();
                Console.Clear();
    
                goto Start;
            }
        }
    }
    Addition and Subtraction seem to work, but multiplaying and dividing don't.

    Your help would be appreciated!

  2. #2
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,754

    Re: Newb trying to learn C# / stuck

    When you compile this, do you not get an error stating that you can't implicitly convert type 'string' to 'float' when you are trying to readline() into nr1 and nr3? Compile your listing and you'll get the line numbers of what is wrong.

    The other issue in your listing is that you multiply in your Dividing case and divide in your multiplying case.

    Good luck with the homework.

    Brad!
    -----------------------------------------------
    Brad! Jones,
    Yowza Publishing
    LotsOfSoftware, LLC

    -----------------------------------------------

  3. #3
    Join Date
    Sep 2015
    Posts
    4

    Re: Newb trying to learn C# / stuck

    At the time when I posted this from what I see now, I removed the "Convert.ToDouble" (and nr 2 and 3 were "double"s and not "float"). And FYI this isn't my homework, like I said, just trying to figure out C#.

  4. #4
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,754

    Re: Newb trying to learn C# / stuck

    Seems like a standard homework problem. Regardless, it is a good learning example.

    For float you can use Convert.ToSingle(). I did that and swapped your cases for the math and everything worked fine.

    Code:
    Dividing:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 * nr3));

  5. #5
    Join Date
    Sep 2015
    Posts
    4

    Re: Newb trying to learn C# / stuck

    Yeah,.. I was changing the code because I thought that something was wrong with it.. but actually I was getting confused with the "wrong" results because of the swapped dividing and multiplying thing. Thanks!

    this now works fine,

    Code:
                string nr1;
                double nr2;
                double nr3;
    
                Console.Write("Enter a calculation operation symbol which you'd like to use: ");
                nr1 = Console.ReadLine();
                Console.Write("Enter the first number:  ");
                nr2 = Convert.ToDouble(Console.ReadLine());
                Console.Write("Enter the second number:  ");
                nr3 = Convert.ToDouble(Console.ReadLine());
    
                if (nr1 == "+")
                {
                    goto Addition;
                }
                else if (nr1 == "-")
                {
                    goto Subtraction;
                }
                else if (nr1 == "/")
                {
                    goto Dividing;
                }
                else if (nr1 == "*")
                {
                    goto Multiplying;
                }
                else Console.Write("Incorrect Entry, try again!");
                goto Start;
            Addition:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 + nr3));
                Console.ReadKey();
                Console.Clear();
                goto Start;
            Subtraction:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 - nr3));
                Console.ReadKey();
                Console.Clear();
                goto Start;
            Dividing:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 / nr3));
                Console.ReadKey();
                Console.Clear();
                goto Start;
            Multiplying:
                Console.Clear();
                Console.WriteLine("The result is: " + (nr2 * nr3));
                Console.ReadKey();
                Console.Clear();
    
                goto Start;
            }
        }
    }
    Last edited by Brad Jones; September 17th, 2015 at 04:41 PM. Reason: fixed code tag

  6. #6
    Join Date
    Feb 2008
    Posts
    108

    Re: Newb trying to learn C# / stuck

    It would be good if you would forget about using 'goto' if you want to write good code.
    I have rewritten your program to use functions instead of 'goto's.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    // This program is a demonstration of using functions to program a simple calculator--
    namespace Calculator
    {
        class Program
        {
            static double Addition(double db1, double db2)
                // Function using Value parameters
            {
                return (db1 + db2);
            }
            static double Subtraction(double db1, double db2)
            {
                return (db1 - db2);
            }
            static double Multiplication(double db1, double db2)
            {
                return (db1 * db2);
            }
            static double Division(double db1, double db2)
            {
                return (db1 / db2);
            }
    
            static void Get_Numbers(ref double D1, ref double D2, ref bool Success)
                // Practice of a function using reference parameters --
            {
                string s2;
                string s3;
    
                Success = true; //Unless error is later detected;
    
                Console.Write("Enter the first number:  ");
                s2 = Console.ReadLine();
                try
                {
                    D1 = Convert.ToDouble(s2);
                }
                catch (FormatException)
                {
                    Console.WriteLine("Unable to convert '{0}' to a Double.", s2);
                    Success = false;
                }
                catch (OverflowException)
                {
                    Console.WriteLine("'{0}' is outside the range of a Double.", s2);
                    Success = false;
                }
                if (Success)
                {
                    Console.Write("Enter the second number:  ");
                    s3 = Console.ReadLine();
                    try
                    {
                        D2 = Convert.ToDouble(s3);
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Unable to convert '{0}' to a Double.", s3);
                        Success = false;
                    }
                    catch (OverflowException)
                    {
                        Console.WriteLine("'{0}' is outside the range of a Double.", s3);
                        Success = false;
                    }
                }
            }
    
            static void Main(string[] args)
            {
                string s1;
                double D1 = 0.0;
                double D2 = 0.0;
                bool done = false;
                bool Numbers_are_Valid = false;
    
                while (!done)
                {
                    Console.Write("Enter a calculation operation symbol which you'd like to use or $ to quit. ");
                    s1 = Console.ReadLine();
                    if (s1 != "$")
                    {
                        switch (s1)
                        {
                            case "+":
                                {
                                    Get_Numbers(ref D1, ref D2, ref Numbers_are_Valid);
                                    if (Numbers_are_Valid)
                                    {
                                        Console.WriteLine("The result is: " + Addition(D1, D2));
                                    }
                                };
                                break;
    
                            case "-":
                                {
                                    Get_Numbers(ref D1, ref D2, ref Numbers_are_Valid);
                                    if (Numbers_are_Valid)
                                    {
                                        Console.WriteLine("The result is: " + Subtraction(D1, D2));
                                    }
                                };
                                break;
    
                            case "*":
                                {
                                    Get_Numbers(ref D1, ref D2, ref Numbers_are_Valid);
                                    if (Numbers_are_Valid)
                                    {
                                        Console.WriteLine("The result is: " + Multiplication(D1, D2));
                                    }
                                };
                                break;
    
                            case "/":
                                {
                                    Get_Numbers(ref D1, ref D2, ref Numbers_are_Valid);
                                    if (Numbers_are_Valid)
                                    {
                                        Console.WriteLine("The result is: " + Division(D1, D2));
                                    }
                                };
                                break;
    
                            default:
                                {
                                    Console.Write("Incorrect Operation Entry, try again!");
                                };
                                break;
                        }
                    }
                    else
                    {
                        Console.WriteLine("Exiting program.");
                        done = true;
                    }
                }
            }
        }
    }
    Developing using:
    .NET3.5 / VS 2010

  7. #7
    Join Date
    Sep 2000
    Location
    Indianapolis
    Posts
    6,754

    Re: Newb trying to learn C# / stuck

    Jim - thanks for posting that. When I looked at the listing, I did it extremely fast. I had case logic in my head and didn't see pay close enough attention to see he was using gotos. I was focused on what was causing errors. D'oh!

    I get a fail 'code reviewing 101' on this one!

    Brad!

Tags for this Thread

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