| INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS |
Each strike or multiple strikes on the keyboard represents a character: a letter, a digit, or a symbol. However, some characters are not labeled on the keyboard and some of them are not printable. Each character is represented by a numeric code that was agreed upon and standardized by a committee and this is known as ASCII code. Characters are the building blocks of any data in a computer, whether they are integers, doubles, or strings. In fact, one of the first jobs of a system programmer is to convert a sequence of characters to its intended meaning. For example, 1, 2, and 3 (one, two, three) becomes 123 (one hundred twenty three). Similarly, a sequence of characters represents a word, several words form a sentence, and a combination of sentences generates a story.
White space is considered a space, a tab, a vertical tab, or a new line. When receiving input using cin (an object of istream, which is also known as console input), the leading white space to an input is ignored. In several situations, ignoring white space is useful, but there are cases where white space is part of the data, such as a street address, where we do not want to break the address into separate units. One solution is to replace the white space with punctuation such as a dot (.) as demonstrated by the program below. Additional characters to replace blank spaces can be cumbersome; you may argue that this is not a desirable solution and wish to know an alternative.
![Text Box: #include<iostream>
using namespace std;
int main(){
char streetaddress[50];
cout<<"ENTER STREET ADDRESS: ";
cin>>streetaddress;
cout<<"STREET ADDRESS IS: "<<streetaddress<<endl;
return 0;
}//MAIN](http://www.geocities.com/lonairl1/chap17_files/image001.gif)

![]()
An istream has four state variables: goodbit, eofbit, failbit, and badbit that internally would be set based on the operation performed to the stream. Each of these state variables has an access function: good( ), eof( ), fail( ), and bad( ) respectively that returns the value of the state variable. In the following program, failbit is set when the invalid input is entered. The member function clear( ) resets the state variables.


![]()
![]()
PRINTING ASCII VALUES
The character A is 65 and character B is 66; can you figure out what character Z is? If you cannot figure it out, the following program demonstrates the ASCII code. You may not have figured out why NULL has an ASCII value of 0 and zero is 48, or why lower case character a is 97 which is larger than upper case A (65). I myself could not figure it out, but one could reason that in the early stages of computing only upper case letters were used and lower case was introduced later. The difference between lower and upper case letters is 32 and magically 32 is the ASCII code for blank space. Between upper case and lower case letters there are six special characters including bracket and backslash.
Moreover, the ASCII value for Σ is 169 and ½ is 189. You might want to try to print the extended ASCII characters and their values on your computer. This program displays the characters for the ASCII values 65 to 90 and displays the ASCII values for the lower case alphabet a through z.


Any input data, whether it is from a keyboard or an input file, is a combination of characters. In order to extract each character, this member function of input stream istream is used in three ways. The member function get( ) with a character parameter stores the character being read in the parameter variable. Another version of get( ) has no parameter, its return value is the character being read. Finally, the third version of get has an array of characters as its parameter where a string can be read.
cin.get(c ); fin.get(c);
c=cin.get( ); c=fin.get( );
cin.get(str); fin.get(str);
Recall, fin is an object of the class ifstream; it stands for file in and is a nice user-convention as shown below:
ifstream fin (input.dat);
The following program demonstrates how a string is created by using a get( ) function which gets a character and passes it as a parameter. For example, parameter c passes the character back in get(c). The eof( ) function checks for the end-of-file.
![Text Box: #include<iostream>
#include<cstdlib >
#include<fstream>
using namespace std;
int main(){
char c;
const int MAXSIZE=5;
char str[MAXSIZE];
int n = 0;
do {cin.get(c);
if(cin.eof()){
cout<<"BYE"<<endl; break; }//IF
else{ str[n] = c; n++; }//ELSE
}while (n!=MAXSIZE);
str[n]='\0';
cout<<"STRING IS :"<<str;
return 0;
}//MAIN](http://www.geocities.com/lonairl1/chap17_files/image013.gif)

In the following: cin.get(c), the function reads a character to variable c and returns a nonzero value. However, at the end-of-file it returns a zero that is used as the loop terminator.
![Text Box: #include<cstdlib >
#include<fstream>
using namespace std;
int main(){
char c;
const int MAXSIZE=5;
char str[MAXSIZE];
int n = 0;
while(cin.get(c)){
if(n==MAXSIZE){str[n]='\0';break;}//IF
str[n] = c; n++;}//WHILE
cout<<"STRING IS :"<<str<<endl;
retrun 0;
}//MAIN](http://www.geocities.com/lonairl1/chap17_files/image017.gif)

![]()
![]()
Another version of the get( ) function does not carry any parameter but its return value is the character and also the end-of-file indicator. Since the end-of-file is 1, the return value is of type integer and not type character. This version is similar to the C version and can be used in situations when we want to count the number of characters or number of lines.


![]()
Recall, the use of cin>>c without setting cin>>noskipws causes the white space before an input to be skipped. In contrast, the get( ) function reads the white space and to skip the white space, you must exclude the white space whenever you do not need it.


![]()
![]()

![]()

COUNT THE NUMBER OF CHARACTERS: EXCLUDE THE WHITE SPACE
The following program counts the number of characters without counting the white space character (blank, new line, tab and vertical tab).


The function get( ) can access a portion of a line or an entire line of input accordingly and store it into a string variable. The general syntax for the get( ) function is get(stringvariable, maxsize, delimiter). The function get(str,5,\n) will read up to 5 characters of input or read up to the new line character, whichever comes first. In the following example: get(str,5,,); the delimiter is coma (,) instead of new line. It is important to observe that the get( ) function does not extract or store the new line or any delimiter. To solve this problem either use another get( ) function, ignore( ) function, or getline( ) function to handle the delimiter.
ignore() MEMBER FUNCTION
The ignore() function of input stream reads and discards up to a specified number of characters provided by its argument or stops and discards the delimiter, such as a new line. If a delimiter is not specified, the EOF is considered the delimiter by default.
cin.ignore(20,\n);
In the above example, the ignore( ) function reads and discards up to 20 characters or stops when \n is reached and discards the new line. If the data is less than 20 characters long, all its characters are read. Two common uses of ignore( ) are after the get( ) string and if only certain fields of the data stream need to be extracted from the rest of the stream.
PORTION OR ENTIRE LINE: FUNCTION getline()
The function getline( ); can read a portion of a line or the entire line depending on the argument setting. The general form of the getline( ) function is: getline(stringvariable, maxsize, delimiter). The delimiter is a new line by default but others such as coma (,) can be used as well. Normally, maxsize is the size of the string but any other number less than maxsize can be used. The getline( ) function stops reading either when the maxsize characters is put into the array, including the null character, or upon encountering the new line, which ever comes first. It is important to note that getline( ) will extract the new line or delimiter and not save it (discards it).
The function getline( ) extracts the new line from the input stream and discards it while the function get( ) leaves the new line in the input stream and makes it the first character for the next input operation. It seems very confusing, but after some practice you will overcome this obstacle. Just remember, getline( ) gets the line and throws the delimiter out while the function get( ) stops with the delimiter.
![Text Box: #include<iostream>
using namespace std;
int main(){
char streetaddress[100];
cout<<"ENTER STREET ADDRESS: ";
cin.getline(streetaddress,100);
cout<<"STREET ADDRESS IS: "<<streetaddress<<endl;
return 0;
}//MAIN](http://www.geocities.com/lonairl1/chap17_files/image036.gif)
