Convert characters in s to floating point value if

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: < int(ch) << ")\n"; fs.seekp(1); // move writing position (‘p’ for ‘put’) to 1 (the 2 nd character) // move fs<<'y'; // write and increment writing position // Stroustrup/Programming 19 19 Positioning Positioning Whenever you can Use simple streaming Streams/streaming is a very powerful metaphor Write most of your code in terms of “plain” istream and ostream Write istream ostream Positioning is far more error-prone Handling of the end of file position is system dependent and Handling basically unchecked basically Stroustrup/Programming 20 String streams String A stringstream reads/writes from/to a string stringstream string rather than a file or a keyboard/screen double str_to_double(string s) // if possible, convert characters in s to floating-point value // if convert { istringstream is(s); // make a stream so that we can read from s // make double d; is >> d; if (!is) error("double format error"); return d; } double d1 = str_to_double("12.4"); // testing // testing double d2 = str_to_double("1.34e-3"); double d3 = str_to_double("twelve point three"); // will call error() // will Stroustrup/Programming 21 String streams String Very useful for formatting into a fixed-sized space (think GUI) for extracting typed objects out of a string Stroustrup/Programming 22 Type vs. line Type Read a string string name; cin >> name; cout << name << '\n'; // input: Dennis Ritchie // input: // output: Dennis // output: Read a line string name; getline(cin,name); // input: Dennis Ritchie // input: cout << name << '\n'; // output: Dennis Ritchie // output: // now what? // now // maybe: // maybe: istringstream ss(name); ss>>first_name; ss>>second_name; Stroustrup/Programming 23 Characters Characters You can also read individual characters char ch; while (cin>>ch) { // read into ch, skipping whitespace characters // read if (isalpha(ch)) { // do something do } } while (cin.get(ch)) { // read into ch, don’t skip whitespace characters // read if (isspace(ch)) { if // do something // do } else if (isalpha(ch)) { // do something else // do } } Stroustrup/Programming 24 Character classification functions Character If you use character input, you often need one or If more of these (from header <cctype> ): <cctype> isspace(c) isalpha(c) isdigit(c) isupper(c) islower(c) isalnum(c) // is c whitespace? (' ', '\t', '\n', etc.) // whitespace? // is c a letter? ('a'..'z', 'A'..'Z') note: not '_' // letter? // is c a decimal digit? ('0'.. '9') // decimal // is c an upper case letter? an // is c a lower case letter? // lower // is c a letter or a decimal digit? // letter Stroustrup/Programming 25 Line-oriented input Line-oriented Prefer >> to getline() Prefer >> getline() People often use getline() because they see no alternative People getline() i.e. avoid line-oriented input when you can But it often gets messy When trying to use getline(), you often end up When getline() using >> to parse the line from a stringstream using >> stringstream using get() to read individual characters using get() Stroustrup/Programming 26 26 Next lecture Next Graphical output Creating a window Drawing graphs Stroustrup/Programming 27...
View Full Document

Ask a homework question - tutors are online