C++ 使用数据结构和数组求和的问题

C++ 使用数据结构和数组求和的问题,c++,data-structures,C++,Data Structures,我有这个代码,有几个功能,我几乎完成了,我只是有困难找到我的程序的租金成本。 我的程序读取一个汽车和租赁成本的文本文件,如下所示: 2014 Toyota Tacoma 115.12 1 2012 Honda CRV 85.10 0 2015 Ford Fusion 90.89 0 2013 GMC Yukon 110.43 0 2009 Dodge Neon 45.25 1 2011 Toyota Rav4 65.02 1 2012 Mazda CX5 86.75 1 2016 Subaru

我有这个代码,有几个功能,我几乎完成了,我只是有困难找到我的程序的租金成本。 我的程序读取一个汽车和租赁成本的文本文件,如下所示:

2014 Toyota Tacoma 115.12 1
2012 Honda CRV 85.10 0
2015 Ford Fusion 90.89 0
2013 GMC Yukon 110.43 0
2009 Dodge Neon 45.25 1
2011 Toyota Rav4 65.02 1
2012 Mazda CX5 86.75 1
2016 Subaru Outback 71.27 0
2015 Ford F150 200.83 1
2010 Toyota Corolla 50.36 1
浮动字符是价格(租赁成本)

然而,我希望用户输入汽车号码(1-10)选择多少天,并输出租赁成本。我只是有问题,它将如何读取用户想要的汽车输入。这是我的主要代码,但我需要告诉你案例3是否需要工作

#include <iostream>
#include <fstream>

using namespace std;

struct car {
    int year;
    char make[10];
    char model[10];
    float price;
    int available;

} ;

void menu();

// Main Function
int main ()
{
// declare variables
int carAmount = 10;
int choice;
car carLib[carAmount];
char filename[10];
ifstream carInData;
float mostExpensive = 0;
int MostExpensiveIndex;
int count = 0;
int days;
int rentalCost = 0;
bool menu1 = false;

    //prompt user for input file
    cout << " Enter file name: ";
    cin >> filename;

    // Start loop menu
    while(menu1 = true){

    menu();

    carInData.open(filename);

    cin >> choice;

    if (carInData.is_open()) {
    // read list of names into array


       for (; count < carAmount; count++) {

        carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available;

        }
    }

    switch (choice) {

   // Case 1 closes menu
    case 1:

    return 0;
    break;

    // Case 2 displays if car is available if 1, unavailable if 0
    case 2:

    // itterate through car array
    for(count = 0; count < carAmount; count++){

    // Displays if car is available or not 
    if (carLib[count].available == 1)
        cout << " Available ";
    else
        cout << " Unavailable ";

    // Display Cars
    cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << "  " << "\n";
    }
    break;

    // Display only available cars
    case 3:

    // itterate through car array
    for(count = 0; count < carAmount; count++){

    // Displays only available cars
    if (carLib[count].available == 1){
        cout << " Available ";

    // Display Cars
    cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << "  " << "\n";
      }
    }
    break;


    // Calculates  rental cost
    case 4:
    cout << " Enter car number and how many days " << "\n";
    cout << " Days: ";
    cin >> days;
    cout << "\n" << "Car: ";
    cin >> carLib[count].price;
    rentalCost += days*count;

    cout << " Rental Cost for " << days << " days is " << rentalCost << "\n";
    break;

    // Finds most expensive car
    case 5:

    MostExpensiveIndex = count;
    for (size_t carIndex = 0; carIndex < carAmount; ++carIndex) {

        if (carLib[carIndex].price <= mostExpensive) continue;
        mostExpensive = carLib[carIndex].price;
        MostExpensiveIndex = carIndex;

    }

    const car & carI = carLib[MostExpensiveIndex];

    cout << " Most Expensive car is: " << " " << carI.year << " " << carI.make << " " <<  carI.model << " "  << carI.price << "\n";

    break;



      }

    }

    return 0;
}


void menu()
{
    cout << " 1 - Exit program.\n";
    cout << " 2 - Show Cars\n"; 
    cout << " 3 - Show only available cars.\n";
    cout << " 4 - Rental Cost\n";
    cout << " 5 - Most Expensive Car\n";
}  
#包括
#包括
使用名称空间std;
结构车{
国际年;
charmake[10];
char模型[10];
浮动价格;
int可用;
} ;
无效菜单();
//主要功能
int main()
{
//声明变量
int-carAmount=10;
智力选择;
carLib[carAmount];
字符文件名[10];
ifstream Carinanda;
float mostExpensive=0;
intmostexpensiveindex;
整数计数=0;
国际日;
int rentalCost=0;
bool menu1=假;
//提示用户输入文件
cout>文件名;
//开始循环菜单
while(menu1=true){
菜单();
打开(文件名);
cin>>选择;
如果(carinanda.is_open()){
//将名称列表读入数组
对于(;计数>carLib[count]。年份>>carLib[count]。制造>>carLib[count]。型号>>carLib[count]。价格>>carLib[count]。可用;
}
}
开关(选择){
//案例1关闭菜单
案例1:
返回0;
打破
//案例2显示1时车辆是否可用,0时车辆是否不可用
案例2:
//直通车阵列
用于(计数=0;计数cout除非我误解了代码的工作原理,否则我认为案例3可以正常工作。不过,您应该担心的是案例4

      case 4:
            cout << " Enter car number and how many days " << "\n";
            cout << " Days: ";
            cin >> days;
            cout << "\n" << "Car: ";
            cin >> carLib[count].price;  // WHY IS THE USER CHANGING THE PRICE?
            rentalCost += days*count;    // WHY IS THE PRICE "DAYS * (CAR ID #)"

            cout << " Rental Cost for " << days << " days is " << rentalCost << "\n";
            break;
案例4:

也许如果你修复了代码中随机的、随意的缩进,一旦用正确的、可读的、逻辑的缩进进行布局,一个简单的解决方案就会变得显而易见。@SamVarshavchik好的!谢谢!是的,我只是将我的租车费改为浮动,以获得确切的金额。我只是将每辆车登记到一个特定的数字,以便用户知道。还有用途假设r只是单独检查每辆车的租金。我注意到如果用户试图检查其他车,价格会比前一辆车高,这不是我想要的。
      case 4:
            cout << " Enter car number and how many days " << "\n";
            cout << " Days: ";
            cin >> days;
            cout << "\n" << "Car: ";
            cin >> count ;
            // Note the decrement of 'count' by one, since you expect the user
            // to enter a number 1-10
            // Should probably include a check that the 'count' is valid
            rentalCost += days*carLib[count-1].price;

            cout << " Rental Cost for " << days << " days is " << rentalCost << "\n";
            break;