Grocery list array C++ program

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:

#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?

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

Thank you!! This is my first post.

What are 3 and 100 in your for loops?

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

  • 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.
    for (int i = 1; i <= 100; i++)
    {
    cin >> itemName[i] >> itemCost[i];
    }
  • Why are you printing the total 3 times instead of just once?

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;