C++ DOS console, Tutorial 1
By: Dennis Dunn
Original: 8-18-02
Updated: 12-28-10
This tutorial covers:
KEY Colors:
- Normal text
- Comments
- Key words
- Special cases that are being pointed out
- Special syntax that is being pointed out
- Sample output for a program
- Hyper links
– [ ] Represents input for a sample run of a program
Ex 1.1
//This is a comment, the compiler ignores this text. It is only for the
//programmer. Comments turn a green color.
/* START COMMENT
This makes everything
between the slash and
the asterisk a comment.
*/ END COMMENT (this text here will cause an error)
The program execution always begins in one place, the main() function. Place this function in the code and then place your code within it. This is what the program always first starts running.
EX: 2.1
//put header files here, before main
#include <iostream.h> //header file needed for "cout" and "cin"
void main()
{
//place code execution here
}//end main
main() can also have parameters passed into it that reflect the parameters typed from the console command prompt, but that will be explained later. For now use the simple void data-type to indicate there will be no return value
Data types:
_______________________________________________________________________
EX 3.1
long int num1;
This declaration statement creates a long integer named "num1". The semicolon (;) is needed at the end of every operation in C.
_______________________________________________________________________
EX 3.2
const double PI = 3.1415926;
This creates a constant decimal value named "PI". Constant variables are named in UPPERCASE as a convention. They must always be assigned a value in the declaration statement as shown above. Once the variable has been assigned it cannot be changed during the rest of the program.
_______________________________________________________________________
EX 3.3
char case, letter, c;
This creates three variables of the same type in one line; unfortunately they cannot be assigned different values at the same time.
When assigning values to a variable always remember that the variable being assigned a value is on the left.
EX 4.1
num1 = 3;
In this case "num1" is receiving a value of 3.
_______________________________________________________________________
Variables can also be assigned to each other.
EX 4.2
num2 = num1;
Now num2 is set equal to num1, so they both have a value of 3.
_______________________________________________________________________
Variables can be assigned while they are being created.
EX 4.3
long int num1 = 3;
This method will reduce the amount of code needed.
_______________________________________________________________________
Values can also be calculated using operators such as: division, multiplication, addition, and subtraction. (/, *, +, -)
EX 4.4
num1 = num1 - 2;
Now num1 has changed from 3 to 1.
_______________________________________________________________________
More examples:
num1 = num2 + 3;
num2 = 3 + 4;
num1 = 5 + (num2 * 2);
The parentheses are used to change order of operations. num2 is doubled before 5 is added. Another important thing to know is that the compiler does not treat parentheses as multiplication automatically.
EX 4.5
num1 = 3 (num2 + 1);
This would cause an error because there is no multiplication operator before the "(".
The correct way to type it:
EX 4.6
num1 = 3 * (num2 + 1);
_______________________________________________________________________
Another useful way to change a value is with the increment and decrement operations.
The "++" and "--" are used to add or subtract 1 from the variable.
Ex. 4.7
num1++; //adds 1 to "num1"
num2--; //subtracts 1 from "num2"
Note that placing the increment or decrement before the variable will modify the variable before it is invoked. For example, the value is incremented before it is used to assign itself to num4:
Ex 4.8
num3 = 3;
num4 = num3++;//num4 receives a value of 3, num3 = 4 after line completes
num3 = 3;
num4 = ++num3;//num4 receives a value of 4, num3 = 4
Most programs need to be able to display things. The functions "cout" or "printf" are often used. Chevrons pointing to the left (<<) must be placed after the word "cout" and in between each variable or phrase. Don’t forget to put a semicolon (;) at the end;
EX 5.1
cout << num1;
This will display the value of num1 on the screen for the user.
_______________________________________________________________________
To display text, "quotes" are needed.
EX 5.2
cout << "The value is: " << num1;
OUTPUT:
The value is: 3
_______________________________________________________________________
Using "endl" places a carriage return after the line.
EX 5.3
cout << "The value is: " << num1 << endl;
_______________________________________________________________________
To get a value from the user the "cin" command is used with chevrons pointing to the right (>>).
EX 5.4
cin >> num1;
This is how the user can enter values into the computer.
TIP: Make sure that the value entered is the same type as the variable. For example, don’t enter a letter "a" into an integer "num1", or this will cause the program to lock-up.
_______________________________________________________________________
To get multiple values from the user at the same time put ">>" in between each variable.
EX 5.5
cin >> num1 >> num2 >> num3;
This is how the user can enter multiple values into the computer at once, probably useful for entering a date or something (Day Month Year).
TIP: Before "cout" and "cin" can be used, the header file "<iostream.h>" must be placed at the top before the main function. Click here to see how.
What will the following program do?
#include <iostream.h>
void main()
{
int num1;
cout << "Enter an integer value: ";
cin >> num1;
cout << "Thank you, the number you entered was: " << num1 << endl;
}//end main
______________________________________________________________________
ANSWER:
Enter integer value: [4]
Thank you, the number you entered was: 4
User input is represented with brackets [ ]
If you were able to understand that, you are on track. If not, just keep practicing until it makes more sense.