cs31 lecture 17

2 stdcout foo is foo and bar is bar stdendl stdcout

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: d” << std::endl; // equivalent to std::cout << “Hello World” << std::endl; 31 Printing out variables x << can support all basic types as rhs float, double, int, char, … Also with qualifier x The user doesn’t need to specify the type of the rhs, it is The inferred by the compiler and the good << operator is called inferred x Examples: int foo = 2; float bar = 3.2; std::cout << “foo is: “ << foo << “ and bar is: “ << bar << std::endl; std::cout 32 Some observations x Variables and more generally expressions can be printed Variables out out x For floating point values, not all digits are printed out There are commands to set the precision It doesn’t mean the number has been truncated x For complex, user-defined types, must define the operator For << << 33 Console input x Useful to get some input from the user, through the Useful keyboard (usually)
 keyboard x Principle: “push” the standard input into a variable x Example: int foo; std::cin >> foo; float bar; float std::cin >> bar; std::cin 34 Some observations x “Conversion” happens when the input does not match the Conversion” type type Example: int foo; std::cin >> foo; std::cout << foo << std::endl; Giving 123.45 as input on the keyboard leads to printing 123 x If the input is “meaningless”, then no assignment is If performed performed Example: input “foo” to the above program le...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online