【问题标题】:how to fix output issue?如何解决输出问题?
【发布时间】:2017-02-08 22:45:11
【问题描述】:

所以基本上我的代码输出是提示用户每辆车的价格 然后保存输入并使用下面代码中的以下四个函数进行计算。然后在每辆车的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;

}

【问题讨论】:

  • 而不是string model1string model2等...考虑使用数组:string model[4];

标签: c++ c++11 visual-c++


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-01-01
    • 2021-01-23
    • 2021-05-28
    相关资源
    最近更新 更多