Answer to Question #95708 in C++ for matimu

Question #95708
using linked list. develop a program that will prompt a user to insert any number of elements. the program should display the number of elements that the user will have decided to create.
1
Expert's answer
2019-10-03T05:18:24-0400
#include <list> // std::list<>
#include <iostream> // std::cout, std::cin, std::endl

int main()
{
    // Create a linked list
    std::list<int> lst;

    // Prompt an user to insert any number of elements
    int element, choice;
    do
    {
        // The user must input only integers, otherwise this code doesn't work
        std::cout << "Do you want to insert a new element? 0 - No, 1 - Yes: ";
        std::cin >> choice;
        while (choice != 0 && choice != 1)
        {
            std::cout << "Invalid input, try once more: ";
            std::cin >> choice;
        }
        if (choice == 1)
        {
            std::cout << "Enter an element: ";
            std::cin >> element;
            lst.push_back(element);
        }
    } while (choice != 0);


    // Display the number of elements that the user will have decided to create
    std::cout << lst.size() << std::endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS