1. ## Inventory program

The problem sounds simple: create a program accepting inventory input from the user (vitamin name, quantity, price) that will display the input in columns as well as calculate grand totals.

I understand using set to create columns, and how to calculate totals. My question is...where do I start? I'm not sure how to accept unlimited amounts of input (i.e. how do I name variables when they could be unlimited in number?)

We haven't yet discussed classes, so using anything before that is acceptable (functions, switches, arrays, strings).

2. You do not need an unknown number of variables. You know exactly how many you have.
The name, the quantity and the price, Plus the total quantity and the total price.

Every loop around just dump the name, quantity and price into temp variables, reuse those same variables over and over. Then you can just loop until the user types in something appropriate for the end of input.

3. If I use the same variable name over and over, won't the previous entry be overwritten?

4. For example, it would ask the user for name, quantity and price, and after a command it would display the info like this:

A 22 13.99
B 9 9.99
B12 5 14.99

If I use a temp variable, won't it only hold one piece of information? Somehow I need to dump that info into unique slots, or am I thinking about this wrong?

5. Yes, sorta.

There are various ways.
You can use pointers to have a dynamic array (for user declared size of arrya).
It would be very concise with classes/structs but since you said they are out.

You can also dump the data into a file as you go along and then just read it back to display.

You can also say declare an array of names, quantity and price. I.E.
Code:
```string name[20];
int quantity[20];
double price[20];```
And tell the user up to 20 (or any number you want).

Or, you can input the data as strings.
Set total = total + user_input in each loop.
But have the price and quantity as strings. Convert the input to characters. Each time through the loop add on the additional data to the string(s) and separate them with a space. With the string class you can go for as many as you want.

Then when you are done with the loop(s) you will have this.

String name == “a b c d e f”
String quantity == “10 9 78 2 1 56”
String price == “1.99 4.55 6.99 5.55 12.98 4.44”
Then you can just read the string and output the data via spaces. No need to even bother converting back to numeric values because 3 char to humans is the same as 3 integer.

But the easiest way would be with dynamic array, but that requires pointers. Has your class covered pointers yet? Plus *cough* classes would make it even easier!

The next easiest option would probably be just declare the set array size and do not let the user do more than that, followed by outputting it to a temp file and just using the one variables. The string thing is kinda silly, but it would still work fairly easy.

6. If you need to save the input, start with a static array with a large size
Code:
```const int MAX_PRODUCTS = 100;
Product products[MAX_PRODUCTS];```
Or you could use a vector from standard template library
Code:
```#include <vector>
int main() {
vector<Product> products;
while (/* some condition */) {
Product p = getProductInput();
products.push_back(p);
}

...
}```
Note. "Product" can be defined as a class or a struct
Reference for vector http://cppreference.com/cppvector/

7. After a bit of research, I'm thinking a dynamic array would work best. Yep, we've covered (uggh) pointers. Unfortunately we haven't covered classes/structs yet, but yeah, I can see how that would be the best choice.

Thanks for the input!

8. Dynamic arrays are at the end of the book...so I'm stuck with a static array. Here is what I have for the beginning, but come up with an error "no conversion from const int to to char *"

Bah, I'm certain it has to do with pointers, but adding an asterisk to inventory in the while statement makes the "!=" a syntax error.

Code:
```int main()  // initiates main function

{
const int arraySize = 100; // sets a permanent number in the array, maximum 100 in inventory
char inventory[ arraySize ][4]; // array inventory has 100 elements with 3 chars max
int quantity[ arraySize ][4] = {0}; // represents the quantity array, limits quantity to 3 digits
double price[ arraySize ][4] = {0}; // represents the price array of the product
double total = 0; // represents total value of the inventory

cout << "Vitamin Inventory Program, 100 product maximum" << endl;

for ( int count = 0; count < arraySize; count++ ) { // count represents corresponding product numbers

if ( count != 0 ) { // eliminates the zero in the array
cout << setw( 10 ) << count << setw( 10 ) << inventory[count - 1]; // counts and ties vitamins 1 - 100

} // end if structure

} // end for structure

while ( inventory != '-1' && count < arraySize ) {  // loops until -1 is entered or count exceeds 100```

9. int quantity[ arraySize ][4] = {0}; // represents the quantity array, limits quantity to 3 digits
No, it does not. It creates 400 int variables. Each can be as big or as little as they want. Only char array's [#] say how many specific characters it holds. You just want the int quantity[arraySize].
Same thing with the price.

Code:
` while ( inventory != '-1' && count < arraySize )`
'-1' is a character not a number because of the ''.
Innovatory is characters not numbers anyway.
And just plain inventory is not anything, only inventory[#][#] are something.
Have you been introduced to strings yet/can you use them? That way you can just do a simple 1 dimensional array on the name (the others need to be just 1 dimensional as well). And even if your while loop condition was valid you probably would want || instead of &&.

10. Thanks for the input! We've done arrays sparingly, and my impression was that an array[#][#] was three dimensional, limiting quantity in the first bracket and size in the second. We've done strings too, but the issue with using strings is that eventually I have to sum quantity * price for totals.

I'll give the string idea a shot tomorrow.

11. Use the string for the names, that way you do not need name[x][x], just name[x].
And each name is in name[0],name[1], ect.
As for the int and double, keep the int and double arrays, but they only need to be 1 dimension.
int quanity[x] and double price[x].

12. I went back and reread the chapter on arrays, and found out that the first subscript represents rows, and the second represents columns - wasn't thinking that before, so that changes things!

Here's what I've come up with, but it's not saving subsequent entries; it only prints out the most recent entry.

Also, it's not calculating totals correctly, it's printing out addresses (do I need a pointer here?)

Code:
```#include <iostream>
#include <iomanip>  // contains function prototype for setw

using namespace std;

int inventory[ 100 ][ 2 ]; // 100 entries, 3 subscripts representing columns vitamin(0), quantity, price
char vitamin_string_name; // first subscript (inventory[100][0]
double unit_price = 0; // second subscript (inventory[100][1])
int num_of_jars = 0; // third subscript (inventory[100][2])
double totalvalue( void ); // will calculate total value for one vitamin
double average = 0; // average price of a vitamin
double vitamin_count = 0; // total number of jars in the inventory
int counter = 1;  // true/false counter for user entries
int i = 0; // index tied to count inventory

int main()  // initiates main function
{

cout << "Vitamin Inventory Program; 100 entries maximum" << endl;

for ( i = 0; i < 100; i++ ){ // counter for index tied to vitamin name
cout << "Vitamin, Price, Quantity: ";
cin >> vitamin_string_name >> unit_price >> num_of_jars;
inventory[ i ][ 0 ] = vitamin_string_name;
inventory[ i ][ 1 ] = unit_price;
inventory[ i ][ 2 ] = num_of_jars;
cout << "Enter another? 1 for Yes, 0 for No: ";
cin >> counter;
if ( counter == 0 )
break;
} // end for counter

cout << "The inventory is:\n";
cout << "Vitamin" << setw( 8 ) << "Price" << setw( 12 ) << "Quantity" << setw( 12 ) << "Total" << endl;
cout << vitamin_string_name << setw( 14 )<< unit_price << setw( 9 ) << num_of_jars << setw( 15 ) << totalvalue << endl;

return 0;

}  // end function main
double totalvalue( void ) {

double totalvalue = 0;

totalvalue = ( inventory[i][1] * inventory[i][2] );

} // end function totalvalue```

13. Code:
`int inventory[100][2]; // 100 entries, 3 subscripts representing columns vitamin(0), quantity, price`
You declared space for 2 elements, why do you think you have room for 3? (you knew 100 meant 100 )
A declaration of array[2] means you can access array[0] and array[1] only.

You can not mix types in a two dimensional array. (see those warnings about trying to shove a double into an int)
You are going to need 3 separate arrays.

string vitamins[100];
int num_of_jars[100];
double unit_price[100];

You have good intentions trying to combine them into "inventory", but you cannot do that (in a reasonable way) without a struct or class.

Code:
`<< totalvalue() << endl;`

well of course it only prints the most recent. (They're being stored, you're just not printing them)
After the for loop i is set to the most recent entry, and you call cout one time. (Not counting your headings)

To print all the inventory you need another loop starting from 0 to how many you read in.

None of those variables need to or should be global.

14. I'm at it again. Still getting errors and a bit frustrated for spending so much time on this project. I haven't gotten to the average portion yet. Thank you for taking the time to respond.

Code:
```#include <iostream>
#include <iomanip>  // contains function prototype for setw

using namespace std;

char vitamin_string_name[100];
double unit_price[100];
int num_of_jars[100];
double totalvalue( void ); // will calculate total value for one vitamin
double average( int [], int ); // average price of a vitamin
double vitamin_count = 0; // total number of jars in the inventory
int counter = 1;  // true/false counter for user entries
int i = 0; // index tied to count inventory
int j = 0;

int main()  // initiates main function
{

cout << "Vitamin Inventory Program; 100 entries maximum" << endl;

for ( i = 0; i < 100; i++ ){ // counter for index tied to vitamin name
cout << "Vitamin, Price, Quantity: ";
cin >> vitamin_string_name[i] >> unit_price[i] >> num_of_jars[i];
cout << "Enter another? 1 for Yes, 0 for No: ";
cin >> counter;
if ( counter == 0 )
break;
} // end for counter

cout << "The inventory is:\n";
cout << "Vitamin" << setw( 8 ) << "Price" << setw( 12 ) << "Quantity" << setw( 12 ) << "Total" << endl;
for ( j = 0; j == i; j++ ){
cout << vitamin_string_name[j] << setw( 14 )<< unit_price[j] << setw( 9 ) << num_of_jars[j] << setw( 15 ) << totalvalue << endl;

return 0;

}  // end function main

double totalvalue( void ) {

double totalvalue = 0;

totalvalue = ( unit_price[i] * num_of_jars[i] );

} // end function totalvalue```

Code:
`char vitamin_string_name[100];`
The name of the variable makes me think that you want a string instead of a char. like this
Code:
`char vitamin_string_name[100][20];// 100 names with max 19 chars`
Code:
`double totalvalue( void ); // will calculate total value for one vitamin`
This doesn't work. This function has to know witch vitamins totalvalue to calculate.
I would use this prototype
Code:
`double totalvalue( int idx ); // will calculate total value for one vitamin`
why do you use double for the total number of jars ( you won't have half a jar )
Code:
`int vitamin_count = 0; // total number of jars in the inventory`
and you should update vitamin_count in the input loop.

Code:
```//for ( j = 0; j == i; j++ ){ // this way your loop will never be entered
for ( j = 0; j < vitamin_count; j++ ){ // better
cout << vitamin_string_name[j] << setw( 14 )<< unit_price[j] << setw( 9 ) << num_of_jars[j] << setw( 15 ) << totalvalue(j) << endl; // you want to call totalvalue
} // that one is missing```
I haven't gotten to the average portion yet.
That one should be easy. Just loop over the unit_price from 0 to vitamin_count -1 add them all up and divide the result by vitamin_count.

BTW the whole thing would be a lot easier if you would use a struct.
e.g.
Code:
```struct vitamin {
char [20]  name;
double     unit_price;
int        quantity;
};```
and declare inventory as
Code:
`struct vitamin[100] inventory;`
This way you could easily get rid of all the global variables by passing that array and the number of items in the inventory to the functions totalvalue() and average()