closed

solved
Last edited on
1
2
3
4
struct products {
char name[1]; 
//
};

This implies that data member name is a char array of size 1 i.e. it can only take one character (which is the max size a char type variable would take anyway). So having a char array of size 1 is redundant and also perhaps what you may not be looking for if this member has to describe the name of the product, so suggest you replace it with std::string name instead which would be more descriptive.

Also you're using goto repeatedly in your program, see here for some advice cautioning against such use:
https://www.tutorialspoint.com/cplusplus/cpp_goto_statement.htm
you can instead write the same program using switch-case that'd offer the same functionality while helping you retain greater control over your program

Now, in terms of deleting a product first you have to find it and to find it you have to decide a unique way of identifying it – looking at struct products, perhaps int productID is the most likely candidate. Below is a stylized example that uses only 5 products and attempts to delete the product with id == 3 from the C-style array of products. It'd be much simpler if you could have used std::vector<products> instead. Finally note in the following program I'd be declaring the custom datatype products as 'Product' instead, using upper-case and singular to describe a generic product and lower-case to describe instances of this generic product:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>
#include <utility>

struct Product
{
    std::string m_name = " ";
    int m_ID = 0;
    Product(){}
    Product (const std::string name, const int ID)
        : m_name(name), m_ID(ID){}
    //member-initialization list: http://stackoverflow.com/questions/18811742/what-is-member-initializer-in-c11
};

int main()
{
    Product products[5];
    products[0] = Product{"AAaa", 1};//calling the overloaded ctor in each case
    products[1] = Product {"BBbb", 2};
    products[2] = Product{"CCcc", 3};
    products[3] = Product{"DDdd", 4};
    products[4] = Product{"EEee", 5};

    auto itr = std::find_if(std::begin(products), std::end(products), [](const Product& p){return p.m_ID == 3;});
    //http://en.cppreference.com/w/cpp/algorithm/find
    //http://en.cppreference.com/w/cpp/iterator/begin
    if (itr != std::end(products))//i.e. productID == 3 exists in the array
    {
        for (auto i = std::distance(std::begin(products), itr); i < 4; ++i)
            //http://en.cppreference.com/w/cpp/iterator/distance
        {
            products[i] = std::move(products [i + 1]);
            //uses the compiler generated move assignment overload
            //http://en.cppreference.com/w/cpp/language/move_assignment
        }
    }
    else
    {
        std::cout << "Element not found \n";
    }
    if (itr != std::end(products))
    {
        for (size_t i = 0; i < 4; ++i)//notice upper value of array size is now 4, not 5
        {
            std::cout << products[i].m_name << " " << products[i].m_ID << "\n";
        }
    }
}

To update the products you proceed in exactly the same way – find the product with std::find_if, then dereference the iterator returned by std::find_if to reach the underlying product object and make changes to it's data-members
Hi Gunnerfunner :)

i was working on this (but as a class exercise :D) but I am guessing your explanation will be much more useful. However, as I did this as my first (almost ever) class can you tell me what do you think? I know is not perfect ;) far from it.
I will post this in separate thread so not to overtake this one :)

closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/213776/
Topic archived. No new replies allowed.