我有這個代碼有幾個功能,幾乎完成,我只是無法找到我的程序的租金成本。 我的程序讀取汽車的一個文本文件,租金成本如下所示:使用數據結構和數組發現總額的問題
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)選擇多少天並輸出租賃成本。我只是遇到了麻煩,它將如何讀取用戶想要的汽車輸入。這是我的主要代碼,但我需要告訴case 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";
}
也許如果你修正了隨機的,隨意的代碼縮進,一旦它的佈局合理,可讀,邏輯縮進,一個簡單的解決方案將變得明顯。 –
@SamVarshavchik好的! –