Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用函数以数字填充数组。如果数组已满,则数字将填充到第二个数组中_C++_Arrays - Fatal编程技术网

C++ 使用函数以数字填充数组。如果数组已满,则数字将填充到第二个数组中

C++ 使用函数以数字填充数组。如果数组已满,则数字将填充到第二个数组中,c++,arrays,C++,Arrays,我正在处理一个名为registered的数组中的程序存储注册学生id。然后,如果该数组已满,它将自动将学生id存储在另一个名为waitlist的数组中。此操作将通过函数完成。 当我注册的数组已满时,我将无法使我的计算机将学生id存储到等待列表数组中。我很抱歉,我只能使用特定的函数将学生id存储到两个数组中。我还需要使用相同的函数打印已注册和等待列表中的学生id列表。 我不知道该怎么办,我找不到一本好的教程,这本书甚至毫无帮助。多谢各位 这是我的密码 #include <iostream&g

我正在处理一个名为registered的数组中的程序存储注册学生id。然后,如果该数组已满,它将自动将学生id存储在另一个名为waitlist的数组中。此操作将通过函数完成。 当我注册的数组已满时,我将无法使我的计算机将学生id存储到等待列表数组中。我很抱歉,我只能使用特定的函数将学生id存储到两个数组中。我还需要使用相同的函数打印已注册和等待列表中的学生id列表。 我不知道该怎么办,我找不到一本好的教程,这本书甚至毫无帮助。多谢各位

这是我的密码

#include <iostream>

using namespace std;

const int maxCap = 10;
void showOption();
void addToRear(int arr[], int& howMany, int value);
void printArray(int arr[], int howMany);
bool isFull(int capacity, int howMany); 
bool isEmpty(int howMany);

int main()
{
    bool repeat = false;
    bool repeatOption = false;

    int reg[maxCap];
    int waitlist[maxCap];
    int option, value, capacity;
    int howMany = 0;
    int registered = 0;
    int waitlisted = 0;
    do
    {
        showOption();

        cout << "please enter your option: 1 ~ 5 " << endl;
        cin >> option;


        switch (option)
        {
        case 1:
            if(isEmpty(capacity, howMany) == true)
            addToRear(reg, howMany, maxCap);
            if(isFull(capacity, howMany) == true)
            addToRear(waitlist, howMany, maxCap);
            break;
        case 3:
            printArray(reg, howMany);
            break;
        case 4:
            printArray(waitlist, howMany);
            break;

        case 5:
            cout << "thank you for using this program. " << endl;

            return 0;
            break;

        default:
            cout << "thank you for using this program. " << endl;
            break;
        }

        //cout << "there are " << howMany << " registered student in this class. " << endl;

        cout << "back to main menu? enter 1 for yes:      " << endl;
        cin >> repeatOption;
    }while(repeatOption);

}

void showOption()
{
    cout << "Main Menu: " << endl;
    cout << "1) Register Student. " << endl;
    cout << "2) Unregister Student. " << endl;
    cout << "3) Print list of registered student. " << endl;
    cout << "4) Print list of waitlisted student. " << endl;
    cout << "5) Exit. " << endl;
    cout << " " << endl;
}
//void getID(int& value)

//enter student id and store it in array register and waitlist
void addToRear(int arr[], int& howMany, int value)
{
    cout << "there are only " << maxCap << " spot for student to be registered." << endl;
    cout << "if there are " << maxCap << " students registered in this class, " << endl;
    cout << "then the rest of the student will be put on waitlist. " << endl;
    cout << " " << endl;
    cout << "enter student ID. " << endl;
    cout << "enter negative value to stop. " << endl;

    int id;
    cin >> id;

    while((id >= 0) && (howMany < maxCap))
    {
        arr[howMany] = id;
        howMany++; 
        cin >> id;
    }


}

//print list of student that is registered or waitlisted
void printArray(int arr[], int howMany)
{
    cout << "there are " << howMany << " number of student in registered list. " << endl;
    cout << "the student ID number is " << endl;

    for (int index = 0; index < howMany; index++)
    {
        cout << arr[index];
        cout << " " << endl;
    }
}

bool isFull(int capacity, int howMany)
{
/if((capacity < 1) || (howMany > 9))
        return true;
    else
        return false;
}

bool isEmpty(int capacity, int howMany)
{
    if((capacity > 9) || (howMany < 1))
        return true;
    else
        return false;
}
#包括
使用名称空间std;
const int maxCap=10;
void showOption();
void addToRear(整数arr[],整数和多少,整数值);
void printary(int arr[],int howMany);
布尔已满(整数容量,整数多少);
布尔为空(整数为多少);
int main()
{
布尔重复=假;
bool repeatOption=false;
int reg[maxCap];
int waitlist[maxCap];
int选项、值、容量;
int多少=0;
int=0;
int waitlisted=0;
做
{
showOption();
cout选项;
开关(选件)
{
案例1:
if(isEmpty(容量,数量)==true)
addToRear(注册、数量、最大上限);
如果(已满(容量,数量)=真)
addToRear(等待列表、数量、最大上限);
打破
案例3:
打印阵列(注册号,数量);
打破
案例4:
printArray(等待列表,数量);
打破
案例5:
库特
  • 使用一个既有已注册候选项又有等待列表候选项的数组会更容易
  • 在添加时,你只需要有一个结束索引。因此,只要索引小于你的截止数字,你的候选人就是注册的
  • 删除时,您必须在删除一个候选条目后将列表上移一个空格。这样,已注册列表将自动更新,以将等待列表中的候选条目减少1
  • 打印时,如果索引大于截止值,则必须检查索引是否已注册并等待列表

  • 您的代码无法识别注册列表和等待列表的满和空条件。我对下面的代码进行了一些修改。您可以尝试下面的代码片段

    case 1:
                int len = sizeof(reg)/sizeof(reg[0]); // Get the length of reg list
                int waitlistLen = sizeof(waitlist)/sizeof(waitlist[0]); //get length of wait list
                if(maxCap > len)               // Check if reg list not full. Add to reg list
                    addToRear(reg, howMany, maxCap);
                else if(maxCap > waitlistLen) // if reg list is full. Check if wait list is full or not
                    addToRear(waitlist, howMany, maxCap);
                else                         // if both the list are full show user option menu and break.
                    showOption();
                break;
    

    您面临的问题是什么?..尝试使用函数void addtorear在数组调用寄存器中存储数字。数组只有10个索引。如果寄存器数组已满,则使用同一函数在另一个数组调用等待列表中存储数字。我使用的是部分填充的数组。假设我一直输入要存储在寄存器a中的数字rray,当它达到限制时,程序应该将数字存储在另一个名为waitlist的数组中。但是,当它达到限制时,它将自动停止要求用户输入数字,并返回主菜单。这就是问题所在。我不能使用单个数组。这是要求。应用程序要维护两个数组,一个用于注册一个用于等待列表的学生。每个数组存储学生的int id号。数组应在主函数内声明(而不是在所有函数外全局声明),并作为参数传递给任何需要它们的函数。如本文所示,除了使用变量存储数组的容量外,还需要使用变量跟踪每个数组中当前存储的值的数量。应用程序应允许用户执行以下5项操作之一,直到用户希望退出:*注册学生*注销学生*打印注册学生列表*打印等待列表确保如果学生希望注册,但班级已满,则该学生将被添加到等待列表中,除非该列表也已满。您可以使用班级列表和大小为10的等待列表进行此分配nt,使用一个常量变量作为大小。在这种情况下,您可以将两个数组传递给addtorear函数。让函数根据当前索引决定要追加哪个数组。您可以对所提到的所有5个操作使用相同的逻辑。对不起,您能写下这段代码并显示它的外观吗?我知道你们可能会认为我是一个懒惰的a$$,只想找到简单的方法来完成项目。但我完全迷失在这里,我根本没有任何编程背景。最糟糕的是,我在网上学习这门课程。我只需要这门课程作为我专业的先决条件。