你好我想做的就是最后一次cout陈述显示任何公寓有最高租金和公寓名称。现在它显示所有复合物输入的总租金以及输入的最后一个复杂名称。我坚持这个,可以真正使用一些帮助。我是c ++的新手,所以请以外行的方式与我交谈,我很难理解某些事情。
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ofstream outputFile;
outputFile.open("rentfile.txt");
int numComplex, numMonths;
double rent, totalAllRent = 0; //// Accumulator for total scores
string nameComplex;
string highNameComplex;
double averageRent;
double highestRentTotal = 0;
//set up numeric output programing
cout << fixed << showpoint << setprecision(1);
cout << "How many complexes will you enter?";
cin >> numComplex; //number of complexes enter
cout << "How many months of rent will you enter complex?";
cin >> numMonths; //number of months of rent enter
for (int complex = 1; complex <= numComplex; complex++)
{
cout << "Enter Complex Name ";
cin >> nameComplex;
outputFile << nameComplex << " ";
for (int months = 1; months <= numMonths; months++)
{
cout << "Enter Rent " << months << " for ";
cout << " Complex " << complex << ": ";
cin >> rent;
outputFile << rent << endl; //write data to output file
totalAllRent = totalAllRent + rent;
if (totalAllRent > highestRentTotal)
{
highNameComplex = nameComplex;
highestRentTotal = totalAllRent;
}
averageRent = totalAllRent / numComplex;
}
}
outputFile.close(); //close the file
ifstream inputFile;
inputFile.open("rentfile.txt");
cout << "Complex Monthly rent Collected per Complex " << endl;
while (inputFile >> nameComplex)
{
for (int i = 0; i < numMonths; i++)
{
inputFile >> rent;
cout << nameComplex << " " << rent << endl;
if (rent == 0)
cout << "Warning one of the complexes submitted zero rent for one of the months " << endl;
}
}
cout << "Total rent collected for the company = " << totalAllRent << endl;
cout << " Average Monthly rent collected for the company = " << averageRent << endl;
cout << highNameComplex << "collect the most rent = " << highestRentTotal << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
嗯......如果是我,我将按如下方式设计该程序:
1.定义vector<vector<double>> vvRents
按月存储所有公寓的租金
2. vvRents
中的每个元素都存储每个公寓所有月份的租金
3.收集所有数据后,按公寓计算年份的总计租金,并将总租金存储在新的向量vTotalRents
中。
4.使用max_element
算法选择最昂贵的公寓
您必须包含<vector>
才能使用vector
课程,并添加<algorithm>
以使用max_element
。