forked from jeremyhansen/rooksguide-cplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chap_input.tex
145 lines (117 loc) · 5.03 KB
/
chap_input.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
% This work by Jeremy A. Hansen is licensed under a Creative Commons
% Attribution-NonCommercial-ShareAlike 3.0 Unported License,
% as described at http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
When a programmer wants a user to enter data, such as the price of an item, he or she will use the \Code{cin} object, pronounced ``see-in'', in conjunction with \Code{>>}, the \Keyword{extraction operator} in the program.
Let us look at the following code:
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cout << "Please enter a value for x: " << endl;
cin >> x;
return 0;
}
\end{lstlisting}\end{minipage}
When you compile and run this code, here's what the output will look like:
\noindent \Code{Please enter a value for x: }
As a user you may want to check the value that was entered.
To do this, simply add an additional \Code{cout} statement like this: \nopagebreak[4]
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
#include <iostream>
using namespace std;
int main()
{
int x = 0;
cout << "Please enter a value for x: " << endl;
cin >> x;
cout << "The value of x is: " << x;
return 0;
}
\end{lstlisting}\end{minipage}
\noindent The output of this code is:
\noindent \Code{Please enter a value for x: }
\noindent Suppose the user enters a value of 1 for x. The output that follows is:
\noindent \Code{The value of x is: 1}
As you can see, the value displayed is the one entered.
This can be a very useful technique in troubleshooting the values of variables throughout a program.
Do not be afraid to insert additional \Code{cout} statements throughout a program to check the values of variables when debugging.
This can help in the debugging process and speed up catching errors.
If you want to have a user input more than one value, just repeat the code for each individual variable:
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
#include <iostream>
using namespace std;
int main()
{
int x = 0;
int y = 0;
cout << "Please enter a value for x: " << endl;
cin >> x;
cout << "Please enter a value for y: " << endl;
cin >> y;
cout << "The value of x is: " << x << endl;
cout << "The value of y is: " << y << endl;
return 0;
}
\end{lstlisting}\end{minipage}
We can't always trust that the user will input the correct data into a variable.
For example, if a user was prompted to input an age into a variable of type \Code{int} but typed the character \Code{z}, the program would not behave properly because the user entered the wrong data type.
We can check for improper input like this by using the \Code{cin.fail()} function in a conditional statement.
Look at the following code:
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
#include <iostream>
using namespace std;
int main()
{
int x = 0;
int y = 0;
cout << "Please enter a value for x: " << endl;
cin >> x;
if (cin.fail())
{
cout << "That is not a valid data type!";
}
}
\end{lstlisting}\end{minipage}
This introduction to \Code{cin} statements is only the beginning.
They will get slightly more complicated after we introduce \Code{string}s, arrays, and overloaded operators.
\LevelD{Review Questions}
\begin{enumerate}
\item Which of the following numbered lines of code are proper \Code{cin} statements?
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
#include <iostream>
using namespace std;
int main()
{
int x = 0;
int y = 0;
cout << "Please enter a value for x: " ;
cin << x; // #1
cin >> x; // #2
cin >> x // #3
cin x; // #4
cin >< x; // #5
x >> cin; // #6
return 0;
}
\end{lstlisting}\end{minipage}
\item Must you always use \Code{cin} with \Code{cout}? Why or why not?
\item What is the redirect operator, and how is it used to process user input?
\item Can you use \Code{cin} to store a value in a variable that already has a value?
\item Write code that allows the user to enter an integer value and store it in a variable. Your code should prompt the user, receive their input, and then print their input value back to them.
\item Add some functionality to the code you wrote for the previous exercise. Add two new variables, one \Code{char} and one \Code{float} or \Code{double}. Prompt the user properly for each value. The program should print out the values of the variables, clearly labeled, on separate lines.
\end{enumerate}
\LevelD{Review Answers}
\begin{enumerate}
\item Only \#2 (\Code{cin >> x;}) is correct.
\item You do not need to to use \Code{cin} statements exclusively with \Code{cout} statements, though it is good practice to provide adequate feedback to users.
\item The redirect operator is \Code{>>}, and it is used in conjunction with \Code{cin} on the left and a variable on the right that receives the value entered by the user.
\item Yes, and the previous value is overwritten.
\end{enumerate}
\LevelD{Further Reading}
\begin{itemize}
\item \url{http://www.cplusplus.com/reference/iolibrary}
\item \url{http://www.cplusplus.com/doc/tutorial/basic_io}
%\item ~
\end{itemize}