Variables and Simple Math
Explore how to declare variables and use simple math operators in C++. Understand how to create flexible code that calculates totals dynamically, using a real-world point-of-sale receipt project. Practice updating values and applying discounts to build a functional receipt generator.
The project
Let’s revisit the POS systems: We’ve all been out shopping.
We arrive at the mart, purchase our items, proceed to the cash counter, scan our items through the system, and then experience the point of sale. There’s this machine there. There is a person behind the counter, but a machine prints the receipts.
Someone has built a system using code for that machine and the receipt it prints. This is what the receipt looks like.
You have been hired to create this point-of-sale system! You can see the sample receipt.
You know how to print stuff on the screen already, but you don’t want to hardcode everything. You want a flexible enough system such that if the price of an item changes, you don’t have to change the code manually at various places.
The same applies to the quantity of items.
The programming concept
Today’s programming concept is the variable.
In C++, a variable is a named container in memory that stores data, such as numbers, prices, or quantities, that your program can use later. You’ll also learn about arithmetic operators, which allow you to perform calculations like addition, subtraction, multiplication, and division—similar to how a POS system automatically computes totals without needing a separate calculator.
int milk_quantity = 1;double milk_price = 4.0;double total_milk = milk_quantity * milk_price;
Here:
intis used for whole numbers.doubleis used for decimal numbers.*multiplies the quantity by the price to compute a total.
The significance of the concept in the project
Let’s focus on one item in the bill:
Milk (1): 4
There are two key pieces of information here:
Quantity: the number of milk bottles purchased.
Price: the cost per bottle.
At the end, the program calculates:
Total bill: 4 + 2 + 6 = 12
This total isn’t manually written—it’s calculated from the earlier items. Using variables makes the program flexible and easy to update. If the price or quantity changes, the total adjusts automatically—no need to rewrite multiple lines of code.
Concept at a glance:
int bill = 10;creates a variable namedbilland assigns it the value 10.You can use operators
+,-,*,/for basic arithmetic.Variables can be printed with
std::coutjust like text or numbers.
Learn to code the concept
Let’s start simple. We’ll create variables for milk’s quantity and price, then calculate and print the total.
Here’s what’s happening:
Lines 5–6 create two variables to store quantity and price.
Line 8 prints them using
<<operators to combine text and values.Line 10 multiplies the quantity and price, storing the result in
total_milk.Line 12 prints the computed result.
Try changing milk_quantity to 3 and run the code again—the total updates automatically. That’s the power of using variables!
Learn to code with AI
Now that you understand variables and arithmetic operators, let’s revisit the full project:
=== MART RECEIPT ===Cashier Name: AlexItems Purchased:Milk (1): 4Bread (1): 2Eggs (12): 0.5 x 12 = 6Total bill: 4 + 2 + 6 = 12
We have coded for Milk and its bill, but let’s code for the complete receipt listed above. What will need to be added to the code?
Generate code with AI
We don’t want to exaggerate the whole idea of using AI to generate code for us.
How about we learn to treat our AI co-partners as highly educated individuals, and we must learn to communicate with them effectively.
We need to be specific in our communication, in our expectations, and in helping it understand the context. We have to help AI help us. For starters, why don’t you tell the GPT what you want it to do for you? Here’s a good prompt for you.
Study this prompt, and then copy and paste it into the AI Copilot to generate the code:
I’m a beginner learning C++, and I’m trying to create a program that prints this receipt on the screen:=== MART RECEIPT ===Cashier Name: AlexItems Purchased:Milk (1): 4Bread (1): 2Eggs (12): 0.5 x 12 = 6Total bill: 4 + 2 + 6 = 12I’ve written a partial program for milk, and I want you to complete it for the full receipt:int milk_quantity = 1;double milk_price = 4.0;cout << "Milk (" << milk_quantity << "): " << milk_price << endl;double total_milk = milk_quantity * milk_price;cout << "Total bill: " << total_milk << endl;
Write your prompt in the AI Copilot below:
Test code with AI
Once the AI has generated the code, copy and paste it into the following code widget, run it, ask the AI Mentor for feedback, and
Food for thought
What does = really mean in C++? (Is it equality, or does it assign a value?)
What happens if you do bill = bill + 1;?
What’s the difference between cout << 10 + 10; and cout << “10” << “10”;?
Practice makes perfect
Now you’ve successfully used AI as your co-pilot to generate code, solve a problem, and you’ve tested the code; it works as well.
Now it’s time to test drive your learning. We’re only going to add one small feature to the point-of-sale problem. Imagine that the customer has come in; he’s done the same groceries, but he has a 20 percent discount coupon with him.
The point-of-sale and receipt generation system should have a way to adjust the bill based on the discount shown on the receipt.
Problem
This is what needs to be generated now. Notice how everything else remains the same, but at the end of it, before the total bill is computed discount gets adjusted in it:
Code its solution
The following code widget has the functional code, as before. All you need to do is add the code that handles the requirement for the discount adjustment. The final output should match the sample output shown above:
Perfect! Now, a more realistic POS app is ready to ship to customers.