Читаем C++ Primer Plus полностью

The call to cin.peek() peeks at the next input character and assigns its value to ch. Then the while loop test condition checks that ch is neither a period nor a newline. If this is the case, the loop reads the character into the array and updates the array index. When the loop terminates, the period or newline character remains in the input stream, positioned to be the first character read by the next input operation. Then the code appends a null character to the array, making it a string.

The gcount() method returns the number of characters read by the last unformatted extraction method. That means characters read by a get(), getline(), ignore(), or read() method but not by the extraction operator (>>), which formats input to fit particular data types. For example, suppose you’ve just used cin.get(myarray, 80) to read a line into the myarray array and you want to know how many characters were read. You could use the strlen() function to count the characters in the array, but it would be quicker to use cin.gcount() to report how many characters were just read from the input stream.

The putback() function inserts a character back in the input string. The inserted character then becomes the first character read by the next input statement. The putback() method takes one char argument, which is the character to be inserted, and it returns type istream &, which allows the call to be concatenated with other istream methods. Using peek() is like using get() to read a character and then using putback() to place the character back in the input stream. However, putback() gives you the option of putting back a character that is different from the one just read.

Listing 17.14 uses two approaches to read and echo input up to, but not including, a # character. The first approach reads through the # character and then uses putback() to insert the character back into the input. The second approach uses peek() to look ahead before reading input.

Listing 17.14. peeker.cpp

// peeker.cpp -- some istream methods

#include

int main()

{

    using std::cout;

    using std::cin;

    using std::endl;

//  read and echo input up to a # character

    char ch;

    while(cin.get(ch))          // terminates on EOF

    {

        if (ch != '#')

            cout << ch;

        else

        {

            cin.putback(ch);    // reinsert character

            break;

        }

    }

    if (!cin.eof())

    {

        cin.get(ch);

        cout << endl << ch << " is next input character.\n";

    }

    else

    {

        cout << "End of file reached.\n";

        std::exit(0);

    }

    while(cin.peek() != '#')    // look ahead

    {

        cin.get(ch);

        cout << ch;

    }

    if (!cin.eof())

    {

        cin.get(ch);

        cout << endl << ch << " is next input character.\n";

    }

    else

        cout << "End of file reached.\n";

    return 0;

}

Here is a sample run of the program in Listing 17.14:

I used a #3 pencil when I should have used a #2.

I used a

# is next input character.

3 pencil when I should have used a

# is next input character.

Program Notes

Let’s look more closely at some of the code in Listing 17.14. The first approach uses a while loop to read input:

while(cin.get(ch))            // terminates on EOF

{

    if (ch != '#')

        cout << ch;

    else

    {

        cin.putback(ch);  // reinsert character

        break;

    }

}

The expression cin.get(ch) returns false on reaching the end-of-file condition, so simulating end-of-file from the keyboard terminates the loop. If the # character shows up first, the program puts the character back in the input stream and uses a break statement to terminate the loop.

The second approach is simpler in appearance:

while(cin.peek() != '#')     // look ahead

{

    cin.get(ch);

    cout << ch;

}

The program peeks at the next character. If it is not the # character, the program reads the next character, echoes it, and peeks at the next character. This continues until the terminating character shows up.

Now let’s look, as promised, at an example—Listing 17.15—that uses peek() to determine whether an entire line has been read. If only part of a line fits in the input array, the program discards the rest of the line.

Listing 17.15. truncate.cpp

// truncate.cpp -- using get() to truncate input line, if necessary

#include

const int SLEN = 10;

inline void eatline() { while (std::cin.get() != '\n') continue; }

int main()

{

    using std::cin;

    using std::cout;

    using std::endl;

    char name[SLEN];

    char title[SLEN];

    cout << "Enter your name: ";

    cin.get(name,SLEN);

    if (cin.peek() != '\n')

        cout << "Sorry, we only have enough room for "

                << name << endl;

    eatline();

    cout << "Dear " << name << ", enter your title: \n";

    cin.get(title,SLEN);

    if (cin.peek() != '\n')

        cout << "We were forced to truncate your title.\n";

    eatline();

    cout << " Name: " << name

         << "\nTitle: " << title << endl;

    return 0;

}

Here is a sample run of the program in Listing 17.15:

Enter your name: Ella Fishsniffer

Sorry, we only have enough room for Ella Fish

Dear Ella Fish, enter your title:

Executive Adjunct

We were forced to truncate your title.

 Name: Ella Fish

Title: Executive

Note that the following code makes sense whether or not the first input statement reads the entire line:

Перейти на страницу:

Все книги серии Developer's Library

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных