我该如何仅使用循环而无需阵列或向量
How can I do this without arrays or vectors only using loops
我几乎已经完成了最后一部分。确实需要帮助,我与老师没有得到回应。我想显示哪个复杂的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;
}
这里有一些建议:
-
它不使用正确的变量:
if (currentRentAmount > previousRentAmount)
应该更改为
if (currentRentAmount > highestComplexRent)
该线应移至外循环(尽管未使用):
averageRent = totalAllRent / numComplex;
删除未使用的变量。
如果语句错误可以修复
if (currentRentAmount > highestComplexRent)
{
highestComplexRent = currentRentAmount;
}
相关文章:
- 将std ::向量转换为阵列和当时的p/调用它会导致访问违规例外,在编组期间mscorlib.dll
- 来自指针向量的OpenGL阵列缓冲区
- 我该如何仅使用循环而无需阵列或向量
- C++向量的向量.阵列旋转 90 度
- 向量阵列,计算点产品
- 将int阵列转换为c 中int的向量
- 矢量阵列是否形成2D向量
- 阵列分配后跟 memcpy 或带储备的向量
- 浮子阵列的视觉C 向量会产生麻烦
- STL向量阵列
- 向量,图像阵列不同
- 在C 中打印向量阵列
- 创建一个二维向量阵列,遍历
- 访问2D向量阵列中的元素
- 将char阵列向量施加到POD矢量
- 将数据从STD ::向量传递到GLSL中的均匀阵列
- C++以内存安全的方式使用给定向量的大小创建2D阵列
- 将2D阵列存储到c++中的2D向量中
- 如何将值分配到类似2D阵列的2D向量中
- 如何将2D阵列的元素复制到1D向量上