0

So, my program uses a while loop to fill two separate vectors by asking the name of an item (vector one) then asking the price of the item (vector two).

    double ItemCost;
    vector<double> Cost;
    string ItemName;
    vector<string> Item;

        while (ItemName != "done")
        {

            cout << "Item name: "; cin >> ItemName;
            Item.push_back(ItemName);// gets item name from use and stores in the vector "Item"
            if (ItemName != "done") 
            {
                cout << "Item cost: $"; cin >> ItemCost;
                Cost.push_back(ItemCost);// gets cost of item and stores it in the vector "cost"
            }
            else
            {
                continue;
            }
    }
system("CLS");

So after the clear screen, I would like the program to output a screen that shows the item name, then to the right it's cost. Then on the next line the same thing for the 2nd item input. Essentially like this would display:

cout << Item[0]; cout << "         $" << Cost[0];
cout << Item[1]; cout << "         $" << Cost[1] << endl;
cout << Item[2]; cout << "         $" << Cost[2] << endl;

But, I want it to do this no matter how many items are input, also doing it the way I did above is obviously a bad Idea if they input less then the amount I have in the code, because the program will try to reach outside the vectors occupied memory ect. That was just to give an example of the format I'm going for.

Pingaz
  • 3
  • 2

3 Answers3

2

If the vectors are the same size, you can use a simple for loop to iterate over the contents:

for (int i = 0; i < Item.size(); ++i) {
    cout << Item[i] << " $" << Cost[i] << endl;
}

Before you run this code, you might want to check the two vectors have the same size using a debug assertion:

assert(Item.size() == Cost.size();

Alternatively, you could use perhaps something like std::min to only loop up the the smallest of the two vectors in case they are different sizes.

ajshort
  • 3,684
  • 5
  • 29
  • 43
1

I would suggest using a std::vector of std::pair to store the price and item together this is simply because it makes keeping track of which price is associated with which item. This removes the need to check that both vectors are the same size and minimizes the chance for an error to occur. Then it is a simple matter of using a range based for loop to iterate through each pair and print them. I have shown an example below with a few improvements. There are a number of advantages of using std::pair over a simple struct, one such advantage is the inclusion of the necessary Boolean functions to be used in something like std::sort() Which if used would essentially sort your list of items alphabetically. This is very useful if you want to future proof your code.

double ItemCost;
string ItemName;
vector<std::pair<string, double> > Item;

    while (ItemName != "done")
    {
        cout << "Item name: "; 
        // Using std::getline() as it terminates the string at a newline.
        std::getline(std::cin, ItemName);

        if (ItemName != "done") 
        {
            cout << "Item cost: $"; cin >> ItemCost;
            Item.push_back(std::make_pair (ItemName,ItemCost));
        }
        else
        {
            continue;
        }
    }

system("CLS");

// Printing here using c++11 range based for loop, I use this extensively
// as it greatly simplifies the code.

for (auto const &i : Item)
{ 
    std::cout << i.first << "\t\t$" <<  i.second;
                         //  ^^ Using double tab for better alignment
                         //     instead of spaces.
}
silvergasp
  • 1,517
  • 12
  • 23
0

Vector knows how big it is so there are a bunch of simple solutions, the easiest being

for (size_t index = 0; index < Item.size() && index < Cost.size(); index++)
{
    cout << Item[index]; cout << "         $" << Cost[index] << endl;
}

But a better idea is to have one vector that stores something like

struct ItemCost
{
    string name;
    double cost; // but it's generally safer to store money by pennies in integers
                 // You can't always count on floating point numbers in comparisons
}

This way Item and Cost can never get out of synch.

Read here for more on why floating point fails for precise things like money: How dangerous is it to compare floating point values?

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54