2017-02-08 45 views
0

所以基本上我的代碼輸出是提示用戶每輛車的價格 ,然後保存輸入並使用以下代碼中的以下四個函數執行calcalcaltions。然後爲每輛車的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; 

} 
+1

如果你創建一個[MCVE]你就會有更多的成功在這裏。 –

+1

而不是'string model1','string model2'等...考慮使用一個數組:'string model [4];' – user4581301

回答

-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); 
+0

我以爲我不得不調用函數4次來要求不同的汽車名稱我不會理解。 –