Absolute Beginners C++


Lesson 3 - Simple Expressions

3.1 Integer Expressions 

  
// ex3-1.cpp - integer arithmetic
#include <iostream>
using namespace std;
const int TILES_PER_SQ_METRE=9;
const int TILES_PER_BOX=25;
int main()
{
    cout << "Tile calculator" << endl;
    cout << "Enter length of room (m) : ";
    int length;
    cin >> length;
    cout << "Enter width of room (m) : ";
    int width;
    cin >> width;
    cout << "You will need "
         << 1+(length*width*TILES_PER_SQ_METRE)
           /TILES_PER_BOX
         << " boxes." << endl;
    return(0);
}
ex3-1
Tile calculator
Enter length of room (m) : 4
Enter width of room (m) : 3
You will need 5 boxes.
Example ex3-1.cpp introduces some more complex arithmetic expressions. It also introduces the idea of a compile-time constant. The program calculates how many boxes of tiles you need to tile a floor. It knows the size of each tile, inputs the size of the room, and does a simple calculation.

The instruction 'const int TILES_PER_SQ_METRE=9;' looks like a variable declaration, which it is, but with a special characteristic. This instruction defines a memory location called TILES_PER_SQ_METER and fills that location with the integer 9. However that location is also declared as 'const', i.e. constant. This means the content of the variable cannot be changed while the program is running. The value of TILES_PER_SQ_METER will always be 9. The only way for the value to be changed is to change the number in the source file before the file is compiled. 

In the last output instruction in the program you can see a more complex arithmetic expression: the symbol '*' means multiply, and the symbol '/' means divide. Parentheses can be used to control the order in which the expression is evaluated. 

3.2 Floating Point Expressions

// ex3-2.cpp - floating-point arithmetic
#include <iostream>
using namespace std;
const double TILE_SIZE=0.3; // each tile = 30cm
const int TILES_PER_BOX=25; // 25 tiles per box
int main()
{
    cout << "Tile calculator" << endl;
    cout << "Enter length of room (m) : ";
    double length;
    cin >> length;
    cout << "Enter width of room (m) : ";
    double width;
    cin >> width;
    int nwide = 1+(int)(width/TILE_SIZE);
    int nlong = 1+(int)(length/TILE_SIZE);
    cout << "You will need "
        << 1+(nwide*nlong)/TILES_PER_BOX
        << " boxes." << endl;
    return(0);
}
ex3-2
Tile calculator
Enter length of room (m) : 4.5
Enter width of room (m) : 3.5
You will need 8 boxes.
The previous example is modified in example ex3-2.cpp. Here the actual sizes of the tiles is specified, and the fact that we need a whole number of tiles to fit across and along the room is explicitly acknowledged. 
The constant TILE_SIZE is set to the fractional number 0.3. The width and the length of the room are now fractional numbers also. To find the number of tiles needed to fit across the room we need the whole number that is bigger than width/TILE_SIZE. We can find this by adding 1 to the whole number part of the fraction width/TILE_SIZE. To take the whole number part we 'cast' the fractional expression into the form of an integer with the operation '(int)' in front of the expression. Thus if width = 3.15 meters, then width/TILE_SIZE would be equal to the value 10.5, and (int)(width/TILE_SIZE) would equal 10, and nwide would equal 11 - the number of tiles we would need to fit across the room. 

3.3 String expressions

// ex3-3.cpp - String manipulation
#include <iostream>
#include <string>
using namespace std;
int main()
{
    // get the users full name
    cout << "Enter your full name : ";
    string fullname;
    getline(cin,fullname);
    // locate the first space character
    int nsep = fullname.find(" ");
    // split into two parts
    string firstname = fullname.substr(0,nsep); 
    string lastname = fullname.substr(nsep+1);
    // and put it back together
    string newname = lastname + ", " + firstname;
    cout << "User: " << newname << endl;
    return(0);
}
ex3-3
Enter your full name : Mark Huckvale
User: Huckvale, Mark
Example ex3-3.cpp introduces some string expressions: some operations that you can do with string variables. In particular the example shows how a whole line of text can be read from the keyboard, how a specific character can be searched for, how a sub-sequence of characters can be copied out from a string, and how strings can be concatenated to form new strings. 
The instruction 'getline(cin,string-variable)' reads a whole line of text from the keyboard up to but not including the newline character. 
The instruction 'fullname.find(" ")' makes a request of the fullname string object to search itself for a space character, and to return the sequence number of the first one it finds. The location of the space is stored in the integer variable nsep. 
The instruction fullname.substr(0,nsep) requests of fullname to take a copy of the first nsep characters: thus if nsep was 4, then substr(0,nsep) would consist of the first four characters of the string. Character positions are indexed from 0. The instruction 'fullname.substr(nsep+1)' also requests a sub string from fullname, this time starting at sequence 'nsep+1', i.e. one after the space, and continuing until the end of the string. 

3.4 Exercises

a. Write a program (ftoc.cpp) that reads in a temperature in Fahrenheit and converts it to a whole number of Celsius (the conversion formula is C = (F-32)*5/9), as in:
ftoc
Enter Fahrenheit value : 70
70 Fahrenheit is 21 Celsius.
b. Write a program (hours.cpp) to convert a fractional number of hours into hours and minutes, as in: 
hours
Enter a fractional number of hours : 2.5
2.5 hours is 2 hours and 30 minutes.
c. The string method function 'rfind()' searches a string in the reverse direction to find(). Write a program (lastword.cpp) that reads in a sentence and reports the last word, as in:  lastword 
Enter a sentence (no full stop) : The cat sat on the mat 
The last word was: 'mat'
 

© 1999 Mark Huckvale University College London