Programming
Lab 24 · C++ Basics Lab: Student Marks Calculator with Decisions and Loops
🎯 Objective
Turn the C++ Basics lesson into a finished, tested program: input, validation, a loop, a function and a grade decision, all working together.
🧩 Skills You Will Practice
- C++ variables and types
- cin/cout and input validation
- for loops
- Functions and return values
- if-else grade logic
🧰 What You Need
- A C++ compiler: g++ on your computer, or a free online C++ compiler for practice
- VS Code or any text editor
- A file marks.cpp
- The C++ Basics lesson open for reference
📋 Step-by-Step Tasks
- Set up and prove the compiler works: create marks.cpp with the Hello program from the C++ Basics lesson, compile it (g++ marks.cpp -o marks) and run it. If the compiler is not found, install a compiler or switch to a free online C++ compiler before writing anything else.
- Add the structure: include iostream and string, write int main(), and declare int total = 0; — initialise it to zero now, because an uninitialised total is a classic C++ bug that gives garbage results.
- Build the input loop: for 5 subjects, print a clear prompt (Subject 1 marks out of 100:), read an integer with cin, and add it to total. Compile and run with simple values first — for example five 80s — and check the total is 400 before adding anything clever.
- Add validation inside the loop: if a mark is below 0 or above 100, print Invalid marks — enter a value between 0 and 100 and ask again for the same subject (step the loop back by one). Test this deliberately with -5 and 150.
- Handle bad types: if the user types a word instead of a number, cin fails and the loop can spin forever. Add the standard recovery after reading: if cin fails, clear the error with cin.clear(), discard the bad input with cin.ignore(), print a message, and ask again. Test it by typing a word.
- Add the average function above main: double average(int total, int count) { return (double)total / count; } — the (double) conversion is essential, otherwise integer division rounds the average down. Call it from main and print the average with one decimal place idea in words (for example 82.4).
- Add the grade decision with an if-else chain: 80+ Grade A, 65+ B, 50+ C, otherwise Needs improvement. Print the total, the average and the grade together in a clean result block, so the output reads like a report, not a jumble of numbers.
- Run the full test set and record every output: (a) 90, 85, 78, 92, 88; (b) five 40s; (c) one invalid mark then a valid one; (d) a word typed as a mark; (e) all 100s. Every case must finish with a correct report and no crash or infinite loop.
- Polish and document: add a comment at the top with your name, what the program does and how to compile and run it. Save the final code and the five test outputs as your deliverable.
✅ Expected Result / Deliverable
A working marks.cpp plus a test record: five test cases (normal, low, invalid number, word input, perfect scores) all producing a correct total, average and grade, with no crash or infinite loop — and a top comment explaining how to compile and run it.
⚠️ Common Mistakes
- Forgetting to initialise total to 0 (garbage totals)
- Integer division making the average wrong (missing the (double) conversion)
- A validation loop that never re-asks, or never ends, on bad input
- Using = instead of == in a condition
- Skipping the cin-failure test and discovering it only in a demo
Safety, Ethics & Pro Tip
Write the code yourself and test every case in the test set — a program you cannot explain is not finished, even if it runs. Use only your own invented marks data in screenshots and notes.