C++ C++;Microsoft Visual Studio阵列问题

C++ C++;Microsoft Visual Studio阵列问题,c++,arrays,C++,Arrays,请原谅我在代码中犯的一些错误,但我是新手,因为新冠病毒使我无法获得常规帮助,我想我会在这里寻求帮助,而不是不停地搜索。我们不使用“std::cout”只是为了让您知道。此外,我还不太了解如何更新数组的大小,因为它是一个常量int。我需要使用“&”或其他什么吗?我试过了,但它没有运行程序 我有一个程序,我们应该在其中添加/删除/显示代码,以允许用户向数组中添加三位代码,或将其拿走,或显示它们,最多允许1000个元素 如果你能看一下解释一下我需要做什么,因为我一直都有问题让这个程序正确运行,它从来没

请原谅我在代码中犯的一些错误,但我是新手,因为新冠病毒使我无法获得常规帮助,我想我会在这里寻求帮助,而不是不停地搜索。我们不使用“std::cout”只是为了让您知道。此外,我还不太了解如何更新数组的大小,因为它是一个常量int。我需要使用“&”或其他什么吗?我试过了,但它没有运行程序

我有一个程序,我们应该在其中添加/删除/显示代码,以允许用户向数组中添加三位代码,或将其拿走,或显示它们,最多允许1000个元素

如果你能看一下解释一下我需要做什么,因为我一直都有问题让这个程序正确运行,它从来没有不断地显示错误的东西,也没有按预期工作

这就是我的输出在读到“输入y以输入更多代码”后的样子:

谢谢大家!!再说一次,我对这一点相当陌生,所以请原谅我犯的错误! 代码:

#包括
#包括
使用名称空间std;
//功能原型
int addCodes(int codes[],常量int SIZE);
int removeCodes(int代码[],常量int大小);
无效显示代码(整数代码[],整数计数);
int main()
{
int响应;
常数int SIZE=1000;
int代码[大小];
整数计数=0;
常量int ADD=1,REMOVE=2,DISPLAY=3;

您的代码有几个缺陷。 首先,您不需要在交换机内循环函数:

for (int index = 0; index < SIZE; index++) // this line is not needed
     addCodes(codes, SIZE);
并从函数返回新的计数值以存储它以供进一步使用。如果不这样做,则无法添加新元素:您正在覆盖以前存储的

break
之后的代码无法访问,因此您将永远不会从这里返回:

if (response == 'n')
{
    break;
    return codes[n];
    main();
}
另外,您不需要再次调用
main
,但是,由于
break
return
的原因,此代码也无法访问(有点重复)

我已经对你的代码做了一些版本。它现在工作得更好了,但是,
cin
为我省略了第一个输入字符。如果你也是这样,请参阅

#包括
#包括
使用名称空间std;
//功能原型
int addCodes(int codes[],常量int SIZE,int_计数);
int removeCodes(int代码[],常量int大小,int计数);
无效显示代码(整数代码[],常量整数大小);
int main()
{
int响应;
常数int SIZE=1000;
int代码[大小];
整数计数=0;
常量int ADD=1,REMOVE=2,DISPLAY=3;

cout
-858993460
0xCCCC
,它意味着在Visual Studio的调试模式下未初始化的堆栈内存。相关:添加和删除代码时,您应该更新计数。现在,您的程序将替换现有值,而不是仅限使用原始数组吗?非常感谢您的帮助,尽管swers对我帮助很大,你的回答真的让我看到了我的代码有多混乱。我真的用错了方法,没有使用所有变量,所以我会进一步研究这个答案,这样我才能正确使用数组;再次感谢!
int addCodes(int codes[], const int SIZE, /* int count */);
if (response == 'n')
{
    break;
    return codes[n];
    main();
}
#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;
}