Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to use a 2D array to store products and the 2D array consists of product name, product ID, product quantity and product price
How can I do this in a simple and tidy way?

Also, how can I search for a product by product ID or add a product to the array 2d

What I have tried:

string v[2][4] = {{“001”,”apple”,”15”,”20$”},{“002”,”milk”,”5”,”3$”}
Posted
Updated 6-Jan-22 4:37am
Comments
KarstenK 6-Jan-22 6:40am    
products are a one dimensional array. Best is to use class design as alreaedy written.
Rick York 6-Jan-22 11:07am    
That declaration has a problem. There are two rows of strings, each four characters long. The string "apple" occupies six characters (with an added null) and "milk" takes up five. The strings need to be bigger, at least six characters long.

Either create a class (this is the C++ way of doing things), or a struct (which is a C way of doing things.

You can then create an array of either the class or struct, and all the related info is kept together for you.

C++ Classes and Objects[^]
struct- C++ Tutorials[^]
 
Share this answer
 
Comments
Ah Ve 6-Jan-22 4:44am    
I know this but how can I represent it in 2d areay?
OriginalGriff 6-Jan-22 5:13am    
Why would you want to use a 2D array, unless you want a 2D array of class / struct instances?
The whole idea is that the class / struct instance holds all the relevant information in a single object, and that means you don't need to have it as a 2D array of strings but can use more appropriate types (int or float for numbers for example) and access the field by name instead of index.

It's really a lot simpler than you think, and helps you produce a whole load better code!
You have already created a 2D array, but as OriginalGriff suggests, a class or struct would be a better idea. And in fact a vector rather than a simple array would be even better.

As it stands you can search for any set by indexing the array, i.e. v[0], v[1] etc. Within each entry the product details will be at the secondary indices: 0, 1, 2 and 3. To add items to the list you would have to create a new array with space for the new items, copy all the old items to the new array and delete the old array. All in all making quite a lot of hard work which C++ could do for you automatically.
 
Share this answer
 
As others have pointed out, a array or vector of a suitable class to represent your product object is the way to go. If, however, you insist on using a 2D array, there are a couple of options. One is to have a 2D array of type any std::any - cppreference.com[^] Another option might be to use a tuple std::tuple - cppreference.com[^] Using a tuple doesn't really preserve array semantics - i.e you can't access a cell as products[row][column[, but it might scratch whatever particular itch it is you're trying to scratch.

NB std::tuple was introduced in C++11 and std::any was introduced in C++17
 
Share this answer
 
Unless you were specifically asked to store these values in a 2D array, you should go with the advice already given in the other solutions.

The main reason is that storing numeric values as string is definitiely not "tidy" and will cause all kind of problems in the rest of your program.

You have already presented a solution to your own question, but I've noticed you are using non-ascii codes for the double quotes and you are missing both a second closing brace, and a semicolon. Also, aggregate initialization does not use assignment: that '=' is wrong. All in all you have many errors in that one line, and quite likely the compiler error messages weren't overly helpful :
string v[2][4] = {{"001","apple","15","20$"},{"002","milk","5","3$"}
---------------^---^---^-^-----^-^--^-^---^---^---^-^----^-^-^-^--^-^^ 

With the right type of quote and the missing brace and semicolon and after deleting the '=' you get valid syntax:
string v[][4] {{"001","apple","15","20$"},{"002","milk","5","3$"}};


As for finding someting in that array, the easiest way is to use std::find or std::find_if from the algorithm library. This code finds the second entry and prints "milk":
C++
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string v[][4] {{"001","apple","15","20$"},{"002","milk","5","3$"}};
    auto has_id_002 = [](const string* values) { return values[0] == "002"; };
    auto result = find_if(v, v+1, has_id_002);
    cout << (*result)[1];
    return 0;
}


P.S.: As Richard pointed out, you can actually use '=' for initialization. Apparently I mixed that up with something else.
 
Share this answer
 
v2
Comments
Richard MacCutchan 6-Jan-22 6:41am    
That compiles cleanly for me with or without the "=" sign. Is that something in a later version of the C++ standard?
Stefan_Lang 6-Jan-22 6:55am    
You are correct. I just remembered the '=' not working in some similar code, but apparently that wasn't the same as here. The line didn't compile and after removing the '=' and fixing something else it worked, so I thought that's how it must be.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900