To a non-programmer, writing computer programs can seem like a daunting task. This is hardly the case: creating a functional program only requires an idea and a little bit of knowledge. C++’s utility and versatility provide the ability to create whatever you can dream up.
This article looks at the importance of computer programming and tracks its evolution to the present day. After a brief introduction to C++, we’ll go over two basic examples in C++ that you can use as inspiration for your own project.
Why Is Computer Programming Important?
We’ve grown accustomed to seamlessly communicating on our mobile devices and making online purchases. Computer programming allows a computer to facilitate these tasks with minimal user instruction.
In fact, you would not even be able to read this article without computer programming. Computer programming powers your internet browser, which allows you to search the internet for an article and then read it. Your browser itself runs on your machine’s operating system, which handles all interactions between the software and hardware. Learning a programming language can give you a front-row seat to our ever-evolving lives in a high-tech world.
What Was the First Programming Language?
Historians credit Ada Lovelace with writing the world’s first computer program back in 1842. She worked with mathematician Charles Babbage, who would go on to create several designs for machines that would process punch cards to output to a printer, plotter, or a bell.
These designs became the foundation for computers, and Babbage became known as the father of the modern computer. Later, Ada added her thoughts to “Sketch of The Analytical Engine Invented by Charles Babbage”, an article about Babbage’s machine written by L. F. MENABREA. In that article, she noted how such a machine could use codes to repeat a series of instructions. Looking back, Ada’s line of thinking falls perfectly in line with today’s computer programs.
One hundred years later, we would see programming languages designed for actual computers. Konrad Zuse developed Plankalkül, the very first computer programming language in the mid-1940s.
Others like Autocode, Short Code, and COBOL began to appear throughout the 50s and 60s. C, the precursor to C++, would not appear until 1972.
How Has Programming Evolved Over the Years?
As the United States Apollo missions began to take shape in the 1960s, software engineers like Margaret Hamilton used typewriters to create mountains of code for the program. The computers that sent man to the Moon over 50 years ago were thousands of times less powerful than the smartphone you use today.
While the Apollo spacecraft had computers, the cars of that era did not. Computers in motorized vehicles would not appear until the 1970s, which saw an uptick in gasoline and emissions regulations.
With improvements in technology, engineers would develop computer programs to control a vehicle’s speed, braking systems, and even interior climate control. Today, our cars can act as WiFi hotspots and even drive themselves. Auto manufacturers still use C++ for a lot of these programs.
What Is C++?
C++ is a general-purpose programming language used to develop operating systems, games, web browsers, and much more. Bjarne Stroustrup began work on what would become C++ in 1979, with the hope that the language could be used anywhere and for any purpose.
The International Organization for Standardization (ISO) has revised C++ six times since its creation. The first was in 1998 when the ISO released a C++ standard for the first time. The same organization released the four most recent versions in the last ten years, with the latest update coming in December 2020.
Creating an Example in C++
Being a general-use language, you can use C++ to create any type of program imaginable. These programs can be practical or just plain fun. We’ve created two examples below that demonstrate both the fun and practical side of programming.
High or Low Guessing Game
The first example is a game that starts you off with a random “card” numbered from one to ten. Your task is to guess if the following random card will be higher or lower than the one prior.
Here’s how it looks in code:
...
int main() {
char guess;
int card1, card2, card3;
int lowest = 1, highest = 10;
int range = (highest - lowest) + 1;
srand(time(NULL));
card1 = lowest + rand() % range;
...
The program starts with variable initializations and declarations. The character variable guess will represent the user’s guess each time the program draws a new card. This example uses three different cards, with card1 revealed to the user at the beginning. The user has to make two correct guesses to win.
srand(time(NULL)) allows these cards to be different numbers each time we run the program. lowest + rand() % range is the command that creates a random number and assigns that number value to the variable. We repeat this three times, once for each card. The block of text below is our introduction to the game:
...
cout << "Welcome to the high/low guessing game. You're given a starting card from one to ten, and must guess if each card in the sequence is higher or lower than the previous one by typing 'h' or 'l' in the prompt.\nYour starting card is a " << card1 << "." << endl;
...
This should give the user the information they need to play, along with the value of their first card. The code below checks to see if the user’s guess is right:
...
cout << "Do you think the next card is higher or lower? ";
cin >> guess;
if (card2 - card1 > 0 && guess == 'h' || card2 - card1 < 0 && guess == 'l') {
cout << "You're right! The card was a " << card2 << ".\n\n";
}
else {
cout << "Sorry, wrong. The card was a " << card2 << ". Try again next time.\n";
sleep_for(seconds(5));
exit(0);
...
This above sequence of code becomes the meat of our game, and we repeat it a total of two times. With the user “holding” their first card, we ask them if they think the next random card is higher or lower.
If the user’s first card is a high value, chances are the next card will be lower. The exciting thing about this game is that this will not always be the case!
We use the cin object to accept input from the user. The user will then type either “h” if they believe the next card is higher, or “l” if they think the next card is lower.
Our program compares the two cards, and takes the user’s guess to determine whether they were right or wrong. The program uses logical operators AND (&&) and OR (||) to check whether the user’s guess is correct.
Using an if/else statement, the program provides the user one of two outputs. The user either receives a reply that they guessed correctly and the value of the next card, or a message informing them that their guess was wrong and to try again next time.
If the user guesses incorrectly, sleep_for(seconds(5)) pauses the program for 5 seconds, giving the user time to read the text. The program then terminates until a user decides to play.
Should a user guess correctly both times, we send them off with the following message:
Congratulations! You just won one million dollars!
The check, of course, is in the mail.
Recipe Converter Calculator
Games are undoubtedly fun, but we can also use C++ to create more practical programs. Here’s a breakdown of a program that converts U.S. measurements to their metric equivalents:
...int main()
{
int choice; float amount, convert;
char check;
cout << "Welcome to the U.S. to metric recipe measurement converter. Please type the number of the conversion you want to make.\n";...
As with our previous program, we start by declaring the variables we need to make our program function. choice represents the user’s selected conversion, whereas amount is the value of that measurement.
convert is the resulting amount of the conversion between U.S. and metric values. check at the end of the program looks to see if the user wants to perform another conversion.
... int i = 0;
while (i == 0) {
cout << "Volume:\n1. Teaspoons to milliliters\n";
cout << "2. Tablespoons to milliliters\n";
cin >> choice;...
Our while loop tells the program to continue looping back to the top of the while loop once it reaches the end, for as long as i is 0. The program will then continue from the beginning of the while loop. As you’ll see below, i will only change values once the user indicates that they will no longer be using the program.
The while loop contains a list of two options for converting U.S. measurements to their metric equivalents, ending with a cin command that asks the user which conversion they want the program to perform.
Once the user indicates their choice by typing in the corresponding number, the program continues to one of eight if statements like this one:
...
if (choice == 2) {
cout << "How many tablespoons do you wish to convert? ";
cin >> amount;
convert = amount * 15;
cout << amount << " tablespoon(s) yields " << convert << " millimeters.\n";
...
Here, the program asks the user how many tablespoons they want to convert. Because we assigned amount as a float, the user can even input a decimal number. The program does the conversion, assigns the converted measurement to convert, and then outputs that value to the user.
...
cout << "Do you wish to perform another conversion? (Y/N): ";
cin >> check;
cout << endl;
if (check == 'N' || check == 'n') {
i++;
cout << "Thanks for using this program!";
At the end of the program, the program checks to see if the user wants to perform another conversion. Typing anything other than ‘N’ or ‘n’ sends the program back up to the top of the while loop. Typing ‘N’ or ‘n’ causes the value of i to increment to 1. Once this happens, the condition in the while loop is no longer true, and the program ends.
It goes without saying that you can add any number of conversions to your program!
Learn Programming With Udacity
Programming has been around for nearly two centuries, and has forever changed the way we live. C++ is one of the world’s most used languages, and we can see it today in cars, operating systems, browsers, and games.
Ready to start your C++ programming journey?
If you’re a complete beginner, check out Udacity’s Introduction to Programming nanodegree.
If you have some coding experience and want to dive right into C++, enroll in our C++ nanodegree instead.
Complete Code Examples
Example 1: Guess if the next card is higher or lower
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main() {
char guess;
int card1, card2, card3;
int lowest = 1, highest = 10;
int range = (highest - lowest) + 1;
srand(time(NULL));
card1 = lowest + rand() % range;
card2 = lowest + rand() % range;
card3 = lowest + rand() % range;
cout << "Welcome to the high/low guessing game. You're given a starting card from one to ten, and must guess if each card in the sequence is higher or lower than the previous one by typing 'h' or 'l' in the prompt.\nYour starting card is a " << card1 << "." << endl;
cout << "Do you think the next card is higher or lower? ";
cin >> guess;
if (card2 - card1 > 0 && guess == 'h' || card2 - card1 < 0 && guess == 'l') {
cout << "You're right! The card was a " << card2 << ".\n\n";
}
else {
cout << "Sorry, wrong. The card was a " << card2 << ". Try again next time.\n";
sleep_for(seconds(5));
exit(0);
}
cout << "Do you think the next card is higher or lower? ";
cin >> guess;
if (card3 - card2 > 0 && guess == 'h' || card3 - card2 < 0 && guess == 'l') {
cout << "Congratulations! You just won one million dollars!";
sleep_for(seconds(5));
exit(0);
}
else {
cout << "Sorry, wrong. The card was a " << card3 << ". Try again next time.";
sleep_for(seconds(5));
exit(0);
}
return 0;
}
Example 2: A U.S. to metric recipe measurement converter
#include <iostream>
using namespace std;
int main()
{
int choice; float amount, convert;
char check;
cout << "Welcome to the U.S. to metric recipe measurement converter. Please type the number of the conversion you want to make.\n";
int i = 0;
while (i == 0) {
cout << "Volume:\n1. Teaspoons to milliliters\n";
cout << "2. Tablespoons to milliliters\n";
cin >> choice;
if (choice == 1) {
cout << "How many teaspoons do you wish to convert? ";
cin >> amount;
convert = amount * 5;
cout << amount << " teaspoon(s) yields " << convert << " millimeters.\n";
}
if (choice == 2) {
cout << "How many tablespoons do you wish to convert? ";
cin >> amount;
convert = amount * 15;
cout << amount << " tablespoon(s) yields " << convert << " millimeters.\n";
}
cout << "Do you wish to perform another conversion? (Y/N): ";
cin >> check;
cout << endl;
if (check == 'N' || check == 'n') {
i++;
cout << "Thanks for using this program!";
}
}
return 0;
}



