The hello program, which you saw earlier, doesn’t include many features you’d expect in a "real" program. To illustrate more of C++, let's consider the program named f2c which converts Fahrenheit temperatures to Celsius.
This is an IPO or Input Processing Output program, typical of those that we'll be building in these first few weeks. A computer is an information processor; a Cuisinart for words, numbers and ideas. Instead of vegetables, you feed it input; raw numbers, facts, figures and symbols (data). Your computer program processes that data, and turns it into information: organized, meaningful and useful output.
The programs we'll be writing are console-mode, IPO programs. These are plain-text programs, which run inside your terminal, and follow this pattern:
- prompt the user to enter some input
- read the user's input, storing it in variables
- process the input
- output the results
- The f2c program asks the user for a temperature in Fahrenheit. This is the prompt. Note that the prompt appears on the same line as the input and ends in a space, so that the input is nicely separated.
- The program stops and waits for the user to enter in some input and press ENTER. In this case, the user entered 212 for the temperature in Fahrenheit. The program reads the user input and stores it in a variable.
- Next, the program uses an algorithm to convert the Fahrenheit temperature into Celcius.
- Finally, it diplays the result on the console.
Here's an example:
The screenshot above is called a sample run. It shows the input and output in the terminal or shell. Now, let's take a closer look at the f2c program itself.
Below you'll find the code for the f2c program. Use the arrow on the left to show and hide the code as we discuss its various features.
The f2c Source Code
/**
* @file f2c.cpp
* @author Stephen Gilbert
* @version CS 150 Reader
* Converts Fahrenheit to Celsius.
*/
#include <iostream>
#include <iomanip>
using namespace std;
double convert(double temp);
int main()
{
cout << "Enter a temperature in Fahrenheit: ";
double fahr;
cin >> fahr;
double celsius = convert(fahr);
cout << "Converted: " << fahr << "F -> "
<< celsius << "C" << endl;
return 0;
﹜
/**
* Converts a temperature from Fahrenheit to Celsius.
* @param temp the Fahrenheit temperature to convert.
* @return the Celsius temperature.
*/
double convert(double temp)
{
return (temp - 32) * 5.0 / 9.0;
﹜
Comments
A comment is text provided for readers of your code; comments are ignored by the compiler. C++ has two kinds of comments:
- Single-line (//) are comments that end when the line ends.
- Paired (/* … */) are comments which can enclose several lines.
Documentation comments(/** … */) are paired comments, which start with /** instead of /*. The are processed using a tool called Doxygen. The f2c program begins with a documentation file comment (lines 1-6), which describes the program as a whole, and uses three Doxygen tags:
- @file contains the name of the program file and allows Doxygen to locate and identify the functions which it needs to document.
- @author should your CS 150 login id (such as sgilbert).
- @version should contain your CS 150 section (such as Spring '24 MWAM)
Functions also have documentation comments appearing before each one. Such comments start with a line describing the purpose of the functions, and then contain tags which describe the function inputs and outputs:
- @param is the name and description of each input, or parameter.
- @return describes the meaning of the output produced by the function.
Lines 24-28 in the sample code contain the documentation comment for the convert function. You'll learn more about documentation comments in the homework.
Below you'll find the code for the f2c program. Use the arrow on the left to show and hide the code as we discuss its various features.
The f2c Source Code
/**
* @file f2c.cpp
* @author Stephen Gilbert
* @version CS 150 Reader
* Converts Fahrenheit to Celsius.
*/
#include <iostream>
#include <iomanip>
using namespace std;
double convert(double temp);
int main()
{
cout << "Enter a temperature in Fahrenheit: ";
double fahr;
cin >> fahr;
double celsius = convert(fahr);
cout << "Converted: " << fahr << "F -> "
<< celsius << "C" << endl;
return 0;
}
/**
* Converts a temperature from Fahrenheit to Celsius.
* @param temp the Fahrenheit temperature to convert.
* @return the Celsius temperature.
*/
double convert(double temp)
{
return (temp - 32) * 5.0 / 9.0;
}
Libraries are collections of useful, pre-written components (functions and classes). The standard library comes with your C++ compiler. It is divided into a number of "packages", known as headers in C++. Here are the headers you'll use most often. You'll meet more as the semester goes on. Find the whole list at cppreference.com.
// input-output
//reading and printing
#include <iostream>
// formatting output
#include <iomanip>
// all math functions
#include <cmath>
// The C++ string class
#include <string>
The #include directive instructs the preprocessor to read the declarations from the header file and insert them, exactly as if you had typed them into your source code.
- Instructions to the preprocessor are called preprocessor directives; these always appear on a line by themselves, always start with a #, and do not end in a semicolon.
- Angle brackets indicate a header is a system library, part of standard C++.
In f2c, you'll find the #include statements on lines 7-8.
The Standard Namespace
In C++, libraries are combined into larger groups called namespaces. The standard library is in the namespace called std, usually pronounced standard instead of "es-tee-dee". For CS 150, add a using directive to the top of your source code, like this:
using namespace std;
In f2c, you find this on line 9. Think of using namespace std; as one more incantation that the C++ compiler requires to work its magic on your code. This only works correctly inside .cpp file. In header files, you must use a different technique, which we'll cover in lecture.
Below you'll find the code for the f2c program. Use the arrow on the left to show and hide the code as we discuss its various features.
The f2c Source Code
/**
* @file f2c.cpp
* @author Stephen Gilbert
* @version CS 150 Reader
* Converts Fahrenheit to Celsius.
*/
#include <iostream>
#include <iomanip>
using namespace std;
double convert(double temp);
int main()
{
cout << "Enter a temperature in Fahrenheit: ";
double fahr;
cin >> fahr;
double celsius = convert(fahr);
cout << "Converted: " << fahr << "F -> "
<< celsius << "C" << endl;
return 0;
}
/**
* Converts a temperature from Fahrenheit to Celsius.
* @param temp the Fahrenheit temperature to convert.
* @return the Celsius temperature.
*/
double convert(double temp)
{
return (temp - 32) * 5.0 / 9.0;
}
A function is a named section of code that performs an operation. Every C++ program must contain exactly one function with the name main, which is automatically called when your program starts up.
- Each statement in the body of the main function is then run (or executed), one after another, in order. This concept is called sequence.
- When main has finished its work, execution of the program ends.
The main function contains six different statements.
- Line 15 is an output statement. It prints a prompt, telling the user what to enter. cout is the standard output stream (similar to System.out in Java or stdout in Python). The characters in quotes (generically called a string literal) are sent to the screen using the insertion operator (<<).
- Line 16 is a variable definition for fahr, a floating-point number, called double in C++, which is uninitialized.
- Line 17 is an input statement. cin is similar to a Scanner object in Java, or a file object in Python. It reads a sequence of characters from the keyboard and stores the converted value in fahr using the >> (or extraction) operator.
- Line 18 has both a variable definition and a function call. The line calls the function named convert, passing a copy of fahr as an argument. Then, it defines a variable, celsius, and initializes it with the returned value.
- Lines 19 and 20 are a single output statement, spread over two lines. The statement combines text and variables to produce the desired result.
- Line 21 is a return statement which ends the program and returns a value to the operating system. 0 indicates success, while anything else signals failure. You may omit this return statement in main, but not in any other function.
Below you'll find the code for the f2c program. Use the arrow on the left to show and hide the code as we discuss its various features.
The f2c Source Code
/**
* @file f2c.cpp
* @author Stephen Gilbert
* @version CS 150 Reader
* Converts Fahrenheit to Celsius.
*/
#include <iostream>
#include <iomanip>
using namespace std;
double convert(double temp);
int main()
{
cout << "Enter a temperature in Fahrenheit: ";
double fahr;
cin >> fahr;
double celsius = convert(fahr);
cout << "Converted: " << fahr << "F -> "
<< celsius << "C" << endl;
return 0;
}
/**
* Converts a temperature from Fahrenheit to Celsius.
* @param temp the Fahrenheit temperature to convert.
* @return the Celsius temperature.
*/
double convert(double temp)
{
return (temp - 32) * 5.0 / 9.0;
}
In addition to main, the f2c program uses the function convert which the main function calls (on line 18) to carry out its work. Before main can call convert, though, the compiler must know what kind of arguments the function requires, and what kind of value it will return. This information is called the function's interface, and it is supplied by adding a function declaration or prototype, appearing before the main function, (on line 11).
double convert(double temperature);
The prototype provides the information needed to call the function: its name along with the types of its inputs and outputs. C++ requires prototype declarations so the compiler can check whether calls to functions are compatible with the definitions of those functions.
The convert function definition, starting on line 29, repeats the interface information from the prototype, butdoes not end in a semicolon. Instead, the definition header is followed by a body (just like main) consisting of a list of statements surrounded (or delimited) by braces {}.
double convert(double temperature)
{
return (temp - 32) * 5.0 / 9.0;
}
The body of convert is a single return statement which uses a formula or expression to convert the input Fahrenheit temperature into the output Celsius temperature, and return it to the caller.