Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3042

[RESOLVED] Reading in Data and Displaying out all Data in Linked List

$
0
0
Hi

I am wanting to be able to add as many different instances of data, while the user has not stated 'n', but then it only ever writes back to the screen the input for the last set of data. Where as I want to display back to the screen all the data that has been entered.
Could anyone help with this please?

Code:

#include <iostream>
#include <string>

/*Function Declaration*/
void createItem();
void returnItem();

//Items structure
struct Items
{
        std::string name;
        float cost;
        int itemsInStock;
        Items* next;
};

Items* ptrFirst = NULL;
Items* ptrCurrent = NULL;
Items* ptrLast = NULL;

int main()
{
       
        createItem();

        returnItem();

}

void createItem()
{
        char ans;
        //Loop until the user has finished
        do
        {
        //Store the current pointer for latter use
        ptrLast = ptrCurrent;

        //Create new Item
        ptrCurrent = new Items;
        std::cout << "Please enter a product name: ";
        std::cin >> ptrCurrent ->name;
        std::cout << "Please enter the price of the product:";
        std::cin >> ptrCurrent ->cost;
        std::cout << "Please enter the number in stock: ";
        std::cin >> ptrCurrent ->itemsInStock;
        ptrCurrent ->next = NULL;

        //Set head of the list if necessary
        if (NULL == ptrFirst) ptrFirst = ptrCurrent;

        //Set the next pointer in the last Item structure created
        if (NULL != ptrLast) ptrLast ->next = ptrCurrent;

        system("PAUSE");

        std::cout << "Would you like to enter another item? (y/n) ";
        std::cin >> ans;

        }while (ans == 'y');
}

void returnItem( )
{
                std::cout << "Item is " << ptrCurrent ->name
                        << " it costs " << ptrCurrent ->cost
                        << " and there are " << ptrCurrent ->itemsInStock << " in stock.";

                system("PAUSE");
}

Thanks in anticipation

Viewing all articles
Browse latest Browse all 3042

Trending Articles