I made a simple calculator in C using switch, printf and scanf. It handles decimals, the four basic operations and division by zero. What would be a good next step for a beginner?
Finished my first C calculator. What should I try next?
Answers
Add a loop so you can do more than one calculation without restarting the program. Let the user enter q to quit, then look at what happens when they enter an invalid number or operator.
That gives you practice with loops and input handling without needing to redesign the whole calculator.
Reply to this answer
Try keeping the last result around. After calculating 2 + 3, you could let the user choose an operation and another number to continue from 5. It makes the calculator feel more useful and gives you some practice managing state.
Reply to this answer
For a bigger challenge, accept a whole expression such as 3 * (2 - 5) + 7 instead of asking for each piece separately. Start with just two numbers and one operator, then add operator precedence and parentheses.
Parsing expressions can get tricky, so there's no need to tackle it all at once. Stacks are one useful data structure to learn when you get to parentheses and precedence.
Reply to this answer
You could add more operations next. Modulus is a nice one to explore with integers, while scientific functions would give you a reason to learn about C's math library.
Reply to this answer
A graphical calculator with buttons and a display could be a fun longer term goal. There's no rush, though. A different small project, like modeling the states of traffic lights at an intersection, would also give you more practice with control flow.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions