1.11 — Developing your first program

Now that you understand the basics of C++ syntax, let's put it all together to develop a complete program from start to finish. This lesson will walk you through the process of planning, writing, and testing a simple but practical program.

The development process

1. Define the problem

Let's create a program that calculates the area of a rectangle. The user will input the length and width, and the program will calculate and display the area.

2. Plan the solution

Our program needs to:

  • Prompt the user for length and width
  • Read the user's input
  • Calculate the area (length × width)
  • Display the result

3. Write the code

#include <iostream>

int main() {
    // Step 1: Declare variables to store input and result
    double length;
    double width;
    double area;
    
    // Step 2: Greet user and explain what the program does
    std::cout << "Rectangle Area Calculator" << std::endl;
    std::cout << "========================" << std::endl;
    
    // Step 3: Get length from user
    std::cout << "Enter the length of the rectangle: ";
    std::cin >> length;
    
    // Step 4: Get width from user
    std::cout << "Enter the width of the rectangle: ";
    std::cin >> width;
    
    // Step 5: Calculate the area
    area = length * width;
    
    // Step 6: Display the result
    std::cout << std::endl;
    std::cout << "Results:" << std::endl;
    std::cout << "Length: " << length << std::endl;
    std::cout << "Width:  " << width << std::endl;
    std::cout << "Area:   " << area << std::endl;
    
    return 0;
}

Code breakdown

Let's examine each part of our program:

Headers and main function

#include <iostream>

int main() {
    // ... program code ...
    return 0;
}
  • Include necessary headers (<iostream> for input/output)
  • Define main() function where program execution begins
  • Return 0 to indicate successful execution

Variable declarations

double length;
double width;
double area;
  • Use double for decimal numbers (better for measurements)
  • Declare variables before using them
  • Choose descriptive names

User interaction

std::cout << "Enter the length of the rectangle: ";
std::cin >> length;
  • Use std::cout to display prompts
  • Use std::cin to read user input
  • Make prompts clear and specific

Calculations

area = length * width;
  • Perform calculations using expressions
  • Store results in variables for later use

Output formatting

std::cout << "Length: " << length << std::endl;
std::cout << "Width:  " << width << std::endl;
std::cout << "Area:   " << area << std::endl;
  • Display results in a clear, formatted manner
  • Use descriptive labels
  • Align output for readability

Testing the program

Sample run 1:

Rectangle Area Calculator
========================
Enter the length of the rectangle: 5.5
Enter the width of the rectangle: 3.2

Results:
Length: 5.5
Width:  3.2
Area:   17.6

Sample run 2:

Rectangle Area Calculator
========================
Enter the length of the rectangle: 10
Enter the width of the rectangle: 8

Results:
Length: 10
Width:  8
Area:   80

Improving the program

Here's an enhanced version with better formatting and additional features:

#include <iostream>
#include <iomanip>  // For formatting output

int main() {
    double length;
    double width;
    double area;
    double perimeter;
    
    // Program header
    std::cout << "=================================" << std::endl;
    std::cout << " Rectangle Area & Perimeter Calc " << std::endl;
    std::cout << "=================================" << std::endl;
    std::cout << std::endl;
    
    // Get input with validation prompts
    std::cout << "Please enter rectangle dimensions:" << std::endl;
    
    std::cout << "Length (in units): ";
    std::cin >> length;
    
    std::cout << "Width (in units):  ";
    std::cin >> width;
    
    // Perform calculations
    area = length * width;
    perimeter = 2 * (length + width);
    
    // Display results with formatting
    std::cout << std::endl;
    std::cout << "RESULTS:" << std::endl;
    std::cout << "--------" << std::endl;
    std::cout << std::fixed << std::setprecision(2);  // 2 decimal places
    std::cout << "Rectangle length:    " << length << " units" << std::endl;
    std::cout << "Rectangle width:     " << width << " units" << std::endl;
    std::cout << "Rectangle area:      " << area << " square units" << std::endl;
    std::cout << "Rectangle perimeter: " << perimeter << " units" << std::endl;
    
    std::cout << std::endl;
    std::cout << "Thank you for using the Rectangle Calculator!" << std::endl;
    
    return 0;
}

Development best practices

1. Start simple

  • Begin with a basic version that works
  • Add features incrementally
  • Test each addition

2. Use meaningful names

// Good
double length;
double width;
double area;

// Poor
double l;
double w;
double a;

3. Add comments for clarity

// Calculate area using length × width formula
area = length * width;

// Display results in formatted table
std::cout << "Area: " << area << std::endl;

4. Format for readability

// Good spacing and alignment
std::cout << "Length: " << length << std::endl;
std::cout << "Width:  " << width << std::endl;
std::cout << "Area:   " << area << std::endl;

5. Test with different inputs

  • Test with integers and decimals
  • Test with small and large numbers
  • Consider edge cases (like zero)

Common beginner mistakes

1. Forgetting to declare variables

// Wrong - using undeclared variable
std::cin >> length;  // Error: length not declared

// Right - declare first
double length;
std::cin >> length;

2. Using wrong data types

// Might lose decimal precision
int length = 5.7;    // 5.7 becomes 5

// Better for measurements
double length = 5.7; // Keeps decimal precision

3. Forgetting return statement

int main() {
    // ... program code ...
    // Missing return 0; causes warning
}

Challenge exercises

Try these modifications to practice:

  1. Triangle Area Calculator: Calculate area of a triangle (area = 0.5 × base × height)
  2. Circle Calculator: Calculate area and circumference of a circle
  3. Temperature Converter: Convert Fahrenheit to Celsius
  4. Age Calculator: Calculate age in days from birth year

Summary

Developing a program involves:

  1. Understanding the problem - what needs to be solved?
  2. Planning the solution - what steps are needed?
  3. Writing the code - implement the solution step by step
  4. Testing thoroughly - verify it works with different inputs
  5. Improving iteratively - add features and polish

The key is to start simple and build complexity gradually. Every expert programmer started with simple programs like this rectangle calculator!