CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2012
    Location
    Cave
    Posts
    3

    A noob doing lots of things wrong

    I just started to try and learn C# after having spent some time with Python.
    The thing is, when I try to debug my console application it gives 21 errors, which gives me the impression that I'm doing everything wrong. None of the errors are the "You missed a ; there" kind of errors. My question is, what am I doing wrong? I suspect it to be something that I used to do while using Python, but which is not the way to do things in C#. I do not expect you guys to read and debug all my code of course, I'm just asking for some help and general ideas about what I'm doing wrong.

    My Code (it's a simple calculator):
    Code:
    string choice="y";
                while(choice!="y" && choice!="Y")
                {
                Console.WriteLine("Welcome to this simple C# calculator!");
                Console.Write("1st Number: ");
                string input1=Console.ReadLine();
                while (Int32.TryParse(input1)==false)
                {
                    Console.Write("Please insert a number: ");
                    input1=Console.ReadLine();
                }
                int intput1=Int32.Parse(input1);
                Console.Write("Operator (+, -, /, *, ^: ");
                string moperator;
                moperator=Console.ReadLine();
                while (moperator!="+" && moperator!="-" && moperator!="/" && moperator!="*" && moperator!="^")
                {
                    Console.WriteLine("Invalid Operator, this application only supports the following operators: +, -, /, *, ^");
                    Console.Write("Please specify a different operator: ");
                    moperator=Console.ReadKey();
                }
                Console.Write("2nd Number: ");
                string input2=Console.ReadLine();
                while (Int32.TryParse(input2)==false)
                {
                    Console.Write("Please insert a number: ");
                    input2=Console.ReadLine();
                }
                int intput2=Int32.Parse(input2);
                if (moperator=="+")
                {
                    Console.WriteLine(input1+"+"+input2+"="+(intput1+intput2).ToString);
                }
                else if (moperator=="-")
                {
                    Console.Write(input1+"-"+input2+"=");
                    Console.WriteLine((intput1-intput2).ToString);
                }
                else if (moperator=="/")
                {
                    Console.WriteLine(input1+"/"+input2+"="+(intput1/intput2).ToString);
                }
                else if (moperator=="*")
                {
                    Console.WriteLine(input1+"*"+input2+"="+(intput1*intput2).ToString);
                }
                else if (moperator=="^")
                {
                    int result=1;
                    int repetition=0;
                    while (repetition<intput2)
                    {
                        result*=intput1;
                        repetition+=1;
                    }
                    Console.WriteLine(input1+"^"+input2+"="+(result).ToString);
                }
                Console.Write("Would you like to perform another calculation? (Press Y to continue, all other keys will shutdown)");
                choice = Console.ReadKey();
                }
    Please help me?

  2. #2
    Join Date
    Aug 2012
    Location
    Cave
    Posts
    3

    Re: A noob doing lots of things wrong

    Only 10 errors left by the way, I changed the code a little while posting the opening post.
    The code it shows has 10 errors, but I don't understand what Visual C# Express is telling me.

    Errors:
    Name:  Error1-6.png
Views: 118
Size:  24.4 KBName:  error7-10.png
Views: 131
Size:  15.7 KB

  3. #3
    Join Date
    Jan 2010
    Posts
    1,133

    Re: A noob doing lots of things wrong

    1. & 3. No overload for method...
    If you check the MSDN Documentation for TryParse(), or pay attention to IntelliSense while you type, you'll see that the method takes two arguments. Your code passes only one. The int type doesn't define a single-argument version of TryParse(), instead, there is Parse() which throws an exception on error. TryParse() does not - it simply returns a bool flag, and , if successful, the result, via the second output parameter.
    int number;
    bool result = Int32.TryParse(value, out number);


    The out keyword is simply a way to pass an int by reference and simultaneously specify it as an out parameter.
    After the final line in the short example above, number will contain the parsed value.

    2. & 10. Cannot implicitly convert...
    ReadKey() returns a ConsoleKeyInfo object, not a string, and you're trying to assign it to a string variable (moperator), and you can't do that since C# is strongly typed.
    You could use the ConsoleKeyInfo to obtain the information: ConsoleKeyInfo cki = Console.ReadLine();, and then cki.Key.ToString() or cki.KeyChar.

    4., 7., 8. & 9. Operator '+' cannot be...
    You forgot to end a method call with (). For example, in this line:
    Console.WriteLine(input1+"+"+input2+"="+(intput1+intput2).ToString);
    (So the compiler thinks that you want to concatenate the string with the method itself and not with its result.)

    5. The best overloaded method match ... invalid arguments.
    6. Argument 1: cannot convert from 'method group' to string.
    The two error messages are about the same error. The solution is the same as for (4. [and 7., 8. & 9.]).
    Last edited by TheGreatCthulhu; August 28th, 2012 at 06:15 AM.

  4. #4
    Join Date
    Aug 2012
    Location
    Cave
    Posts
    3

    Re: A noob doing lots of things wrong

    Thank you very much! All the errors are gone now!

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