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
From the earlier tutorial you learned the <iostream.h> header file needed for "cout" and "cin" statements. There are a lot more header files that are in the C++ library that have been created to make programming easier. Some include:
#include <iomanip.h>
When setting the decimal places. Use the term setios::flags()
______________________________________________________________________
#include <math.h>
Raising powers is done by using the "pow" function:
Pow (base, power)
Square root:
Sqrt (number)
Absolute value:
Abs (number)
______________________________________________________________________
#include <fstream.h>
Allows a text file to be created with output from the program, like making a receipt for the program after it has been run.
______________________________________________________________________
#include <conio.h>
char tap = getch();
This is similar to "cin" except that it is only used to get 1 letter from the user. Instead of typing in words or numbers and then pressing enter, this function will only wait for a key to be pressed on the keyboard. The value of the key pressed will be returned into the variable char. This is useful for asking the user if he wants to continue.
EX:
printf ("Press any key to continue...");
char tap = getch();
TIP: This function will only work when using "printf" and not "cout". See the next section on how to use the "printf" function.
The "printf" function is very similar to "cout" except that it is a standard function. The basic setup is shown below.
printf ("text %type...", variable_list);
For more information on functions see Tutorial 3.1
______________________________________________________________________
The output text and types of variables are placed within the quotes"".
EX 2.1
printf ("The value is %i.", num1);
The value of "num1" will be displayed where the "%i" is. The "%" is a placeholder that tells the function where to put the variable within the text. The "i" stands for integer. The following table shows the different types of variable output.
After the text within the quotes, there must be a comma (,) delimited list of all variables to be displayed in the text.
EX 2.2
printf ("The value is %i.", num1);
Here is an example using multiple variables:
EX 2.3
printf ("Today is: %i-%i-%i", iMonth, iDay, iYear);
SAMPLE OUTPUT:
Today is: 10-22-2014
You can set the float precision as shown below:
EX 2.3
printf ("Pi = %.3f:", 3.1415926);
SAMPLE OUTPUT:
Pi is: 3.141
3. Output ACTIONS
There are many actions that can be performed in the "cout" and "printf" functions within the "quotes". They are a single letter with a "\" (backslash) in front of them:
\n New Line (Carriage Return)
\r New Line (Carriage Return)
\t Tab
\a Alert Sound
\" Double Quote
\\ Backslash
EX: 3.1
cout << "\nEnter value: "; //get value from user
Notice that there is no space needed to separate the action from the text that will be displayed. The "\n" will not be displayed in the output; instead a carriage return will be displayed.
_______________________________________________________________________
EX: 3.2
cout << "Amount Paid:\t\t $"<<iAmount<<endl; //display value for user
This line will put two tabs between the text and the variable.
What will the following program do?
______________________________________________________________________
#include <iostream.h>
#include <conio.h>
void main()
{
int day, month, year;
char tap;
printf ("Enter today’s date (MM DD YYYY):";
cin >> month >> day >> year;
printf ("Thank you, the date you entered was: %i-%i-%i", month, day, year);
printf ("\nPress any key to continue...");
tap = getch();
}//end main
______________________________________________________________________
ANSWER:
Enter today’s date: [8 18 2002]
Thank you, the date you entered was: 8-18-2002
Press any key to continue...[a]
User input is represented with brackets [ ]
If you were able to understand that, try making your own program.