This is really easy to modify, simply replace all our int types with double. Lastly, as for a bonus to this answer, you can also add alias operations. What do I mean? But you can still use x as well. Our entire programme is now down to 29 lines with appropriate whitespace, and supports much more than your original did.
Your original was 44 lines. All-in-all, very good start. Hopefully you learned a lot from this answer and can apply some of it to future applications. Be aware that entering text like "hello" at a number prompt will cause an exception. There are several ways to approach this.
The easiest would be to use Int Here is an example:. For starters, you did a great job at writing your first program, there are few things i will highlight here that will be useful in your future programs.
DRY- Do not Repeat Yourself: in each of the decision statements you had the variable answer get assigned multiple times, Console. WriteLine and Console. ReadLine repeated. Always find a workaround when you notice repetition. Error Handling: Just as every one pointed, when reading inputs you need to validate or check their validity. For instance, in my main method I have this line of codes. Notice, I used Int TryParse to check if your input values can be parsed to int and if not loop till you get a valid input.
The validation for the operation is taken care of by the default in the switch statement. Depending on the interaction you aim to achieve, you could implement something similar for the operation or stop the program when inaccurate results are passed. Meaningful names: For future references, it's best to have a good naming convention such as your namespace and class should depict the use of the class. For example, the class could be given be the name CalculatorSystem Pascal case.
Grouping with Methods: To make your codes reusable , its best to group them using methods. Note, methods in C are in Pascal case as well. An example of this. I refactored your solution and i created a Calculate method that has firstNum, secondNum and operation as inputs and returns the answer as a string. Although, i tried to to change your code and make it simple. Notice I used a switch as opposed to an if statement and you can find the advantages of using the switch here Advantages of Switch over If statement.
Also for good practices, its good to seclude your print out statements to the Main method only because later on you might decide to have a gui appplication displaying the answer and making amends to the Main method is easier. Note: I removed the Console. ReadLine after the "Hello, welcome to Alex's basic calculator because it was redundant. You can make some amends by asking for users' names etc. Comments: Including comments is regarded as a good practice, which am glad you imbibed during your console application.
Other Interesting activities to delve into include checking the behaviour of your program if floats, doubles were inputted will it be truncated and experimenting with Environment. Exit 0 to enable users exit the application. Good luck with coding. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Learn more. Check Whether a Number is Palindrome or Not. Related Topics C switch Statement. C break and continue. Print an Integer Entered by the User. Multiply Two Floating-Point Numbers. Compute Quotient and Remainder. Types of User-defined Functions in C Programming. Share on:. Did you find this article helpful?
Sorry about that. How can we improve it? Leave this field blank. In this blog, you will learn how to create a simple calculator Console app using C language. This app will help end users to perform tasks, such as Addition, Subtraction, Multiplication, and Division.
Open the Visual Studio editor. As you can see in the Start Page, there is an option for New Project. The following pop up appears.
0コメント