C++ 如何访问数组的某些值

C++ 如何访问数组的某些值,c++,arrays,C++,Arrays,我正在开发一个需要输入/输出多种数据类型的程序,包括一个字符,代表用户预订的体育项目类型,一个int,代表他们的年龄,以及一个double,根据用户和他们想预订的体育项目输出,代表他们的保险费率 因此,简化程序的逻辑是输入程序»选择添加预订»使用字符输入运动»使用整数输入年龄»根据这些情况计算保险费率并返回给用户显示 我将对字符使用数组,存储他们希望参与的运动,一个年龄数组,一个索引整数,它将跟踪用户输入数据的数组中的哪个点,我可能需要为保险费率创建一个数组 无论如何,TL;DR我怎么说,访问数

我正在开发一个需要输入/输出多种数据类型的程序,包括一个字符,代表用户预订的体育项目类型,一个int,代表他们的年龄,以及一个double,根据用户和他们想预订的体育项目输出,代表他们的保险费率

因此,简化程序的逻辑是输入程序»选择添加预订»使用字符输入运动»使用整数输入年龄»根据这些情况计算保险费率并返回给用户显示

我将对字符使用数组,存储他们希望参与的运动,一个年龄数组,一个索引整数,它将跟踪用户输入数据的数组中的哪个点,我可能需要为保险费率创建一个数组

无论如何,TL;DR我怎么说,访问数组的特定元素以打印出类似于用户的内容用户的年龄[index]在函数中保留了一个运动类型的会话[index],基于用户请求查看的运动信息?我真的迷路了

这是代码,如果有帮助的话。这还远没有完成

#include <iostream>
using namespace std;
void print_menu(); //Prototypes
int input_choice();
void input_reservation(int patron_age[], char sport_type[], int index, double insurance_Rate);
char input_type(char sport_type[], int index);
int input_age(int patron_age[], int index);
double compute_rate(int patron_age[], char sport_type[], int index, double     insurance_Rate);
void print_all(int patron_age[], char sport_type[], int index, int size);
void print_by_sport(int patron_age[], char sport_type[], int index, int size);

int main() //Main Function
{
    int patron_age[100];
    char sport_type[100];
    int index = 0;
    double insurance_Rate;
    int menu_choice; //Variable Declaration
    do
    {
        int size = 0;
        print_menu();
        menu_choice = input_choice();
        if (menu_choice == 1)
        {
            input_reservation(patron_age, sport_type, index, insurance_Rate);
            cout << "Size is equal to " << size << " it will now be incremented" << endl;
            size++;
            index++;
            cout << "Size is now equal to " << size;
        }
        if (menu_choice == 2)
        {
            print_by_sport(patron_age, sport_type, index, size);
        }
        if (menu_choice == 3)
        {
            print_all(patron_age, sport_type, index, size);
        }
        if (menu_choice == 4)
        {
            cout << "The program will now end " << endl;
        }
    } while (menu_choice != 4);
    return 0;
}
void print_menu() //Function that prints the program menu
{
    cout << "Please pick from the following menu " << endl;
    cout << "1. Add a new reservation " << endl;
    cout << "2. Print all reservations " << endl;
    cout << "3. Print all reservations for a given sport " << endl;
    cout << "4. Quit" << endl;
}
int input_choice() //Function to get menuchocie from user
{
    int menu_selection;
    cin >> menu_selection;
    while (menu_selection > 4 || menu_selection < 1) //Validates input
    {
        cout << "\tError: Invalid input, please try again: ";
        cin >> menu_selection;
    }
    return menu_selection; //Returns the menuchocie
}
void input_reservation(int patron_age[], char sport_type[], int index, double insurance_Rate) //Working
{
    double insurance;
    input_type(sport_type, index);
    input_age(patron_age, index);
    insurance = compute_rate(patron_age, sport_type, index, insurance_Rate);
    cout << "The insurance rate is $" << insurance << endl;
} 
char input_type(char sport_type[], int index)
{
    cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding: "; //Working
    cin >> sport_type[index];
    while (sport_type[index] != 'f' && sport_type[index] != 'F' &&     sport_type[index] != 'g' && sport_type[index] != 'G' && sport_type[index] 
    != 'h' && sport_type[index] != 'H')
    {
        cout << "Error: Invalid input, please try again: ";
        cin >> sport_type[index];
    }
    return sport_type[index];
}
int input_age(int patron_age[], int index)
{
    cout << "Please enter the age of the patron, minimum 16: ";
    cin >> patron_age[index];
    while (patron_age[index] < 16 || patron_age[index] > 112)
    {
        cout << "Error: Invalid input, please try again: ";
        cin >> patron_age[index];
    }
    return patron_age[index];
}
double compute_rate(int patron_age[], char sport_type[], int index, double     insurance_Rate)
{
    if (sport_type[index] == 'f' || sport_type[index] == 'F') //If sport index is flying, do this insurance calculation
    {
        if (patron_age[index] <= 25)
        {
            insurance_Rate = 68.95;
        }
        else if (patron_age[index] > 25)
        {
            insurance_Rate = 55.95;
        }
    }
    else if (sport_type[index] == 'g' || sport_type[index] == 'G')//If sport index is gliding, do this insurance calculation
    {
        if (patron_age[index] <= 25)
        {
            insurance_Rate = 73.95;
        }
        else if (patron_age[index] > 25)
        {
            insurance_Rate = 65.95;
        }
    }
    else if (sport_type[index] == 'h' || sport_type[index] == 'H') //If     sport index is hand gliding, do this insurance calculation
    {
        if (patron_age[index] <= 25)
        {
            insurance_Rate = 99.95;
        }
        else if (patron_age[index] > 25)
        {
            insurance_Rate = 92.95;
        }
    }
    return insurance_Rate;
}
void print_all(int patron_age[], char sport_type[], int index, int size)     //Function to print all reservations
{
    cout << "Now printing patron information.... ";
    for (int i = 0; i < size; i++)
    {
    cout << "A patron aged " << patron_age[index] << " reserved a session of " << sport_type[index] << endl;
    }
}
void print_by_sport(int patron_age[], char sport_type[], int index, int size)     //Function to print all reservations based on sport type
{
    for (int i = 0; i < size; i++)
    {

    }
}

我假设您希望完成“全部打印”和“按运动打印”功能

首先,你的问题不明确。确保在发布下一个问题之前阅读

您必须为参与游戏的每个人分配一个唯一的用户ID,否则无法区分两个年龄相同的人参与同一游戏

因为在您的程序中,您已经定义了一个名为index的变量,可以用作用户ID

最初,当程序开始将用户ID保持为零时

依我看,最好为不同的游戏维护不同的阵型。因为在你的方法中,当涉及到打印关于某项运动的信息时,所有信息都分散在一个数组中,你必须遍历整个数组并检查该项运动

目前,您定义了三个游戏

飞行 滑翔 悬挂滑翔 保持三个阵型

unsigned int flying_age[100] = {0};
unsigned int gliding_age[100] = {0};
unsigned int hang-gliding_age[100] = {0};
当程序开始将indexUSER_ID初始化为0时

算法 新预订-转到步骤4 打印所有预订-转到步骤6 打印特定运动项目的预订-转到步骤7 输入游戏名称和年龄。基于游戏名称存储人物 特定数组和特定索引器ID中的信息。 增量索引器ID 从loop0开始到indexUser_id-1,只有一个数组会被设置为这个特定的用户id。检查它并打印 从loop0开始到indexUser_id-1,遍历上述数组中的一个特定数组并打印所需信息。
希望这有帮助。

这是作业还是娱乐?我问这个问题是因为可能有比数组更简单的方法。我对此投了反对票,因为这个问题不包括任何显示问题的代码,还有其他原因。我仍然愿意提供帮助。@Brad谢谢,这是一项任务。此外,我不允许为program@immibisThe代码位于粘贴箱的链接中。还有什么其他原因?谢谢你的回答,我发现它非常有用,但我已经修复了print_all_reservation功能,使其正常工作。然而,我的问题是,现在的输出会说一些类似于18岁的用户保留了一段g,但我希望它在这种情况下输出的是一个18岁的用户保留了一段滑翔。因此,我想将代表游戏的字符更改为游戏的全名。有什么方法可以做到这一点吗?在打印行下方之前,请检查它。如果sport_type[index]=“g”cout