帮我改一个C++程序

来源:百度知道 编辑:UC知道 时间:2024/06/30 10:44:00
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

int main()
{
int code, Max_pass, Min_pass, Maxprofit_pass,Profit;
float Ticket_price, Proposed_price, Max_profit, Maxprofit_ticket, Fixed,count ;

cout << "Enter the proposed ticket price:$";
cin >> Proposed_price;
cout << "\nEnter the fixed cost:\t\t$";
cin >> Fixed;
cout << "\nEnter the minimum passengers:\t";
cin >> Min_pass;
cout << "\nEnter the maximum passengers:\t";
cin >> Max_pass;
cout << fixed << showpoint << setprecision(2);
system("cls");
cout <<"\nNumber Passengers\tTicket Price\t\tProfit\n\n\n";
cout << fixed << showpoint << setprecision(2);

count开始定义为float类型,在for循环中又重新定义为整型,因此程序出错.
for (int count = Min_pass; count <= Max_pass; count+=10)
改法:将上面for循环中的int去掉即可.
建议:程序中count用来控制循环次数,定义为整型较好,min_pass和max_pass也是整型,因此赋值后不会出现精度误差.

题中的count重复被定义了,程序修改如下:
直接去掉第二次定义中的int:
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;

int main()
{
int code, Max_pass, Min_pass, Maxprofit_pass,Profit;
float Ticket_price, Proposed_price, Max_profit, Maxprofit_ticket, Fixed,count ;

cout << "Enter the proposed ticket price:$";
cin >> Proposed_price;
cout << "\nEnter the fixed cost:\t\t$";
cin >> Fixed;
cout << "\nEnter the minimum passengers:\t";
cin >> Min_pass;
cout << "\nEnter the maximum passengers:\t";
cin >> Max_pass;
cout << fixed <&