Sams Teach Yourself C++ in 21 Days

(singke) #1
15: cout << “Long: “;
16: cin >> myLong;
17: cout << “Double: “;
18: cin >> myDouble;
19: cout << “Float: “;
20: cin >> myFloat;
21: cout << “Unsigned: “;
22: cin >> myUnsigned;
23:
24: cout << “\n\nInt:\t” << myInt << endl;
25: cout << “Long:\t” << myLong << endl;
26: cout << “Double:\t” << myDouble << endl;
27: cout << “Float:\t” << myFloat << endl;
28: cout << “Unsigned:\t” << myUnsigned << endl;
29: return 0;
30: }

int: 2
Long: 70000
Double: 987654321
Float: 3.33
Unsigned: 25

Int: 2
Long: 70000
Double: 9.87654e+008
Float: 3.33
Unsigned: 25
On lines 7–11, variables of various types are declared. On lines 13–22, the user
is prompted to enter values for these variables, and the results are printed (using
cout) on lines 24–28.
The output reflects that the variables were put into the right “kinds” of variables, and the
program works as you might expect.

Inputting Strings ............................................................................................


cincan also handle character pointer (char*) arguments; thus, you can create a character
buffer and use cinto fill it. For example, you can write the following:
char YourName[50]
cout << “Enter your name: “;
cin >> YourName;
If you enter Jesse, the variable YourNameis filled with the characters J, e, s, s, e, \0. The
last character is a null; cinautomatically ends the string with a null character, and you

OUTPUT


600 Day 17


LISTING17.1 continued

ANALYSIS
Free download pdf