# Grocery list array C++ program

**URL:** <https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754>\
**Category:** General\
**Created:** [April 13, 2017, 8:56pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754 "2017-04-13T20:56:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![codycrow](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codycrow/32/89058_2.png) [@codycrow](https://forum.freecodecamp.org/u/codycrow)\
**Post date:** [April 13, 2017, 8:56pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/1 "2017-04-13T20:56:53Z")

</div>

Hey guys and gals,

I am a new coder and I am taking a class on C++. This week we are dealing with arrays. I thought I had a solid grasp on the subject, but I have been stumped! Here are the assignment instructions:

**Write a program that allows you to enter grocery item names into an array of strings and the cost of each item in an array of doubles. At the beginning of the program prompt the user to enter the total number of items they will be entering. Max value of 100. After entering the item names and cost, the application should display the names and cost and total cost of all items.**

Here is the code I have so far:

```auto
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    //declare arrays
    string itemName[100];
    double itemCost[100] = {0.0};

    //declare variables
    string names = "";
    double total = 0;
    int numItems = 0;

    //number of items
    cout << "Enter number of grocery items you will be entering: ";
    cin >> numItems;
    cout << "\n";
    
        for (int i = 1; i <= 100; i++)
        {
        cin >> itemName[i] >> itemCost[i];
        }

        //input message
    cout << "Please enter the item names as one word only. Example: tomatoes\n" ;
    cout << "Enter the cost as a decimal number. Example: 2.44\n\n";

    //item name and price input
    for (int i = 0; i < 3; ++i)
    {
        cout << "Enter item: ";
        cin >> itemName[i];
        cout << "Enter the cost of the " << itemName[i] << " $";
        cin >> itemCost[i];
        }

        cout << "\n";

    //Display Data
      for (int i = 0; i < 3; ++i)
{
    cout << itemName[i];
    cout << ' ';
    cout << itemCost[i];
    cout << endl;
}

//total calculations
for (int i = 1; i <= 3; i++)
{
    total += itemCost[i];
    cout << total;
}

return 0;
}

```

I am not sure how to set the max array and have or how to get the program to respond to the number of items. Also, not sure if that is the way to calculate and display the total. What am I missing?

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [April 13, 2017, 9:26pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/2 "2017-04-13T21:26:49Z")

</div>

I cleaned up your code.  
You need to use triple backticks to post code to the forum.  
See [this post for details](http://forum.freecodecamp.com/t/forum-code-formatting/25574).

---

<div class="post-metadata">

**Author:** ![codycrow](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codycrow/32/89058_2.png) [@codycrow](https://forum.freecodecamp.org/u/codycrow)\
**Post date:** [April 13, 2017, 9:32pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/3 "2017-04-13T21:32:57Z")

</div>

Thank you!! This is my first post.

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [April 13, 2017, 9:35pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/4 "2017-04-13T21:35:24Z")

</div>

What are `3` and `100` in your `for` loops?

---

<div class="post-metadata">

**Author:** ![codycrow](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/codycrow/32/89058_2.png) [@codycrow](https://forum.freecodecamp.org/u/codycrow)\
**Post date:** [April 13, 2017, 9:40pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/5 "2017-04-13T21:40:18Z")

</div>

I was trying to set the max array to 100 and input 3 items. This is what the program is supposed to output:

Enter number of grocery items you will be entering: 3

Please enter the item names as one word only. Example: tomatoes  
Enter the cost as a decimal number. Example: 2.44

Enter item 1: apples  
Enter the cost of the apples: 3.56

Enter item 2:: milk  
Enter the cost of the milk: 2.50

Enter item 3: bread  
Enter the cost of the bread: 2.99

OUTPUT:  
Items Cost   
apples $3.56  
milk $2.50  
bread $2.99

Total: $9.05

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [April 13, 2017, 9:44pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/6 "2017-04-13T21:44:38Z")

</div>

- Why would you make an array of length 100 if you are only going to allow 3 items?
- Why are you asking the user for the number of items if you are always going to require 3?
- This code expects the user to input something 100 times without ever prompting them. You then overwrite the first three values in your next `for` loop.

```auto
    for (int i = 1; i <= 100; i++)
    {
    cin >> itemName[i] >> itemCost[i];
    }

```

- Why are you printing the total 3 times instead of just once?

---

<div class="post-metadata">

**Author:** ![saren1986](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@saren1986](https://forum.freecodecamp.org/u/saren1986)\
**Post date:** [April 13, 2017, 9:51pm UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/7 "2017-04-13T21:51:45Z")

</div>

Hi,  
You should use a new and delete operator. E.g.  
string \*itemName;  
double \*itemCost;

itemName = new string[numItems];  
itemCost = new double[numItems];

Then you will have arrays with size what you exactly need.  
Next, you should use a for loop \>\> for (int i = 0; i \< numItems; i++)  
for input and display data.  
After all use delete operator to delete your arrays:  
delete [] itemName;  
delete [] itemCost;

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 11:33am UTC](https://forum.freecodecamp.org/t/grocery-list-array-c-program/105754/8 "2021-01-16T11:33:13Z")

</div>

Closed on January 16, 2021, 11:33am UTC


