Introduction
This is the classic first C project, done properly. Instead of trusting the user to type valid numbers, it checks every scanf result, recovers from bad input and refuses mathematically undefined operations. You will practise breaking a program into small functions and driving it with a menu loop.
Problem statement
Objectives
- Structure an interactive program around a menu loop
- Validate numeric input using scanf's return value
- Use <math.h> functions (fmod, pow, sqrt)
- Handle error cases such as division by zero explicitly
Features
- Addition, subtraction, multiplication, division, modulus, power and square root
- Re-prompts on non-numeric input instead of looping forever
- Rejects division/modulus by zero and square roots of negatives
- Counts completed calculations and reports them on exit
Concepts demonstrated
- Functions
- switch
- Input validation
- Floating-point formatting
System architecture
The program has one loop in main that prints the menu, reads a choice and dispatches with a switch. A helper, read_number, owns all input validation: it loops until scanf("%lf", ...) succeeds, discarding the rest of a bad line with getchar. Each case computes and prints its result; error cases print a message and continue so they are not counted as calculations.
Folder structure
Complete source code
Makefile
What each file does
- src/main.c
- The whole program:
read_number(validated input),print_menu, and the menu loop with onecaseper operation. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
1 12.5 7.5 4 10 0 6 2 10 7 81 9 abc 0
Testing
- Run the scripted session in
tests/sample-input.txtand compare withtests/expected-output.txt. - Try dividing by zero and taking the square root of -4; both must print an error and return to the menu.
- Type letters instead of a number for an operand; the program must ask again.
- Press Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows) at the menu; it must exit cleanly.
Common errors
undefined reference to `pow'
On Linux, link the maths library by adding -lm at the end of the gcc command.
Program loops forever after typing letters
You removed the getchar loop in read_number; the bad characters stay in the input buffer and scanf fails again immediately.
Possible improvements
- Keep a history of the last ten results and allow reusing the previous answer
- Support expressions like
12 * (3 + 4)(see the Expression Evaluator project) - Add trigonometric and logarithmic functions
Additional challenges
- 1Add a memory feature (M+, M-, MR, MC).
- 2Print results as exact fractions when both inputs are integers and the result isn't.
- 3Accept input in a single line such as
12 + 30.
Built and tested it yourself? Mark it to update your dashboard.