Programming lesson
Mastering Console I/O and Arithmetic in C++: A Movie Theater, Tax Report, and Savings Account Tutorial
Learn how to write C++ programs that handle console input, calculations, and formatted output using three classic assignment examples: movie theater profit, sales tax report, and compound interest.
Introduction: Why Console I/O and Arithmetic Still Matter in 2026
In an age of AI-powered apps and cloud computing, you might wonder why we still drill console input/output (I/O) and basic arithmetic in computer science courses. The answer is simple: every complex program—from a movie ticket booking system to a fintech savings calculator—rests on these fundamentals. In 2026, with the rise of decentralized finance (DeFi) and personalized AI assistants, understanding how to precisely compute and display values is more relevant than ever. This tutorial walks you through three classic programming problems from Assignment #03, teaching you how to handle user input, use constants, perform arithmetic, and format output like a pro.
Program 1: Movie Theater Profit Calculator
Understanding the Problem
A movie theater keeps a percentage of ticket revenue; the rest goes to the distributor. Your program asks for the movie name, adult tickets sold, and child tickets sold. Adult tickets cost $10.00, child tickets $6.00. The theater retains 20% of gross profit (net profit), and the distributor gets 80%.
Key Concepts: Constants and Input
Use const double ADULT_PRICE = 10.00; and const double CHILD_PRICE = 6.00; for ticket prices. The profit margin (20%) should also be a constant. Why constants? They make code easier to update and prevent accidental changes. For example, if ticket prices rise, you change only one line.
Step-by-Step Logic
- Prompt for movie name (use
getlineto allow spaces like "St. Patrick's Revenge"). - Prompt for adult and child ticket counts.
- Calculate gross profit:
adultTickets * ADULT_PRICE + childTickets * CHILD_PRICE. - Calculate net profit (theater's share):
grossProfit * 0.20. - Calculate distributor's share:
grossProfit - netProfit. - Display all values with two decimal places using
fixed << setprecision(2).
Sample Output
Movie Name: St. Patrick's Revenge
Adult Tickets Sold: 5621
Child Tickets Sold: 125
Gross Box Office Profit: $56960.00
Net Box Office Profit: $11392.00
Amount Paid to Distributor: $45568.00Common Pitfalls
- Forgetting to handle multi-word movie names: use
cin.ignore()beforegetline. - Not formatting dollar amounts with two decimals.
- Using magic numbers instead of constants.
Program 2: Monthly Sales Tax Report
Real-World Context
Every retail company—whether a brick-and-mortar store or an e-commerce giant—must file sales tax reports. In 2026, with many states adjusting tax rates, this program remains a staple. You'll compute product sales from total collected (including tax) and break down state and county taxes.
Key Formulas
Total collected = product sales + sales tax. Sales tax = 4% state + 2% county = 6% total. So product sales S = T / 1.06, where T is total collected. Then state tax = S * 0.04, county tax = S * 0.02.
Implementation Steps
- Ask for month (string), year (int), total collected (double).
- Use constants:
STATE_TAX = 0.04,COUNTY_TAX = 0.02. - Compute sales = total / 1.06.
- Compute state tax = sales * STATE_TAX.
- Compute county tax = sales * COUNTY_TAX.
- Display all values formatted to two decimals.
Sample Output
Month: January 2017
---------------------
Total Collected: $482.00
Sales: $454.72
County Sales Tax: $9.09
State Sales Tax: $18.19
Total Sales Tax: $27.28Edge Cases
- Month names with spaces: use
getline. - Year as integer: validate input (though not required here).
- Negative totals: assume valid input.
Program 3: Compound Interest Savings Calculator
Trend Connection
With the explosion of DeFi platforms and high-yield savings accounts in 2026, understanding compound interest is crucial. This program calculates the balance after one year, given principal, annual interest rate (as decimal), and compounding frequency.
The Formula
Amount = Principal * (1 + (Rate / T)) ^ T, where T is number of compounding periods per year. Use pow() from <cmath>.
Step-by-Step
- Prompt for principal (double), rate (double), times compounded (int).
- Use constants? Not necessary, but you can define
YEARS = 1for clarity. - Calculate interest = Amount - Principal.
- Display rate as percentage (e.g., 3% for 0.03) by multiplying by 100.
- Format all monetary values with two decimals.
Sample Output
Interest Rate: 3%
Times Compounded: 120
Principal: $30284.00
Interest: $922.17
Amount in Savings: $31206.17Watch Out For
- Rate as decimal: if user enters 3 instead of 0.03, you'll get huge numbers. In a real app, validate.
- Integer division: use
Rate / Twhere both are double/int, but ensureRateis double.
General Tips for All Three Programs
Formatting Output
Always use #include <iomanip> and cout << fixed << setprecision(2); for currency. For percentages, you may need to multiply by 100 and display as integer or with decimals.
Using Constants
Constants make your code professional. For example, in Program 1, define const double PROFIT_MARGIN = 0.20;. In Program 2, const double STATE_TAX = 0.04;. In Program 3, you might define const int YEARS = 1;.
Testing Your Code
Test with the given sample inputs. For Program 1, try movie name with spaces. For Program 2, ensure year is integer. For Program 3, test with fractional interest rate like 0.035.
Conclusion
These three programs cover essential C++ skills: console I/O, arithmetic, constants, and formatted output. Mastering them prepares you for more advanced topics like file I/O, functions, and object-oriented programming. In 2026, whether you're building a movie ticket app, a tax calculator for a startup, or a savings tool for a fintech company, these fundamentals are your foundation. Practice, test edge cases, and soon you'll write clean, professional code.