5.3 Reading Data

I l @ ve RuBoard

So far you've learned how to compute expressions and output the results. You need to have your programs read numbers as well. The output class object std::cout uses the operator << to write numbers. The input object std::cin uses the operator >> to read them. For example:

 std::cin >> price >> number_on_hand; 

This code reads two numbers: price and number_on_hand . The input to this program should be two numbers, separated by whitespace. For example, if you type:

 32 5 

price gets the value 32 and number_on_hand gets 5 .

This does not give you very precise control over your input. C++ does a reasonable job for simple input. If your program expects a number and you type <enter> instead, the program will skip the <enter> (it's whitespace) and wait for you to type a number. Sometimes this may lead you to think your program's stuck.

In Example 5-3, we use std::cin to get a number from the user , then we double it.

Example 5-3. double/double.cpp
 #include <iostream> int   value;       // a value to double int main(  ) {     std::cout << "Enter a value: ";     std::cin >> value;     std::cout << "Twice " << value << " is " << value * 2 << '\n';     return (0); } 

Notice that there is no \n at the end of Enter a value: . This is because we do not want the computer to print a newline after the prompt. For example, a sample run of the program might look like this:

 Enter a value:  12  Twice 12 is 24 

If we replaced Enter a value: with Enter a value:\n the result would be:

 Enter a value:  12  Twice 12 is 24 

Question 5-1: Example 5-4 is designed to compute the area of a triangle, given its width and height. For some strange reason, the compiler refuses to believe that we declared the variable width . The declaration is right there on line two, just after the definition of height. Why isn't the compiler seeing it?

Example 5-4. comment/comment.cpp
 #include <iostream> int  height;   /* the height of the triangle int  width;    /* the width of the triangle */ int  area;     /* area of the triangle (computed) */ int main(  ) {     std::cout << "Enter width height? ";     std::cin >> width >> height;     area = (width * height) / 2;     std::cout << "The area is " << area << '\n';     return (0); } 

The general form of a std::cin statement is:

 std::cin >>   variable   ; 

This works for all types of simple variables such as int , float , char , and wchar_t .

Reading strings is a little more difficult. To read a string, use the statement:

 std::getline(std::cin,   string   ); 

For example:

 std::string name;    // The name of a person std::getline(std::cin, name); 

We discuss the std::getline function in Chapter 16.

When reading a string, the std::cin class considers anything up to the end-of-line part of the string. Example 5-5 reads a line from the keyboard and reports the line's length.

Example 5-5. len/len.cpp
 #include <string> #include <iostream> std::string line;       // A line of data int main(  ) {     std::cout << "Enter a line:";     std::getline(std::cin, line);     std::cout << "The length of the line is: " << line.length(  ) << '\n';     return (0); } 

When we run this program we get:

 Enter a line:  test  The length of the line is: 4 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net