Hi please help 6.3 Character I/O
Polonius: What do you read, my lord? Hamlet: Words, words, words.
All data is input and output as character data. When your program outputs the number 10, it is really the two characters '1' and '0' that are output. Similarly, when the user wants to type in the number 10, he or she types in the character '1' followed by the character '0'. Whether the computer interprets this 10 as two characters or as the number 10 depends on how your program is written. But, however your program is written, the computer hardware is always reading the characters '1' and '0', not the number 10. This conversion between characters and numbers is usually done automatically so that you need not think about such detail. Sometimes, however, all this automatic help gets in the way. Therefore, C++ provides some low-level facilities for input and output of character data. These low-level facilities include no automatic conversions. This allows you to bypass the automatic facilities and do input/output in absolutely any way you want. You could even write input and output functions that read and write numbers in Roman numeral notation, if you wanted to be so perverse.
The Member Functions get and put
The function get allows your program to read in one character of input and store it in a variable of type char. Every input stream, whether it is an input-file stream or the stream cin, has get as a member
function. We will describe get as a member function of the stream cin, but it behaves in exactly the same way for input-file streams as it does for cin, so you can apply all that we say about get to input-file streams as well as to the stream cin.
Before now, we have used cin with the extraction operator >> in order to read a character of input (or any other input, for that matter). When you use the extraction operator >>, as we have been doing, some things are done for you automatically, such as skipping blanks. With the member function get, nothing is done automatically. If you want, for example, to skip over blanks using cin.get, you must write code to read and discard the blanks.
The member function get takes one argument, which should be a variable of type char. That argument receives the input character that is read from the input stream. For example, the following reads in the next input character from the keyboard and stores it in the variable nextSymbol:
It is important to note that your program can read any character in this way. If the next input character is a blank, this code will not skip over the blank, but will read the blank and set the value of nextSymbol equal to the blank character. If the next character is the new-line character '\n', that is, if the program has just reached the end of an input line, then the call to cin.get shown earlier sets the value of
nextSymbol equal to '\n'. Reading blanks and '\n'
WILLIAM SHAKESPEARE, Hamlet
   char nextSymbol; cin.get(nextSymbol);
     
 Although we write it as two symbols, '\n' is just a single character in C++. With the member function get, the character '\n' can be input and output just like any other character. For example, suppose
your program contains the following code:
and suppose you type in the following two lines of input to be read by this code:
That is, suppose you type AB followed by Return and then CD followed by Return. As you would expect, the value of c1 is set to 'A' and the value of c2 is set to 'B'. That’s nothing new. But when this code fills the variable c3, things are different from what they would be if you had used the extraction operator
>> instead of the member function get. When this code is executed on the input we showed, the value of c3 is set to '\n'; that is, the value of c3 is set equal to the new-line character. The variable c3 is not
set equal to 'C'.
One thing you can do with the member function get is to have your program detect the end of a line. The following loop will read a line of input and stop after passing the new-line character '\n'. Then, any subsequent input will be read from the beginning of the next line. For this first example, we have simply echoed the input, but the same technique would allow you to do whatever you want with the input:
 char c1, c2, c3; cin.get(c1); cin.get(c2); cin.get(c3);
 AB CD
  Detecting the end of an input line
 cout << "Enter a line of input and I will echo it:\n"; char symbol;
do
{
    cin.get(symbol);
    cout << symbol;
} while (symbol != '\n');
cout << "That's all for this demonstration.";
This loop will read any line of input and echo it exactly, including blanks. The following is a sample dialogue produced by this code:
Notice that the new-line character '\n' is both read and output. Since '\n' is output, the string that begins with the word "That's" is on a new line.
 Enter a line of input and I will echo it:
Do Be Do 1 2  34 Do Be Do 1 2  34
That's all for this demonstration.
 
  The Member Function get
Every input stream has a member function named get that can be used to read one character of
input. Unlike the extraction operator >>, get reads the next input character, no matter what that character is. In particular, get reads a blank or the new-line character '\n' if either of these is the next input character. The function get takes one argument, which should be a variable of type char. When get is called, the next input character is read and the argument variable (called
Char_Variable below) has its value set equal to this input character. Syntax
inputStream.get(Char_Variable); Example
If you wish to use get to read from a file, you use an input-file stream in place of the stream cin. For example, if inStream is an input stream for a file, then the following reads one character from the input file and places the character in the char variable nextSymbol:
inStream.get(nextSymbol);
Before you can use get with an input-file stream such as inStream, your program must first connect the stream to the input file with a call to open.
'\n' and "\n"
'\n' and "\n" sometimes seem like the same thing. In a cout statement, they produce the same effect, but they cannot be used interchangeably in all situations. '\n' is a value of type char and can be stored in a variable of type char. On the other hand, "\n" is a string that happens to be made up of exactly one character. Thus, "\n" is not of type char and cannot be stored in a variable of type char.
The member function put is analogous to the member function get except that it is used for output rather than input. put allows your program to output one character. The member function put takes one argument, which should be an expression of type char, such as a constant or a variable of type char. The value of the argument is output to the stream when the function is called. For example, the following outputs the letter 'a' to the screen:
cout.put('a');
The function cout.put does not allow you to do anything you could not do by using the methods we
discussed previously, but we include it for completeness.
If your program uses cin.get or cout.put, then just as with other uses of cin and cout, your program should include the following directive:
   char nextSymbol; cin.get(nextSymbol);
           #include <iostream>

 Similarly, if your program uses get for an input-file stream or put for an output-file stream, then just as with any other file I/O, your program should contain the following directive:
  #include <fstream>
The Member Function put
Every output stream has a member function named put, which takes one argument which should be
an expression of type char. When the member function put is called, the value of its argument (called Char_Expression below) is output to the output stream.
Syntax
outputStream.put(Char_Expression); Examples
If you wish to use put to output to a file, you use an output-file stream in place of the stream cout. For example, if outStream is an output stream for a file, then the following will output the character
'Z' to the file connected to outStream: outStream.put('Z');
Before you can use put with an output-file stream, such as outStream, your program must first connect the stream to the output file with a call to the member function open.
When using either of these include directives, your program must also include the following: using namespace std;
The putback Member Function (Optional)
Sometimes your program needs to know the next character in the input stream. However, after reading the next character, it might turn out that you do not want to process that character and so you would like to simply put it back in the input stream. For example, if you want your program to read up to but not including the first blank it encounters in an input stream, then your program must read that first blank in order to know when to stop reading—but then that blank is no longer in the stream. Some other part of your program might need to read and process this blank. There are a number of ways to deal with this sort of situation, but the easiest is to use the member function putback. The function putback is a member of every input stream. It takes one argument of type char and it places the value of that argument back in the input stream. The argument can be any expression that evaluates to a value of type char.
For example, the following code will read characters from the file connected to the input stream fin and write them to the file connected to the output stream fout. The code reads characters up to, but not including, the first blank it encounters.
     cout.put(nextSymbol); cout.put('a');
         
  Notice that after this code is executed, the blank that was read is still in the input stream fin, because the code puts it back after reading it.
Notice that putback places a character in an input stream, while put places a character in an output stream. The character that is put back into the input stream with the member function putback need not be the last character read; it can be any character you wish. If you put back a character other than the last character read, the text in the input file will not be changed by putback, although your program will behave as if the text in the input file had been changed.
Programming Example Checking Input
If a user enters incorrect input, the entire run of the program can become worthless. To ensure that your program is not hampered by incorrect input, you should use input functions that allow the user to reenter input until the input is correct. The function getInt in Display 6.7   asks the user whether the input is correct and asks for a new value if the user says the input is incorrect. The program in Display 6.7   is just a driver program to test the function getInt, but the function, or one very similar to it, can be used in just about any kind of program that takes its input from the keyboard.
Display 6.7 Checking Input
      fin.get(next); while (next != ' ')
{
    fout.put(next);
    fin.get(next);
}
fin.putback(next);

 Sample Dialogue
 1 //Program to demonstrate the functions newLine and getInput.
 2    #include <iostream>
3 using namespace std;
4
5 void newLine( );
6 //Discards all the input remaining on the current input line.
7 //Also discards the '\n' at the end of the line.
8 //This version works only for input from the keyboard.
9
10 void getInt(int& number);
11 //Postcondition: The variable number has been
12 //given a value that the user approves of.
13
14
15 int main( ) 16 {
17 int n; 18
19        getInt(n);
20 cout << "Final value read in = " << n << endl
21 << "End
22 return 0;
23 } 24
of demonstration.\n";
25
26 //Uses iostream:
27 void newLine( )
28 {
29 char symbol;
30 do
31 { 32
33
34 }
35 //Uses iostream:
36 void getInt(int&
37 {
38 char ans;
39 do
40 {
41 cout << "Enter input number: ";
42            cin >> number;
43 cout << "You entered " << number
44 << ". Is that correct? (yes/no): ";
45 cin >> ans;
46            newLine( );
47 } while ((ans != 'Y') && (ans != 'y'));
48 }
  cin.get(symbol);
 } while (symbol != '\n');
number)

    Notice the call to the function newLine( ). The function newLine reads all the characters on the remainder of the current line but does nothing with them. This amounts to discarding the remainder of the line. Thus, if the user types in No, then the program reads the first letter, which is N, and then calls the function newLine, which discards the rest of the input line. This means that if the user types 75 on the next input line, as shown in the sample dialogue, the program will read the number 75 and will not attempt to read the letter o in the word No. If the program did not include a call to the function
newLine, then the next item read would be the o in the line containing No instead of the number 75 on the following line.
Notice the Boolean expression that ends the do-while loop in the function getInt. If the input is not correct, the user is supposed to type No (or some variant such as no), which will cause one more iteration of the loop. However, rather than checking to see if the user types a word that starts with
'N', the do-while loop checks to see if the first letter of the user’s response is not equal to 'Y' (and not equal to the lowercase version of 'Y'). As long as the user makes no mistakes and responds with some form of Yes or No, but never with anything else, then checking for No or checking for not being Yes are the same thing. However, since the user might respond in some other way, checking for not being Yes is safer. To see why this is safer, suppose the user makes a mistake in entering the input number. The computer echoes the number and asks if it is correct. The user should type in No, but suppose the user makes a mistake and types in Bo, which is not unlikely since
'B' is right next to 'N' on the keyboard. Since 'B' is not equal to 'Y', the body of the do-while loop will be executed, and the user will be given a chance to reenter the input.
But, what happens if the correct response is Yes and the user mistakenly enters something that begins with a letter other than 'Y' or 'y'? In that case, the loop should not iterate, but it does iterate one extra time. This is a mistake, but not nearly as bad a mistake as the one discussed in the last paragraph. It means the user must type in the input number one extra time, but it does not waste the entire run of the program. When checking input, it is better to risk an extra loop iteration than to risk proceeding with incorrect input.
Pitfall Unexpected ‘\n’ in Input
When using the member function get, you must account for every character of input, even the
characters you do not think of as being symbols, such as blanks and the new-line character '\n'. A common problem when using get is forgetting to dispose of the '\n' that ends every input line. If there is a new-line character in the input stream that is not read (and usually discarded), then when your program next expects to read a “real” symbol using the member function get, it will instead read the character '\n'. To clear the input stream of any leftover '\n' characters, you can use the function newLine, which we defined in Display 6.7   . Let’s look at a concrete example.
It is legal to mix the different forms of cin. For example, the following is legal:
        When in doubt, enter the input again
  Enter input number: 57
You entered 57. Is that correct? (yes/no): No.             Using this information in chapter 6 above please do this exercise below.                                         For  your assignment, you are going to do Programming Project #6 at the end of Chapter 2 in the textbook - do not do the harder version.

An employee is paid at a rate of $16.78 per hour for the first 40 hours worked in a week. Any hours over that are paid at the overtime rate of one-and-one-half times that. From the worker’s gross pay, 6% is withheld for Social Security tax, 14% is withheld for federal income tax, 5% is withheld for state income tax, and $10 per week is withheld for union dues. If the worker has three or more dependents, then an additional $35 is withheld to cover the extra cost of health insurance beyond what the employer pays. Write a program that will read in the number of hours worked in a week and the number of dependents as input and will then output the worker’s gross pay, each withholding amount, and the net take-home pay for the week.

 

Submit the code and a screenshot of the output.