If you need refreshers on earlier syntax, see the syntax examples from lab 1. |
// constants can either go near the top (like the example below) // or inside the main routine (or functions when we get to them) // like variables, constants must be declared before (above) // the point where they are first used // // For constants you specify the type, name, and (mandatory) initial value, // and also use the const keyword to indicate it is a constant. // constant for Pi const double Pi = 3.14; int main() { // ... } |
#include <iostream> const double Pi = 3.14; int main() { // circle radius, in metres, to be supplied by user double radius; // computed diameter, area, circumference double diameter, area, circumference // get the radius from the user std::cout << "Enter the circle radius (in metres), e.g. 5.7" << std::endl; std::cin >> radius; // compute the diameter, area, circumference diameter = 2 * radius; circumference = Pi * diameter; area = Pi * radius * radius; // display the results std::cout << "For a circle of radius " << radius << ":" << std::endl; std::cout << " the diameter is " << diameter << std::endl; std::cout << " the circumference is " << circumference << std::endl; std::cout << " the area is " << area << std::endl; return 0; <= will discuss this later } |
// include the cmath library #include <cmath> int main() { double result; // compute the square root of 32 and store it in result result = sqrt(32); // compute 5 raised to the power of 3 (i.e. 5*5*5) and store it result = pow(5, 3); } |
#include <iostream> #include <iomanip> int main() { // a few variables we'll use in the examples double d = 12.3456789; int i = 123; char c = 'x'; // printing a variety of items, each padded with spaces on the left to fit a certain size std::cout << std::setw(4) << i << std::endl; // pads 123 to width 4 std::cout << std::setw(6) << i << std::endl; // pads 123 to width 6 std::cout << std::setw(2) << i << std::endl; // no effect since 123 is already wider than 2 // specify fixed width precision will be in use std::cout << std::setiosflags(std::ios::fixed); // specify 2 digits after the decimal point std::cout << std::setprecision(2) << d << std::endl; // prints 12.35 (rounds up the .345 to .35) } |
short dayOfMonth; int cityPopulation; long worldPopulation; float itemCost; double bigCalculatedValue; |
#include <climits> #include <cfloat> #include <iostream> int main() { std::cout << "The biggest short value is " << SHRT_MAX << std::endl; std::cout << "The biggest int value is " << INT_MAX << std::endl; std::cout << "The biggest long value is " << LONG_MAX << std::endl; std::cout << "The biggest float value is " << FLT_MAX << std::endl; std::cout << "The biggest double value is " << DBL_MAX << std::endl; std::cout << "The maximum precision of a float is " << FLT_DIG << " digits" << std::endl; std::cout << "The maximum precision of a double is " << DBL_DIG << " digits" << std::endl; } |
// show that integer division drops fractional portions int x = 7; int y = 3; std::cout << "The result of dividing 7 by 3 is: " << (x/y) << std::endl; // show the computation of remainders in integer division using the modulo (%) operator std::cout << "The remainder after dividing 7 by 3 is: " << (x%y) << std::endl; |
// declare two character variables char initial; char testValue; // assign the character 'Q' to a variable testValue = 'Q'; // read a single typed character from user input std::cout << "Please enter your first initial, e.g. D" << std::endl; std::cin >> initial; // display chars using cout std::cout << "You entered " << initial << std::endl; std::cout << "The char we stored in variable testValue was " << testValue << std::endl; // using the special tab (\t) and newline (\n) characters char tab = '\t'; char newline = '\n'; std::cout << "Printing a tab does this ..." << tab << "..." << std::endl; std::cout << "Printing a newline does this ..." << newline << "..." << std::endl; |
#include <iostream> #include <iomanip> int main() { // a few variables we'll use in the examples double d = 12.3456789; int i = 123; char c = 'x'; // printing a variety of items, each padded with spaces on the left to fit a certain size std::cout << std::setw(4) << i << std::endl; // pads 123 to width 4 std::cout << std::setw(6) << i << std::endl; // pads 123 to width 6 std::cout << std::setw(2) << i << std::endl; // no effect since 123 is already wider than 2 // specify fixed width precision will be in use std::cout << std::setiosflags(std::ios::fixed); // specify 2 digits after the decimal point std::cout << std::setprecision(2) << d << std::endl; // prints 12.35 (rounds up the .345 to .35) } |
#include <iostream> // prototype for a function that takes no parameters and returns no value void printSomething(); // prototype for a function that takes no parameters // and returns a value of type integer int getAValue(); // prototype for a function that takes two parameters (an int and a float) // and returns a value of type double double multiply(int x, float y); // a main routine calling each of the three functions listed above int main() { int a = 1; float b = 2.0; double c; printSomething(); a = getAValue(); c = multiply(a, b); return 0; } // ===== implementations of the three functions ===== // prototype for a function that takes no parameters and returns no value void printSomething() { std::cout << "Something!" << std::endl; } // prototype for a function that takes no parameters // and returns a value of type integer int getAValue() { // getting the user to type in a value, and return whatever it is int v; std::cout << "Please enter an integer value" << std::endl; std::cin >> v; return v; } // prototype for a function that takes two parameters (an int and a float) // and returns a value of type double double multiply(int x, float y) { // compute and return the product of x and y double result = x * y; return result; } |