Разорвать цикл без добавления строки, которая останавливает цикл while

Как указано в других ответах, это невозможно с необработанным указателем формы SomeObject* somePointer. Однако c++11 представил новый набор управления динамической памятью и новых интеллектуальных указателей . Используя интеллектуальный указатель, вы можете определить, доступен ли ресурс. Например, в следующем:

std::weak_ptr w; // Our pointer to a resource.
{
    std::shared_pointer s = std::make_shared(5); // The resource.

    w = s;       // We can set the weak pointer to the shared pointer.

    auto s2 = w; // Here we can promote the weak pointer to a shared  pointer to control 
                 // the resource.

    *s2 = 6;     // Here we can use the resource.

}                // Here the resource is destroyed.

auto s2 = w;     // Here we will fail to get the resource because it has been destroyed. We
                 // have successfully used smart pointers to detect if the resource exists.

Подробнее о std :: shared_ptr и std :: weak_ptr для получения дополнительных примеров. До c++11 эквивалентные типы интеллектуальных указателей доступны в boost .

-2
задан Davidoff7776 23 February 2019 в 14:07
поделиться

1 ответ

Если product равно "stop", нет необходимости запрашивать количество или , чтобы добавить что-либо в список покупок. Сделайте эту проверку ранее .

while True:
    try:
        product = get_user_input("Input the product name: ")
        if product == "stop":
            break
        quantity = get_user_input("Input the product quantity: ", int)
        shopping_list.add_item(product, quantity)
    except Exception as e:
        print("\nAn error occurred:", e)
0
ответ дан chepner 23 February 2019 в 14:07
поделиться
Другие вопросы по тегам:

Похожие вопросы: