Skip to content

Lab · C++ Programming (modern C++ and DSA)

Lab: a chai stall's day in C++

Labbeginner· about 1.5 hours· 4 steps

The brief

Sharma ji runs a chai stall outside a college in Pune and wants a small program for his day's sales. You compile your first program with warnings on, work out the sales with variables, print money with two decimals without the integer-division trap, and then read the numbers from the keyboard.

Why it matters

Every C++ job starts with the same loop: write, compile with warnings, run, read the output. Billing counters, ticket machines and point-of-sale software all do this kind of arithmetic, and the integer-division and leftover-newline bugs you meet here are the ones that cost real shops money.

Skills you practise

  • g++ with -Wall -Wextra
  • std::cout and chaining <<
  • int, double and const
  • integer division and static_cast
  • fixed and setprecision
  • std::cin, std::ws and std::getline

You need

  • g++ 11 or newer (MinGW-w64 via MSYS2 on Windows, build-essential on Ubuntu, Xcode command line tools on macOS, where g++ runs Apple clang)
  • A terminal: Git Bash, MSYS2 or PowerShell on Windows; Terminal on macOS or Linux
  • A text editor or VS Code

What you will have at the end

chai.cpp: a program that reads a stall's counts and name and prints its sales, average per hour and chai share, building with no warnings.

How it is marked (rubric, 100 points)
Correct resultsEvery check passes: 2319 and 257.67 with the fixed numbers, 2739 and 304.33 with the typed input.45
Clean buildg++ -Wall -Wextra prints no warnings or errors.20
Readable codePrices are named constants, variables have clear names and are always initialised.20
ExplainedYou can explain integer division and why std::ws is needed before getline.15

0/4 steps done

Step 1 of 4

Compile and run your first program

chai.cpp compiles with warnings on and prints "Namaste from Sharma Chai Stall!".

First check that a compiler is installed: g++ --version prints its name and version. Then add one std::cout line to main that prints the greeting followed by '\n'. Compile with -std=c++17 -Wall -Wextra so the compiler warns about likely mistakes; a clean build prints nothing at all. On Windows the program is chai.exe, so run chai.exe (or .\chai.exe in PowerShell) instead of ./chai.

int main() {
    std::cout << "Namaste from Sharma Chai Stall!\n";
    return 0;
}
One statement, ending with a semicolon.
Run this on your machine, then paste the output below
$ g++ --version; g++ -std=c++17 -Wall -Wextra -o chai chai.cpp; ./chai
Your notes: chai.cpp

Write down each command you ran. It goes into your project summary.