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

            cout << "fish #" << i+1 << ": ";

    }

// calculate average

    double total = 0.0;

    for (int j = 0; j < i; j++)

        total += fish[j];

// report results

    if (i == 0)

        cout << "No fish\n";

    else

        cout << total / i << " = average weight of "

            << i << " fish\n";

    cout << "Done.\n";

    return 0;

}

Note

As mentioned earlier, some execution environments require additional code to keep the window open so that you can see the output. In this example, because the input 'q' turns off further input, the treatment is more elaborate:

if (!cin)  // input terminated by non-numeric response

{

    cin.clear();  // reset input

    cin.get();    // read q

}

cin.get();        // read end of line after last input

cin.get();        // wait for user to press

You also could use code similar to this in Listing 6.13 if you wanted the program to accept more input after exiting the loop.

Listing 6.14 further illustrates using the cin return value and resetting cin.

The expression cin >> fish[i] in Listing 6.13 is really a cin method function call, and the function returns cin. If cin is part of a test condition, it’s converted to type bool. The conversion value is true if input succeeds and false otherwise. A false value for the expression terminates the loop. By the way, here’s a sample run of the program:

Please enter the weights of your fish.

You may enter up to 5 fish .

fish #1: 30

fish #2: 35

fish #3: 25

fish #4: 40

fish #5: q

32.5 = average weight of 4 fish

Done.

Note the following line of code:

while (i < Max && cin >> fish[i]) {

Recall that C++ doesn’t evaluate the right side of a logical AND expression if the left side is false. In such a case, evaluating the right side means using cin to place input into the array. If i does equal Max, the loop terminates without trying to read a value into a location past the end of the array.

The preceding example doesn’t attempt to read any input after non-numeric input. Let’s look at a case that does. Suppose you are required to submit exactly five golf scores to a C++ program to establish your average. If a user enters non-numeric input, the program should object, insisting on numeric input. As you’ve seen, you can use the value of a cin input expression to test for non-numeric input. Suppose the program finds that the user enters the wrong stuff. It needs to take three steps:

1. Reset cin to accept new input.

2. Get rid of the bad input.

3. Prompt the user to try again.

Note that the program has to reset cin before getting rid of the bad input. Listing 6.14 shows how these tasks can be accomplished.

Listing 6.14. cingolf.cpp

// cingolf.cpp -- non-numeric input skipped

#include

const int Max = 5;

int main()

{

    using namespace std;

// get data

    int golf[Max];

    cout << "Please enter your golf scores.\n";

    cout << "You must enter " << Max << " rounds.\n";

    int i;

    for (i = 0; i < Max; i++)

    {

        cout << "round #" << i+1 << ": ";

        while (!(cin >> golf[i])) {

            cin.clear();     // reset input

            while (cin.get() != '\n')

                continue;    // get rid of bad input

            cout << "Please enter a number: ";

        }

    }

// calculate average

    double total = 0.0;

    for (i = 0; i < Max; i++)

        total += golf[i];

// report results

    cout << total / Max << " = average score "

            << Max << " rounds\n";

    return 0;

}

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

Please enter your golf scores.

You must enter 5 rounds.

round #1: 88

round #2: 87

round #3: must i?

Please enter a number: 103

round #4: 94

round #5: 86

91.6 = average score 5 rounds

Program Notes

The heart of the error-handling code in Listing 6.14 is the following:

while (!(cin >> golf[i])) {

    cin.clear();     // reset input

    while (cin.get() != '\n')

        continue; // get rid of bad input

    cout << "Please enter a number: ";

}

If the user enters 88, the cin expression is true, and a value is placed in the array. Furthermore, because cin is true, the expression !(cin >> golf[i]) is false, and this inner loop terminates. But if the user enters must i?, the cin expression is false, nothing is placed into the array, the expression !(cin >> golf[i]) is true, and the program enters the inner while loop. The first statement in the loop uses the clear() method to reset input. If you omit this statement, the program refuses to read any more input. Next, the program uses cin.get() in a while loop to read the remaining input through the end of the line. This gets rid of the bad input, along with anything else on the line. Another approach is to read to the next whitespace, which gets rid of bad input one word at a time instead of one line at a time. Finally, the program tells the user to enter a number.

Simple File Input/Output

Sometimes keyboard input is not the best choice. For example, suppose you’ve written a program to analyze stocks, and you’ve downloaded a file of 1,000 stock prices. It would be far more convenient to have the program read the file directly than to hand-enter all the values. Similarly, it can be convenient to have a program write output to a file so that you have a permanent record of the results.

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

Все книги серии 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.

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

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