如何访问数组的某些值

时间:2016-12-12 02:40:01

标签: c++ arrays

我正在开发一个需要输入/输出多种数据类型的程序,包括代表顾客正在预订的运动类型的char,代表他们年龄的int,以及根据顾客年龄和运动他们希望保留一个位置的输出,这代表他们的保险费率。“/ p>

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

我将使用数组作为char,存储他们希望参与的运动,一个年龄数组,一个索引整数,它将跟踪用户输入数据的阵列中的哪个点,以及我可能需要为保险费率制作一个数组。

无论如何,TL; DR我该如何说,访问数组的特定元素以打印出类似于&#34; 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++) { } } &#的赞助人34;在一个功能的基础上,用户要求查看哪些运动信息?我真的输了。

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

    // Enable focus rect and accelerator underline in all controls.
    ::SendMessage(WM_CHANGEUISTATE, MAKELONG(UIS_CLEAR, UISF_HIDEACCEL | UISF_HIDEFOCUS), 0);

1 个答案:

答案 0 :(得分:0)

我假设您要完成print_all和print_by_sport函数。

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

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

因为在你的程序中你已经定义了名为“index”的变量,可以用作用户ID。

最初在程序启动时将用户ID保持为零。

IMO,最好为不同的游戏维护不同的阵列。因为在打印特定运动的信息方面,它只是分散在一个阵列中,你必须循环整个并检查该特定运动。)

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

  1. 飞行
  2. 滑翔
  3. 悬挂式滑翔
  4. 保留三个阵列

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

    当程序开始初始化索引(USER_ID)为0时。

    算法

    1. 新预订 - 转到第4步
    2. 打印所有预订 - 转到第6步
    3. 特定运动的打印预约 - GOTO Step 7
    4. 输入游戏名称和年龄。基于游戏名称存储的人 该特定数组和该特定索引(USER ID)中的信息。
    5. 增量索引(用户ID)
    6. 开始循环(0到索引(User_id) - 1)并且只为该特定用户ID设置一个数组。检查并打印
    7. 开始循环(0到索引(User_id) - 1)并遍历特定数组(上述数组之一)并打印所需信息。
    8. 希望这有帮助。