所以基本上我的代码输出是提示用户每辆车的价格
然后使用以下代码中的以下四个函数保存输入并执行calucaltions。然后同时为每辆车的display_total_Car_cost()
功能输出以下每辆车的以下内容。
我的问题是我的代码每次为每个价格提示用户两次,然后同时显示所有汽车的以下输出我如何更改我的代码,以便它提示用户每个汽车价格一次然后输出不同线路上每辆车的最后一个功能display_total_car_cost()
。
下面是我的代码,我对输出进行了注释,以便您可以看到我的代码输出的视觉效果。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include <iomanip>
float get_vehicle_price(string);
float calculate_other_costs(float);
float calculate_registration_fee(float);
void display_total_car_cost(string, float, float);
int main() {
ifstream myfile;
string car;
string car2;
string car3;
string car4;
myfile.open("infile.txt");
getline(myfile, car);
getline(myfile, car2);
getline(myfile, car3);
getline(myfile, car4);
get_vehicle_price(car);
get_vehicle_price(car2);
get_vehicle_price(car3);
get_vehicle_price(car4);
float price1 = get_vehicle_price(car);
float price2 = get_vehicle_price(car2);
float price3 = get_vehicle_price(car3);
float price4 = get_vehicle_price(car4);
float other_costs1 = calculate_other_costs(price1);
float other_costs2 = calculate_other_costs(price2);
float other_costs3 = calculate_other_costs(price3);
float other_costs4 = calculate_other_costs(price4);
string model1 = car;
string model2 = car2;
string model3 = car3;
string model4 = car4;
display_total_car_cost(model1, price1, other_costs1);
display_total_car_cost(model2, price2, other_costs2);
display_total_car_cost(model3, price3, other_costs3);
display_total_car_cost(model3, price4, other_costs4);
return 0;
}
float get_vehicle_price(string carname) {
float carprice;
cout << "Enter the price of the :" << carname;
cin >> carprice;
return carprice;
}
float calculate_other_costs(float price) {
float salestax = price *0.06;
float other_costs = salestax + calculate_registration_fee(price);
return other_costs;
}
float calculate_registration_fee(float price) {
const int administrative_fee = 240;
const int cost_of_tags = 120;
float registration_fee = (price *0.10) + administrative_fee + cost_of_tags;
return registration_fee;
}
void display_total_car_cost(string models, float price, float other_cost){
float totalcost = price + other_cost;
cout << fixed << showpoint << models << setw(2) << setprecision(2) << price << setw(3) << setprecision(4) << other_cost << setw(4) << setprecision(2) << totalcost;
}
答案 0 :(得分:-1)
对get_vehicle_price()
的前四次调用是不可用的:您要求丢失的值。您可以删除/评论他们
//get_vehicle_price(car);
//get_vehicle_price(car2);
//get_vehicle_price(car3);
//get_vehicle_price(car4);
并仅保留以下有用的四个电话
float price1 = get_vehicle_price(car);
float price2 = get_vehicle_price(car2);
float price3 = get_vehicle_price(car3);
float price4 = get_vehicle_price(car4);