0
0
C++programming~15 mins

Assignment operators in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Assignment Operators in C++
📖 Scenario: You are helping a small shop keep track of the number of items in stock. You will use assignment operators to update the stock count as items are added or sold.
🎯 Goal: Build a simple C++ program that uses assignment operators to update the stock count of items in a shop.
📋 What You'll Learn
Create an integer variable stock with an initial value.
Create an integer variable new_arrivals to represent items added to stock.
Use assignment operators += and -= to update stock.
Print the final value of stock.
💡 Why This Matters
🌍 Real World
Shops and businesses often need to keep track of inventory and update counts as items arrive or are sold.
💼 Career
Understanding assignment operators is essential for managing data and performing calculations in software development.
Progress0 / 4 steps
1
Create the initial stock variable
Create an integer variable called stock and set it to 50 to represent the starting number of items in stock.
C++
Need a hint?

Use int stock = 50; to create the variable.

2
Add new arrivals variable
Create an integer variable called new_arrivals and set it to 20 to represent new items added to the stock.
C++
Need a hint?

Use int new_arrivals = 20; to create the variable.

3
Update stock using assignment operators
Use the += operator to add new_arrivals to stock. Then use the -= operator to subtract 15 items sold from stock.
C++
Need a hint?

Use stock += new_arrivals; and stock -= 15; to update the stock.

4
Print the final stock count
Write a std::cout statement to print the text "Final stock: " followed by the value of stock.
C++
Need a hint?

Use std::cout << "Final stock: " << stock << std::endl; to print the result.