使用数据结构和数组找出总和的问题

时间:2016-09-24 00:49:37

标签: 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 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";
}  

1 个答案:

答案 0 :(得分:0)

除非我误解了你的代码是如何工作的,否则我认为案例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;

你可以在那里看到我的评论。我会把它改成

      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;

请注意,carLib从0-> 9递增,但您的用户可能认为它从1-10开始计算。当您打印选项2(所有汽车信息)时,您可能会有所帮助,其中包含您希望从用户那里获得的汽车ID号。

另外还有一些问题

  • 您将租赁费用存储为int(不确定是否按设计),所以请记住days*price计算将四舍五入。
  • rentalCost正在逐渐增大,所以我猜测用户正在租用多辆车?
  • 您应该检查一下,在增加租金之前,用户是否可以实际租车。