我该如何仅使用循环而无需阵列或向量

How can I do this without arrays or vectors only using loops

本文关键字:阵列 向量 循环 何仅使      更新时间:2023-10-16

我几乎已经完成了最后一部分。确实需要帮助,我与老师没有得到回应。我想显示哪个复杂的Enter总租金总数最高。现在,我有一个名为CurrentRentamount的双重命名,它可以在每个循环重置后保持总体运行。因此,问题是,如果第一个复合体是收集到最高的租金综合体输入的,它会失去该价值,因为它的重置为0。我感觉如此接近,但到目前为止。我不能使用矢量/数组,因为我们从技术上讲还没有学习。

   // 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 highestComplexRent = 0;
    double currentRentAmount = 0;
    double previousRentAmount = 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;
            totalAllRent = totalAllRent + rent;
            averageRent = totalAllRent / numComplex;
            outputFile << rent << endl; //write data to output file 
            currentRentAmount = currentRentAmount + rent;
            cout << currentRentAmount << endl;

            if  (currentRentAmount > highestComplexRent)
            {
                currentRentAmount = highestComplexRent;
            }
            }   
    currentRentAmount = 0;
    }


    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 = " << highestComplexRent << endl;
    system("pause");
    return 0;
}

首先是友好的音符;"复杂数字"是指非常具体的事情,您正在谈论公寓的复数。但是,当大多数人阅读复杂的数字时,他们认为X IY(认为虚构数字)

第二,您遇到的问题是逻辑之一。您想找到最高租金的APT综合体,但您似乎在这种情况下更新了最高的租金价值。

if (currentRentAmount > previousRentAmount)
{
    highestComplexRent = currentRentAmount;
}

问问自己为什么要这样做,在循环的每一个迭代中都会发生什么。如果 currentRentAmount > previousRentAmount既然presingrentamount始终为0.,则意味着,如果当前租金的价值大于0最高租金,则将其设置为当前租金。

您想要的是:1.确保高弹力设置为循环外0。2. IF检查应该是if(currentRentAmount > highestComplexRent)(考虑一下为什么会起作用,我可以告诉您答案,但请考虑一下一秒钟,您自己到达它更好)

祝你好运

您可以使用变量maxcomplexrent来保持复杂的轨道。

double maxComplexRent = 0.0;
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;
            totalAllRent = totalAllRent + rent;
            averageRent = totalAllRent / numComplex;
            outputFile << rent << endl; //write data to output file 
            currentRentAmount = currentRentAmount + rent;
            cout << currentRentAmount << endl;


            }   
    //Here
    maxComplexRent = maxComplexRent>currentRentAmount? maxComplexRent:currentRentAmount;
    currentRentAmount = 0;
    }

这里有一些建议:

  1. 它不使用正确的变量:

    if (currentRentAmount > previousRentAmount)
    

应该更改为

    if (currentRentAmount > highestComplexRent)
  1. 该线应移至外循环(尽管未使用):

    averageRent = totalAllRent / numComplex;
    
  2. 删除未使用的变量。

如果语句错误可以修复

if (currentRentAmount > highestComplexRent)
        {
            highestComplexRent = currentRentAmount;
        }