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 solution in pseudocode (optional)

Before jumping into C++ code, let's write our solution in plain English (pseudo code):

START
    DECLARE length as a number
    DECLARE width as a number
    DECLARE area as a number

    PRINT "Enter the length of the rectangle: "
    READ length from user

    PRINT "Enter the width of the rectangle: "
    READ width from user

    SET area = length × width

    PRINT "The area of the rectangle is: " + area
END

This pseudocode helps us think through the logic before worrying about C++ syntax.

💡 About Pseudocode
The pseudocode above is just one example of how to think through a problem before coding. Pseudocode isn't necessary - it's simply a tool to help you get comfortable with breaking down problems into steps. If you prefer, you can skip formal pseudocode and just use natural English comments in your code like "Ask user for length" or "Calculate the area". The goal is to think through your solution before jumping into syntax!

4. First attempt: A rushed version

Let's see what happens when we rush and don't follow our pseudocode carefully:

#include <iostream>

int main() {
double l,w;
std::cout<<"length?";
std::cin>>l;
std::cout<<"width?";
std::cin>>w;
std::cout<<l*w;
return 0;
}

Problems with this version:

  • Poor variable names (l, w instead of descriptive names)
  • No spacing around operators (<< and >> are cramped)
  • Unclear prompts ("length?" vs "Enter the length of the rectangle:")
  • No explanation of what the output number means
  • Poor formatting and indentation
  • Doesn't follow our pseudocode structure

5. Improved version: Following our pseudocode

Now let's write it properly by following our pseudocode step by step:

#include <iostream>

int main() {
    // DECLARE length as a number
    double length;
    double width;
    double area;

    // PRINT "Enter the length..."
    std::cout << "Enter the length of the rectangle: ";
    // READ length from user
    std::cin >> length;

    // PRINT "Enter the width..."
    std::cout << "Enter the width of the rectangle: ";
    // READ width from user
    std::cin >> width;

    // SET area = length × width
    area = length * width;

    // PRINT "The area is: " + area
    std::cout << "The area of the rectangle is: " << area << std::endl;

    return 0;
}

Why this version is better:

  • Descriptive variable names (length, width, area)
  • Clear, helpful prompts for the user
  • Proper spacing and formatting
  • Comments that match our pseudocode
  • Explains what the output represents
  • Follows a logical structure

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

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
}

Other Basic Programs

We have included these ideas as exercises, complete as many you like:

  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!