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?