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;
}
}
}