Inventory program

This is a discussion on Inventory program within the C++ Programming forums, part of the General Programming Boards category; The problem sounds simple: create a program accepting inventory input from the user (vitamin name, quantity, price) that will display ...

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).

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.

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.

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.

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 &&.

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.

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].

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.

it's printing out addresses

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.