【问题标题】:How to access certain values of an array如何访问数组的某些值
【发布时间】:2017-04-26 21:12:15
【问题描述】:

我正在开发一个程序,该程序需要多种数据类型的输入/输出,包括代表顾客预订的运动类型的 char、代表他们年龄的 int 和一个 double是基于顾客年龄和他们想要为其预留位置的运动的输出,代表他们的保险费率。

因此,简化程序的逻辑是输入程序 » 选择添加预订 » 使用 char 输入运动 » 使用 int 输入年龄 » 在这些情况下计算保险费率并将其返回以显示给用户。

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

无论如何,TL;DR 我怎么说呢,访问数组的特定元素以在基于哪个运动的函数中打印出类似“patron_age[index] 年龄的赞助人保留了sport_type[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++)
    {

    }
}

【问题讨论】:

  • 这是为了作业还是为了好玩?我问是因为可能有比数组更简单的方法。
  • 我对此表示反对,因为该问题不包括任何显示问题的代码,以及其他原因。
  • 我仍然愿意提供帮助。
  • @Brad 谢谢,这是一个任务。另外,我不允许在程序中使用结构或类
  • @immibis 代码在粘贴箱的链接中。还有什么其他原因?

标签: c++ arrays


【解决方案1】:

我假设你想完成print_allprint_by_sport 功能。

首先,您的问题并不明确和准确。在发布下一个问题之前,请务必阅读 https://stackoverflow.com/questions/how-to-ask

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

由于您已经在程序中定义了一个名为 index 的变量,该变量可用作 USER ID

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

IMO,最好为不同的游戏维护不同的数组。因为在您打印特定运动信息的方法中,它们都分散在一个数组中,您必须遍历整个数组并检查该特定运动。)

目前,您定义了三个游戏。

  1. 飞翔
  2. 滑翔
  3. 悬挂式滑翔

保留三个数组

unsigned int flying_age[100] = {0};
unsigned int gliding_age[100] = {0};
unsigned int hang-gliding_age[100] = {0};

当程序开始将index(USER_ID)初始化为0时。

算法

  1. 新预订 - 转到第 4 步
  2. 打印所有预订 - 转到第 6 步
  3. 打印特定运动的预订 - GOTO 步骤 7
  4. 输入游戏名称和年龄。根据游戏名称存储人 该特定数组和特定 index(USER ID) 中的信息。
  5. 递增index(USER ID)
  6. 开始 for 循环(0 to index(User_id) - 1),只有一个数组会被设置为这个特定的用户 ID。检查并打印
  7. 开始 for 循环(0 to index(User_id) - 1) 并遍历特定数组(上述数组之一)并打印所需信息。

希望这会有所帮助。

【讨论】:

  • 感谢您的回答,我发现它很有帮助,但我已经修复了 print_all_reservation 函数以使其正常工作。然而,我的问题实际上是输出当前会说“一个 18 岁的顾客预订了一次 g”,但我希望它在这种情况下输出的是“一个 18 岁的顾客预订了一次滑翔” .因此,我想将代表游戏的字符更改为游戏的全名。有没有办法做到这一点?
  • 在打印之前检查它。
  • if (sport_type[index] == 'g') cout
猜你喜欢
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2022-10-21
  • 2021-07-28
  • 1970-01-01
  • 2020-07-01
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多