0

when I'm executing this program, it works fine just for one option, but if I try to choose an other option when executing, it will throw an error in this line:

cout << "The available quantity is: " << *(it->second.find(disp)) << endl;

here's my program:

void avail_art(void)
{
    char c,con;
    int quantity,disp=MAX;
    bool b = true;
    map<string, unordered_set<int>>m{
        {"Mouse",{120,disp}},
        {"Keyboard",{75,disp}},
        {"Monitor",{750,disp}},
        {"GPU",{1200,disp}},
        {"CPU",{1000,disp}}
    };
    auto it = m.find("CPU");
    cout << "M - for mouse." << endl;
    cout << "K - for keyboard." << endl;
    cout << "R - for monitor." << endl;
    cout << "G - for GPU." << endl;
    cout << "C - for CPU." << endl;
    cout << "Q - for quit." << endl;
    cout << "======================" << endl;
    while (b)
    {
        cout << "Enter your choice: ";
        cin >> c;
        switch (c)
        {
        case 'M':
            it = m.find("Mouse");
            cout << "You've choosen the Mouse!" << endl;
            cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
        l:
            cout << "Enter the quantity: ";
            cin >> quantity;
            if (quantity > * (it->second.find(disp)))
            {
                cout << "Out of Stock! the Stock has only :" << *(it->second.find(disp)) <<" Piece."<< endl;
                goto l;
            }
            cout << "Confirm? y/n: ";
            cin >> con;
            if (con == 'y')
            {
                auto its=it->second.find(disp);
                it->second.insert(disp=(*its - quantity));
                it->second.erase(its);
            }
            break;
        case 'K':
            it = m.find("Keyboard");
            cout << "You've choosen the Keyboard!" << endl;
            cout << "The available quantity is: " << *(it->second.find(disp)) << endl;
            cout << "Enter the quantity: ";
            cin >> quantity;
            cout << "Confirm? y/n :";
            cin >> con;
            if (con == 'y')
            {
                auto its = it->second.find(disp);
                it->second.insert(disp = (*its - quantity));
                it->second.erase(its);
            }
            break;
        }

    }
}

can someone help me please

  • you should check if the iterator returned from `find` is valid. In general dont assume that stuff just works. You will get problems like this when it doesn't – 463035818_is_not_an_ai Apr 20 '20 at 17:47
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5910058) – Jesper Juhl Apr 20 '20 at 17:48
  • 1
    `goto l;` -- Please don't do this. A simple `do { check if stock is available } while(stock_not_available);` is more appropriate. – PaulMcKenzie Apr 20 '20 at 17:50
  • @PaulMcKenzie even tho the problem is not there, before I did this, it didn't work. I can't see why I had this problem but I think something related with the access to the member of the set –  Apr 20 '20 at 18:11
  • 1
    The reason why you shouldn't use `goto` in code is that it can render your question not worth the time to solve. Not being disrespectful, but any code with the hint of begin spaghetti lessens the chance that others will want to untangle the spaghetti. Also, having a `goto` that goes "up" instead of "down" in the code is even more likely that the code will be skipped over by persons that may want to help. Just trying to be realistic. – PaulMcKenzie Apr 20 '20 at 18:18
  • 2
    When asking for help with an error, it's helpful to include the actual error. Tell us what's going wrong; don't make us guess. – Caleb Apr 20 '20 at 19:54
  • You may want to read about [why 'Can somebody help me?' is not an actual question](https://meta.stackoverflow.com/a/284237/11107541). – starball Nov 29 '22 at 22:53

0 Answers0