我目前正在制作一个项目计划,要求用户输入他们想要玩的特定运动以及他们在娱乐区预订的年龄。我很困惑如何将他们的运动和年龄存储到一个数组中,以便以后可以在程序中显示,如果他们选择查看一个或多个用户所做的所有预订。如果有人可以帮我弄清楚如何将一个或多个用户输入存储到一个数组中,以便以后可以在程序中显示它会很棒!
#include <iostream>
#include <string>
using namespace std;
int main()
{
char t; // Type of sport selected
char g, G; // Gliding
char h, H; // Hang-gliding
char f, F; //Flying
int a; // Age of patron
double x; // Rates
int s; // Selection from menu
int i; // Arrays variable
int num;
char sport[100]; // Array for all sports of patrons
int age[100]; // Array for all ages of patrons
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;
cin >> s;
for (i = 0; i < num; ++i)
if (s == 1) {
cout << "Please enter f/F for flying, g/G for gliding and h/H for hang-gliding" << endl;
cin >> t;
getline (cin, sport[i]);
cout << "Please enter the age of patron, minimum age is 16" << endl;
cin >> a;
if ((t == 'f' || t == 'F') && (a <= 25)) {
x = 68.95;
}
else if ((t == 'g' || t == 'G') && (a <= 25)) {
x = 73.95;
}
else if ((t == 'h' || t == 'H') && (a <= 25)) {
x = 99.95;
}
else if ((t == 'f' || t == 'F') && (a > 25)) {
x = 55.95;
}
else if ((t == 'g' || t == 'G') && (a > 25)) {
x = 65.95;
}
else if ((t == 'h' || t == 'H') && (a > 25)) {
x = 92.95;
}
cout << "The insurance rate is $ " << x << endl;
}
else if (s == 2) {
cout << "A patron aged " << a << " reserved a session of " << t << endl;
}
else if (s == 3) {
}
else if (s == 4);
return 0;
答案 0 :(得分:0)
你应该创建一个包含多个信息的类Patron,然后创建一个Patron类型的数组而不是多个数组:
class Patron
{
//data for each patron...
};
在main中:
Patron patrons[...];
您也可以使用动态容器,例如vector而不是数组。
std::vector<Patron> patrons;