grocery list using arrays

Someone, please help.

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.

***Your program must work EXACTLY like the one shown below.***

Example Run:

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


My program:


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

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;
}





Figured it out myself.

#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.0;
int numItems = 0;
//number of items
cout << "Enter number of grocery items you will be entering: ";
cin >> numItems;

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 sub = 1; sub <= 3; sub += 1)
{
cout << "Enter item " << sub << ":";
cin >> itemName[sub];
cout << "Enter the cost of the " << itemName[sub] << ":";
cin >> itemCost[sub];
}
cout << "Iteams" << " " << "Cost" << endl;
//Display Data
for (int i = 1; i <= 3; ++i)
{
cout << itemName[i];
cout << ' ';
cout << "$" << itemCost[i];
cout << endl;
}// end for

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

cout << "Total:$ " << total << endl;

system("PAUSE");
return 0;
}

Topic archived. No new replies allowed.