C++ Notes: Example - Celsius to Fahrenheit

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
// basics/c2f.cpp - Convert Celsius to Fahrenheit 
// Fred Swartz, 2003-10-26                 // (1)

#include <iostream>                        // (2)
using namespace std;                       // (3)

int main() {                               // (4)
    float celsius;                         // (5)
    float fahrenheit;
    
    cout << "Enter Celsius temperature: "; // (6)
    cin >> celsius;
    fahrenheit = 1.8 * celsius + 32;
    cout << "Fahrenheit = " << fahrenheit << endl;
    
    system("PAUSE");  // only Dev-C++      // (7)
    return 0;                              // (8)
}
The text from the above example can be selected, copied, and pasted into an editor.

Notes

  1. Comments at the beginning should include the following.
  2. #include statements make standard libary functions available.
  3. The using namespace std statement allows access to standard library names (eg, cin and cout) without a prefix (std::cin and std::cout).
  4. Execution of every program starts in the main() function. There is an alternate way to declare main, but we will use this simple style.
  5. Local variables are declared at the beginning of every function.
  6. The computation is often a variation of read-process-write.
  7. This system call is only necessary for DevC++ users to keep the console window from closing when the program terminates. It is NOT standard C++.
  8. The main() function is declared to return an int, therefore it must return some integer value. Zero is commonly used to indicate that the program terminated successfully, but the system normally ignores this number.