Each one of the characters you see on your keyboard is
represented as a numeric value, but whether it appears as a number, a
letter, or a symbol, each one of these is considered a character. To display
any character on your screen, you can use putchar() (which
stands for pur-character). In the parentheses of putchar(),
type the letter or symbol in single-quotess. Here is an example:
#include <iostream>
using namespace std;
void main()
{
putchar('A');
}
This would produce the lette A. In reality, and as
mentioned already, each character is represented by an ASCII numeric byte
value. If you know that number, you can write between the parentheses of
putchar(). Here is an example:
#include <iostream>
using namespace std;
void main()
{
putchar(65);
}
In the English alphabet, a letter is one of the
following symbols: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s,
t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R,
S, T, U, V, W, X, Y, and Z. Besides these readable characters, the following
symbols are called digits and they are used to represent numbers: 0, 1, 2,
3, 4, 5, 6, 7, 8, 9. In addition, some symbols on the (US QWERTY) keyboard
are also called characters or symbols. They are ` ~ ! @ # $ % ^ & * ( ) - _
= + [ { ] } \ | ; : ' < ? . / , > ".
To declare a variable for a letter or a symbol, you have
many options. You can use the char keyword from the C
language. To specify the value of the variable, type it in single-quotes.
Here is an example:
#include <iostream>
using namespace std;
void main()
{
char letter = 'a';
}
To display its value, write the name of the variable in
the parenteses of putchar(). Here is an example:
#include <iostream>
using namespace std;
void main()
{
char letter = 'a';
putchar(letter);
}
To request a character from the user, you can use
cin >> followed by the name of the variable that will hold
the character. As an alternative, you can use _getch() and
assign it to the variable of the character.
To provide its own representation of characters that can
fit in 8 bits, the Win32 library has the CHAR data type.
You can use it to declare a variable and initialize by including its
character in single-quotes. Here is an example:
#include <iostream> #include <Windows.h> using namespace std; void main() { CHAR symbol = '$'; putchar(symbol); }
If the numeric value of a character is positive, such a
character is referred to as unsigned. To declare a variable that can hold
unsigned characters, use the unsigned char data type. Here
is an example:
#include <iostream>
using namespace std;
void main()
{
unsigned char letter = 'q';
putchar(letter);
}
To support unsigned unsigned characters, the Win32
library provides the UCHAR data type. You can use it in the
exact same you would use C's unsigned char.
At one time, the 7 bits set aside by ASCII were not
enough to accommodate some characters and symbols, especially in non-Latin
languages. The solution was to double the size, which was to use two bytes.
This provided support for Unicode characters and symbols that are referred
to as wide characters.
To declare a variable that can hold English and
non-English characters, use the wchar_t keyword. To give a
value to the variable, normally, you can just include it in single-quotes.
Here is an example:
#include <iostream>
using namespace std;
void main()
{
wchar_t symbol = '%';
putchar(symbol);
}
A better alternative to indicate that you are dealing
with a wide character is to precede its value with L. Here
is an example:
#include <iostream>
using namespace std;
void main()
{
wchar_t symbol = L'%';
putchar(symbol);
}
To request a wide character from the user, you can use
cin >> followed by the name of the variable. Alternatively,
you can use _getwch() and assign it to the variable that
must hold the character.
Besides wchar_t, you can declare a variable for a wide
character using the char16_t keyword as data type. As the
16 number in its name indicates, this data type supports wide characters.
You can use it the same way you would a wchar_t data type.
Here is an example:
#include <iostream>
using namespace std;
void main()
{
char16_t character = 'e';
putchar(character);
}
To support Unicode, the Win32 library provides the
TCHAR and the WCHAR data types. You can
use them in the same way you would use wchar_t. Here is an
example:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
TCHAR symbol = L'&';
putchar(symbol);
}
An escape sequence is a special character that displays
non-visibly. For example, you can use this type of character to indicate the
end of line, that is, to ask the program to continue on the next line. An
escape sequence is represented by a backslash character, \, followed by
another character or symbol. For example, the escape sequence that moves to
the next line is \n.
An escape can be included in single-quotes as in '\n'.
It can also be provided in double-quotes as "\n". The C language recognizes
the following escape sequences:
To use an escape sequence, you can also first declare a
character variable and initialize it with the desired escape sequence in
single-quotes.
As mentioned already, a byte is used to handle a storage
area of 8 bits. By default, the storage is used to hold a small numeric
value. To support it, the Win32 library provides the BYTE
data type. When you declare a variable of type BYTE, you can specify its
value using a single-quoted character or a (small) number. Here is an
example:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
BYTE byte = 65;
putchar(byte);
}
When you initialize a BYTE variable
using a number, if you use putchar() to display its value,
the compiler would convert that number to its equivalent letter or symbol.
To support wide characters and symbols, the Win32 library provides the
TBYTE data type. You use it the same way you would use
unsigned char. Here is an example:
#include
#include
using namespace std;
void main()
{
TBYTE byte = 128;
putchar(byte);
}
This would produce the Ç character.
Because a byte primarily holds a numeric value, you can
initialize it with a hexadecimal number. Here is an example:
#include <iostream> #include <Windows.h> using namespace std; void main() { TBYTE byte = 0xEE; putchar(byte); }
This would produce the Euro symbol.
A group of characters is called an array. This is a
study we will cover when learning about arrays. For now, if you want to use
a group of characters of any kind, you can declare a variable using either
the char or the CHAR data type, followed
by a valid name for the variable, followed by an opening square bracket [,
followed by a number of characters, followed by a closing square bracket ],
and ended with a semi-colon. The syntax to declare a group of characters is:
char VariableName[NumberOfCharacters]; CHAR VariableName[NumberOfCharacters];
The char keyword or the CHAR
data type is required to let the compiler know that you want to create a
variable that uses characters. The variable must have a valid name. The
number of characters can be an estimate. For example, if you want to request
employees' first names, type a number that you think would represent the
largest name possible. The maximum number should be 80, but the average and
a good regular number should be between 12 or 20. To represent an address or
a similar long group of characters, use a dimension between 32 and 50. Here
is an example:
char firstName[20];
To initialize an array of characters, do not specify the
dimension and leave the square brackets empty. You can use the assignment
operator and include the initialized value in double-quotes. Here is an
example:
#include <iostream>
#include <Windows.h>
using namespace std;
void main()
{
char university[] = "University of the District of Columbia";
CHAR city[] = "San Pedro";
}
As another technique, leave the square brackets empty,
then add an opening and a closing parentheses. Inside the parentheses, enter
the string in double-quotes. Here is a program:
#include <iostream>
using namespace std;
void main()
{
char University[] = "University of the District of Columbia";
char Faculty[]("Computer sciences");
}
To display the value of a string variable to the console
using the C++ language, you can type it on the right side of cout <<. Here
are examples:
#include <iostream> using namespace std; void main() { char University[] = "University of the District of Columbia"; char Faculty[]("Computer sciences"); cout << "Welcome to the Student Orientation Program.\n"; cout << "For your studies, we have selected:\n"; cout << "Institution: "; cout << University; cout << "\n"; cout << "Faculty: "; cout << Faculty; cout << "\n"; }
As an alternative using C, if you are using the
char keyword or the CHAR data type, you can use
printf_s().
To request an array of characters from the user, you
have many options. In the C language, first declare the variable, then can
use gets() or gets_s(). In the
parentheses, enter the name of the variable. In C++, you can type the name
of the array variable on the right side of cin >>. Here are
examples:
#include <iostream> using namespace std; void main() { char FirstName[12]; char LastName[12]; cout << "Enter First Name: "; cin >> FirstName; cout << "Enter Last Name: "; cin >> LastName; cout << "\nFull Name: " << FirstName << " " << LastName; cout << endl; }
Alternatively, you can use the following formulas:
cin.getline(VariableName, Dimension); cin.getline(VariableName, Dimension, Delimeter);
To use the array variables, change the content of the
file as follows:
#include <iostream> using namespace std; void main() { char FirstName [20], LastName [20]; char Address [40]; char JobPerformed [80]; cout << "Welcome to College Park Auto-Parts\n"; cout << "Enter the following information about the customer's.\n"; cout << "First Name: "; cin >> ws; cin.getline(FirstName, 20); cout << "Last Name: "; cin >> ws; cin.getline(LastName, 20); cout << "Address: "; cin >> ws; cin.getline(Address, 40); cout << "Describe the job performed on the customer's car in 100 words or less:\n"; cin >> ws; cin.getline(JobPerformed, 80); cout << "\nCPAP Invoice # 1202"; cout << "\nCustomer Name: " << FirstName << " " << LastName; cout << "\nAddress: " << Address; cout << "\nJob Performed: " << JobPerformed; cout << "\n\n"; }
A regular string uses characters and symbols that need 8
bits each. In Unicode, a string may use international characters outside of
the English language. To declare a string variable as an array of characters
and that can hold all types of symbols, you can use the wchar_t
data type:
wchar_t VariableName[NumberOfCharacters];
As you can see, the formula is the same as done for the
char data type. To specify the value of the variable, you
have many options. If you want to initialize the variable when declaring it,
leave the parentheses empty and assign a string to it. This time, the string
can start with L. Here is an example:
#include <iostream>
using namespace std;
void main()
{
wchar_t city[] = L"Rio de Janeiro";
}
As another solution, if you are using Win32 in your
code, precede the string with TEXT and include the string
in parentheses. Here are examples:
#include <iostream> #include <Windows.h> using namespace std; void main() { wchar_t city[] = L"Rio de Janeiro"; wchar_t country[] = TEXT("Emirats Arabes Unis"); wchar_t team[](TEXT("Football Club Internazionale Milano")); }
In an MFC application, you can start the string with
_T the same way you would use TEXT. Here
is an example:
#include <iostream> #include <AfxWin.h> using namespace std; void main() { wchar_t city[] = L"Rio de Janeiro"; wchar_t country[] = TEXT("Emirats Arabes Unis"); wchar_t team[](TEXT("Football Club Internazionale Milano")); wchar_t player[] = _T("Samuel Eto'o Fils"); }
To display the value of a variable that holds a wide
string, you can use _putws(), wprintf, or wprintf_s().
In all cases, type the name of the variable in the parentheses. Here are
examples:
#include <iostream> #include <AfxWin.h> using namespace std; void main() { wchar_t city[] = L"Rio de Janeiro"; wchar_t country[] = TEXT("Emirats Arabes Unis"); wchar_t team[](TEXT("Football Club Internazionale Milano")); wchar_t player[] = _T("Samuel Eto'o Fils"); _putws(city); _putws(country); wprintf(team); wprintf_s(L"\n"); wprintf_s(player); wprintf_s(L"\n"); }
To request the value of a variable that can hold a wide
string, based on the C language, use _getws_s() or
_getws(). In the parentheses, enter the name of the variable.
A string is a character, a group of characters, or an
empty space that you want the compiler to treat "as is". Besides using an
array of characters, a special library called the Standard Template Library
(STL) provides an alternative. You can declare a string using the
string word (it is a class).
To use a string in your program, first include the
string library using the using namespace std; expression.
In your program, declare a variable starting with the word string
followed by a valid name for the variable. Here are examples:
string Continent; string Company;
When requesting its value from the user, by default,
string is used to get only a one-word value. Here is an
example program that requests a first and last names:
#include <iostream> #include <string> using namespace std; void main() { string FirstName, LastName; cout << "Enter first name: "; cin >> FirstName; cout << "Enter last name: "; cin >> LastName; cout << "\n\nFull Name: " << FirstName << " " << LastName << "\n\n"; }
You can initialize a string variable of any length. One
technique is to use the assignment operator and include the string in
double-quotes. Here is an example:
string UN = "United Nations"; cout << "The " << UN << " is an organization headed by a Secretary General";
Another technique involves using parentheses following
the name of the string variable, and including the string in double-quotes.
Here is an example:
string BookTitle("Drugs, Sociology, and Human Behavior."); cout << "For our class next week, please read \"" << BookTitle;cout << "\"";
If you want to request the value of the variable from
the user, you should use the getline function. To use the getline
function, follow this formula:
getline(cin, StringName);
Inside of the parentheses, the word cin informs the
compiler that the request will come from an external source, mainly the user
typing from the keyboard. The StringName is the name you declared the
variable with. The getline() function expects that the user will
press Enter to end the sentence; the end line character is "\n".
Here is an example program that requests strings of any
length from the user:
#include <iostream> #include <string> using namespace std; void main() { string MusicAlbum; string TrackTitle; cout << "Welcome to Radio Request where the listeners select their songs:\n"; cout << "Type the album name: "; getline(cin, MusicAlbum); cout << "Type the song title: "; getline(cin, TrackTitle); cout << "\nNow for your pleasure, we will play: " << TrackTitle << "\nfrom the " << MusicAlbum << " wonderful album.\n\n"; }
If you want the user to end the sentence with another
character such as * or !, use the following function
getline(cin, StringName, Delimiter);
The following example uses the = symbol as the end
character of the sentence:
string Address; cout << "Enter your address. To end, type = "; getline(cin, Address, '='); cout << "\nSo, you live at: " << Address;
Here is an example:
#include <iostream> #include <string> using namespace std; void main() { string FirstName, LastName; cout << "Welcome to College Park Auto-Parts\n"; cout << "Enter the following information about the customer's.\n"; cout << "First Name: "; cin >> FirstName; cout << "Last Name: "; cin >> LastName; cout << "\n\nCPAP Invoice # 1202"; cout << "\nCustomer Name: " << FirstName << " " << LastName << "\n\n"; }
When requesting a string made of various words, such as
an address, you can use the getline() function as follows:
#include <iostream> #include <string> using namespace std; void main() { string FirstName, LastName; string Address; string JobPerformed; cout << "Welcome to College Park Auto-Parts\n"; cout << "Enter the following information about the customer's.\n"; cout << "First Name: "; cin >> FirstName; cout << "Last Name: "; cin >> LastName; cout << "Address: "; getline(cin, Address); cout << "Describe the job performed on the customer's car:\n"; getline(cin, JobPerformed); cout << "\n\nCPAP Invoice # 1202"; cout << "\nCustomer Name: " << FirstName << " " << LastName; cout << "\nAddress: " << Address; cout << "\nJob Performed: " << JobPerformed << "\n\n"; } |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Introduction to Values of a Program
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment