Step Two  How Old Are You?

In order to keep track of things input or output, to aid in calculations, or to hand things off to different functions we need VARIABLES. In C++ there are several fundamental data types: int, float, char and bool. Variables of type int can store positives, negatives or zero but no decimals. Variables of type float can store decimals, chars store single characters like 'a' or '5' and variables of type bool can be either true or false.

The following are C++ statements which declare different types of variables.

// make an integer variable called 'count' with initial value -3
int count = -3;
// make a floating point variable called 'slope' with value 4.5
float slope = 4.5;
// a char variable called 'letter' with capital A stored in it
char letter = 'A';
// a boolean variable called 'flag' set to false
bool flag = false;


Sample program:

( 1)   // Program Name: age.cpp
( 2)   // Description: Tell a Person how old they are in days
( 3)   // Author: Calamity Sam
( 4)   // Date: September 10, 1752
( 5)
( 6)   #include <iostream>  //this is needed for C++ I/O
( 7)   #include <string>    //this will be needed for strings
( 8)   using namespace std;
( 9)
(10)   int main(void) {
(11)     //display something on a line by itself
(12)     cout << "Hello! How many years old are you? ";
(13)     int age = 0;
(14)     cin >> age;
(15)     cout << endl << "You are about " << age*365 << " days old." << endl;
(16)     return 0;
(17)   }

Go Back to Step One

Advance to Step Three

Return to the Table of Contents