【发布时间】: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 代码在粘贴箱的链接中。还有什么其他原因?