-1

Please forgive me for some of the errors I have made within my code, but I am new to this and since COVID is preventing me from reaching regular help, I thought I would turn here to ask for some help, rather than googling non-stop. We do not use "std::cout" just so you know. Also I am not quite getting how to update the SIZE of the array, with it being a const int. Do I need to use '&' or something? I tried that and it doesn't run the program.

I have this program, where we are supposed to add/remove/display codes to allow the user to add three-digit codes to an array or take them away, or display them, with a maximum amount of elements allowed of 1000.

If you could take a look at explain what I need to do instead, since I have been having issues getting this program to run correctly, with it never endingly displaying the wrong thing, and not working as intended.

This is what my output looks like after it reads "Enter y to enter more codes": Output

Thank you! Again, I am fairly new to this so forgive my errors made! Code:

#include <iostream>
#include <iomanip>
using namespace std;

//Function Prototypes
int addCodes(int codes[], const int SIZE);
int removeCodes(int codes[], const int SIZE);
void displayCodes(int codes[], int count);

int main()
{
    int response;
    const int SIZE = 1000;
    int codes[SIZE];
    int count = 0;
    const int ADD = 1, REMOVE = 2, DISPLAY = 3;
    cout << "-------Code Manager-------" << endl;

    do
    {
        cout << "\nEnter 1 to Add Codes" << endl;
        cout << "Enter 2 to Remove Codes" << endl;
        cout << "Enter 3 to Display Product Codes" << endl;
        cout << "\nEnter your selection: ";
        cin >> response;
        
        switch(response)
        {
        case (ADD):
            {
            for (int index = 0; index < SIZE; index++)
                addCodes(codes, SIZE);
                break;
            }
        case (REMOVE):
            {
            for (int index = 0; index < SIZE; index++)
                removeCodes(codes, SIZE);
                break;
            }
        case (DISPLAY):
            {
            for (int index = 0; index < SIZE; index++)
                displayCodes(codes, SIZE);
                break;
            }
        default:
            {
                cout << "Please enter a valid selection!";
                cin >> response;
            }
        } 
    } while (response == 1 || response == 2 || response == 3);
    return 0;
}
// Add codes Function - for loops - Will add code to end of existing product codes
int addCodes(int codes[], const int SIZE)
{
    char response;
    cout << "Enter y to enter codes, Enter n to stop: ";
    cin >> response;
    do
    {
        for (int n = 0; n < SIZE; n++)
        {
            cout << "Enter the product code: ";
            cin >> codes[n];
            cout << "Enter n to stop: ";
            cin >> response;
            if (response == 'n')
            {
                break;
                return codes[n];
                main();
            }
        }
    } while (response != 'n');
    return codes[SIZE];
    main();
}
// Remove Code function - Will shift codes to the left/Adjust number of codes in Array by 1
// IF NOT in code list, display an "ERROR" and IF empty display an "EMPTY"

int removeCodes(int codes[], const int SIZE)
{
    int remove;
    int count;
    cout << "What code should be removed from the database: ";
    cin >> remove;
    for (count = 0; count < SIZE; count++)
        if (codes[count] == remove)
            break;
    if (count < SIZE)
    {
        for (int i = count; i < SIZE; i++)
            codes[i] = codes[i - 1];
    }
    return SIZE;
}

void displayCodes(int codes[], const int SIZE)
{
    if (codes[0] == -858993460) // this is what was displaying when I would attempt to run the program
    {
        cout << "The database is empty";
    }
    else if(codes[0] != 858993460) // check comment above
    {
        for (int num = 0; num < SIZE; num++)
        {
            cout << codes[num] << endl;
        }
    }
}

D. Sikilai
  • 467
  • 1
  • 3
  • 17
  • 1
    `-858993460` is `0xcccccccc` and it means uninitialized stack memory in Debug mode of Visual Studio. Related: [https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations](https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations) – drescherjm Nov 23 '20 at 02:02
  • When you add & remove codes, you should update the count. Right now your program will replace the existing values instead – Martheen Nov 23 '20 at 02:11
  • Are you restricted to use only raw arrays? – Suthiro Nov 23 '20 at 03:13

1 Answers1

0

Your code has several flaws. First of all, you don't need to loop the functions inside the switches:

for (int index = 0; index < SIZE; index++) // this line is not needed
     addCodes(codes, SIZE);

because it is effectively looping over loops in the functions. You need to iterate over one-dimensional array, so you generally need simple (I mean not nested) loop.

Next, you should call add and remove functions with an additional parameter, to tell the functions how many elements you already have:

int addCodes(int codes[], const int SIZE, /* int count */);

and return new count value from functions to store it for further usage. If you don't do this, you are not able to add new elements: you are overwriting previously stored.

The code after break is unreachable, so you will never return from here:

if (response == 'n')
{
    break;
    return codes[n];
    main();
}

also you don't need to call main again, however, this code is also (kinda double time) unreachable due to break and return.

I've made some editions to your code. It works much better now, however, cin omits the first input character for me. If it is the case for you also, please see this question.

#include <iostream>
#include <iomanip>
using namespace std;

//Function Prototypes
int addCodes(int codes[], const int SIZE,  int _count);
int removeCodes(int codes[], const int SIZE,  int _count);
void displayCodes(int codes[], const int SIZE);

int main()
{
    int response;
    const int SIZE = 1000;
    int codes[SIZE];
    int count = 0;
    const int ADD = 1, REMOVE = 2, DISPLAY = 3;
    cout << "-------Code Manager-------" << endl;

    do
    {
        cout << "\nEnter 1 to Add Codes" << endl;
        cout << "Enter 2 to Remove Codes" << endl;
        cout << "Enter 3 to Display Product Codes" << endl;
        cout << "\nEnter your selection: ";
        cin >> response;

        switch(response)
        {
        case (ADD):
            {
                count = addCodes(codes, SIZE, count);
                break;
            }
        case (REMOVE):
            {
                count = removeCodes(codes, SIZE, count);
                break;
            }
        case (DISPLAY):
            {
                displayCodes(codes, count);
                break;
            }
        default:
            {
                cout << "Please enter a valid selection!";
                cin >> response;
            }
        }
    } while (response == 1 || response == 2 || response == 3);
    return 0;
}
// Add codes Function - for loops - Will add code to end of existing product codes
int addCodes(int codes[], const int SIZE, int _count)
{
    char response;

    do
    {
        for (int n = _count; n < SIZE; n++)
        {
            cout << "Enter the product code or 'n' to stop: ";
            cin >> response;
            if (response == 'n')
                break;
            cin >> codes[n];
            //increase the counter
            _count++;
        }
    } while (response != 'n');
    return _count;
}
// Remove Code function - Will shift codes to the left/Adjust number of codes in Array by 1
// IF NOT in code list, display an "ERROR" and IF empty display an "EMPTY"

int removeCodes(int codes[], const int SIZE, int _count)
{
    int remove;
    cout << "What code should be removed from the database: ";
    cin >> remove;
    if (remove > _count)
        cout << "The code is not set"<<endl;
    else
    {
        // shift the elements inside array by one
        for (int ii=remove; ii < SIZE - 1; ++ii)
            codes[ii] = codes[ii+1];
        // decrease the counter
        _count--;
    }
    // return the counter
    return _count;
}

void displayCodes(int codes[], const int SIZE)
{
    if (0 == SIZE)
        cout << "The database is empty";
    else
        for (int num = 0; num < SIZE; num++)
            cout << codes[num] << endl;
}
Suthiro
  • 1,210
  • 1
  • 7
  • 18
  • Thank you so much for your help, although both answers were much help to me, your answer really showed me how messed up my code was. I really went about it the wrong way and didn't use all my variables, so I will study this answer a lot more, so that I can use arrays correctly; thanks again! – student2666 Nov 23 '20 at 04:30