Jump to content

C# beginner looking for help

Go to solution Solved by Mr_KoKa,

Two problems, you're missing closing bracket of if(Choice == "/"). If this code compiles for you, the possible scenario is you have the closing bracket at the end, and printing of the answer falls into that specific if, in other words, answer is produced only for division operation. You want to add close bracket before line:

Console.WriteLine("the operation has been apllied");


 

Second thing is, you re using WriteLine with Ans as argument, but you don't specify where to put the Argument in format. I think you through it will just print all arguments one by one, first the string "The Answer is = " and then the Ans variable, but it doesn't work this way, first argument of WriteLine is format where you can include placeholders which other arguments will fall in later on, it would look like this:

 

Console.WriteLine("First argument {0}, second argument {1}", 100, 200) // Produces: "First argument 100, second argument 200"
Console.WriteLine("Second argument {1}, first argument {0}", 100, 200) // Produces: "Second argument 200, first argument 100"

This is simple example and there is more, you can read about it here: https://msdn.microsoft.com/pl-pl/library/586y06yf(v=vs.110).aspx

hi everyone

I am doing a course on c#...started one week ago with absolutely no prior exp in this,getting frustrated with this calculator.

Can anyone tell me what is wrong and why,i don't want a quick fix,i want to learn(you get no cookie for bashing me on making a "simple" mistake)

                  

     Double no1 = 0, no2 = 0, Ans = 0;
            String Choice;
            Console.WriteLine("Wellcome to 'my' calculator...it definitely did not take me  2 hours of tutorials to figuer this out");
            Console.WriteLine("Press <F1> then Enter to get started...Simon says!");
            while (Console.ReadKey().Key != ConsoleKey.F1) { }
            Console.ReadKey();
            Console.Write("Please enter No1: ");
            no1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please chose operation:(+,-,* or /)");
            Choice = Console.ReadLine();
            Console.Write("Please enter No2: ");
            no2 = Convert.ToDouble(Console.ReadLine());

            // comment here 
            if (Choice == "+")
            {
                Ans = no1 + no2;
            }
            if (Choice == "-")
            {
                Ans = no1 - no2;
            }
            if (Choice == "*")
            {
                Ans = no1 * no2;
            }
            if (Choice == "/") 
            {
                
                if (no2 == 0)
                {
                    Console.WriteLine("Ans= ERR");
                }
                else
                {
                    Ans = no1 / no2;
                }
                Console.WriteLine("the operation has been apllied");
                Console.Write("The Anwser is = " ,Ans);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();

 

 

Not sure if it matters but i am using ms visual studio 2015

thanks if anyone will actually help!

Link to comment
https://linustechtips.com/topic/663513-c-beginner-looking-for-help/
Share on other sites

Link to post
Share on other sites

Can you use some better formatting? The indents seem to be a bit messed up.

Main Gaming Rig:

Spoiler

Core i7-4770, Cryorig M9i Cooler, ASUS B85M GAMER, 8GB HyperX Fury Red 2x4GB 1866MHz, KFA2 GTX 970 Infin8 Black Edition "4GB", 1TB Seagate SSHD, 256GB Crucial m4 SSD, 60GB Corsair SSD for Kerbal and game servers, Thermaltake Core V21 Case, EVGA SuperNOVA 650W G2.

Secondary PC:

Spoiler

i5-2500k OCed, Raijintek Themis, Intel Z77GA-70K, 8GB HyperX Genesis in grey, GTX 750 Ti, Gamemax Falcon case.

 

Link to post
Share on other sites

@DanStan, you'll want to copy and paste that code into the "Code Tags" in the Forum text editor (The <> button).

 

Just pasting the code into the text editor will screw up your formatting. The Code Tags also have line numbers, which helps with debugging.

For Sale: Meraki Bundle

 

iPhone Xr 128 GB Product Red - HP Spectre x360 13" (i5 - 8 GB RAM - 256 GB SSD) - HP ZBook 15v G5 15" (i7-8850H - 16 GB RAM - 512 GB SSD - NVIDIA Quadro P600)

 

Link to post
Share on other sites

2 minutes ago, dalekphalm said:

@DanStan, you'll want to copy and paste that code into the "Code Tags" in the Forum text editor (The <> button).

 

Just pasting the code into the text editor will screw up your formatting. The Code Tags also have line numbers, which helps with debugging.

Done,thanks for that

Link to post
Share on other sites

Two problems, you're missing closing bracket of if(Choice == "/"). If this code compiles for you, the possible scenario is you have the closing bracket at the end, and printing of the answer falls into that specific if, in other words, answer is produced only for division operation. You want to add close bracket before line:

Console.WriteLine("the operation has been apllied");


 

Second thing is, you re using WriteLine with Ans as argument, but you don't specify where to put the Argument in format. I think you through it will just print all arguments one by one, first the string "The Answer is = " and then the Ans variable, but it doesn't work this way, first argument of WriteLine is format where you can include placeholders which other arguments will fall in later on, it would look like this:

 

Console.WriteLine("First argument {0}, second argument {1}", 100, 200) // Produces: "First argument 100, second argument 200"
Console.WriteLine("Second argument {1}, first argument {0}", 100, 200) // Produces: "Second argument 200, first argument 100"

This is simple example and there is more, you can read about it here: https://msdn.microsoft.com/pl-pl/library/586y06yf(v=vs.110).aspx

Link to post
Share on other sites

12 minutes ago, Mr_KoKa said:

Two problems, you're missing closing bracket of if(Choice == "/"). If this code compiles for you, the possible scenario is you have the closing bracket at the end, and printing of the answer falls into that specific if, in other words, answer is produced only for division operation. You want to add close bracket before line:


Console.WriteLine("the operation has been apllied");


 

Second thing is, you re using WriteLine with Ans as argument, but you don't specify where to put the Argument in format. I think you through it will just print all arguments one by one, first the string "The Answer is = " and then the Ans variable, but it doesn't work this way, first argument of WriteLine is format where you can include placeholders which other arguments will fall in later on, it would look like this:

 


Console.WriteLine("First argument {0}, second argument {1}", 100, 200) // Produces: "First argument 100, second argument 200"
Console.WriteLine("Second argument {1}, first argument {0}", 100, 200) // Produces: "Second argument 200, first argument 100"

This is simple example and there is more, you can read about it here: https://msdn.microsoft.com/pl-pl/library/586y06yf(v=vs.110).aspx

i have done that and it work,it looks like this now

Double no1 = 0, no2 = 0, Ans = 0;
            String Choice;
            Console.WriteLine("Wellcome to 'my' calculator...it definitely did not take me  2 hours of tutorials to figuer this out");
            Console.WriteLine("Press <F1> then Enter to get started...Simon says!");
            while (Console.ReadKey().Key != ConsoleKey.F1) { }
            Console.ReadKey();
            Console.Write("Please enter No1: ");
            no1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Please chose operation:(+,-,* or /)");
            Choice = Console.ReadLine();
            Console.Write("Please enter No2: ");
            no2 = Convert.ToDouble(Console.ReadLine());


            // comment here :)
            if (Choice == "+")
            {
                Ans = no1 + no2;
            }
            if (Choice == "-")
            {
                Ans = no1 - no2;
            }
            if (Choice == "*")
            {
                Ans = no1 * no2;
            }
            if (Choice == "/")
            {

                if (no2 == 0)
                {
                    Console.WriteLine("Ans= ERR");
                }
                else
                {
                    Ans = no1 / no2;

                }

                Console.WriteLine("the operation has been apllied");
            }
                Console.WriteLine("The Anwser is {0} = " ,Ans);
                Console.WriteLine("Press any key to exit...");
                Console.ReadKey();

Thanks

Link to post
Share on other sites

16 minutes ago, Mr_KoKa said:

Two problems, you're missing closing bracket of if(Choice == "/"). If this code compiles for you, the possible scenario is you have the closing bracket at the end, and printing of the answer falls into that specific if, in other words, answer is produced only for division operation. You want to add close bracket before line:


Console.WriteLine("the operation has been apllied");


 

Second thing is, you re using WriteLine with Ans as argument, but you don't specify where to put the Argument in format. I think you through it will just print all arguments one by one, first the string "The Answer is = " and then the Ans variable, but it doesn't work this way, first argument of WriteLine is format where you can include placeholders which other arguments will fall in later on, it would look like this:

 


Console.WriteLine("First argument {0}, second argument {1}", 100, 200) // Produces: "First argument 100, second argument 200"
Console.WriteLine("Second argument {1}, first argument {0}", 100, 200) // Produces: "Second argument 200, first argument 100"

This is simple example and there is more, you can read about it here: https://msdn.microsoft.com/pl-pl/library/586y06yf(v=vs.110).aspx

i'll ask about one more thing

how can i get it to show Ans as  no1 / no2 insted of 0 if no2=0 when i divide?

Link to post
Share on other sites

You need to explain me some more, because if no2 is 0, then you cannot divide by it, and you do check for that, so I don't really get what do you mean. If you want to display whole equation with result then you need to put more placeholders in format string and more arguments like: 

 

Console.WriteLine("{0} {1} {2} = {3}", no1, Choice, no2, Ans);

 

I see you use ' sign istead of " in one sentence, you can use " as long as you escape it inside of string. like:

Console.WriteLine("Wellcome to \"my\" calculator...it definitely did not take me  2 hours of tutorials to figuer this out");

 

Link to post
Share on other sites

23 minutes ago, Mr_KoKa said:

You need to explain me some more, because if no2 is 0, then you cannot divide by it,

Sure he can! Since he's using doubles 0/0 = NaN and anything else / 0 = +/- INF. Checking for 0 could be used to give a more detailed message about the result but in floating point world dividing by 0 is fine as opposed to ints where dividing by 0 is an actual error.

1474412270.2748842

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×