c++中查找某些变量的平均值、最大值和循环计数的问题
Problems with finding an average, max, number of certain variables, and counting number of loops, in C++
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int userInput(int& Length, char& AuthorLevel);
int computePay(int& Length, char& AuthorLevel);
int main(void)
{
int Length; // The length of the story.
int counter = 0;
int numA = 0;
int numB = 0;
int numC = 0;
char AuthorLevel; // The level of the author.
float PayOut; // The final payout.
float averagePayout = 0.0;
float highestPayout = 0.0;
while (1)
{
userInput(Length, AuthorLevel);
if ( Length == -1 )
{
break; // Force the loop to quit, and the program to end.
}
computePay(Length, AuthorLevel);
}
return 0;
} //end main()
//==================================================================
int userInput(int& Length, char& AuthorLevel )
{
cout << "Please enter the word count of the story (-1 to stop): ";
cin >> Length;
if( Length != -1)
{
cout << "Now enter the author's level (A, B, or C): ";
cin >> AuthorLevel;
cout << endl;
}
return 0;
}
//==================================================================
int computePay(int& Length, char& AuthorLevel)
{
float PayOut; // The final payout.
int numA = 0; // Number of A's that have been printed.
int numB = 0; // Total number of B's.
int numC = 0; // Number of C's.
float averagePayout = 0.0;
float highestPayout = 0.0;
int counter = 0; // The number of times the program has ran.
if(Length < 7500 || Length != -1)
{
PayOut = 0.08 * Length;
}
else if(Length < 8000)
{
PayOut = 600;
}
else if(Length < 17500)
{
PayOut = 0.075 * Length;
}
else if(Length < 19000)
{
PayOut = 1313;
}
else
{
PayOut = 0.07 * Length;
};
if (AuthorLevel == 'A' || AuthorLevel == 'a')
{
numA++;
PayOut = 1.75 * PayOut;
}
else if(AuthorLevel == 'B' || AuthorLevel == 'b')
{
numB++;
PayOut = 1.25 * PayOut;
}
else if (AuthorLevel == 'C' || AuthorLevel == 'c')
{
numC++;
PayOut = 1.00 * PayOut;
}
else
{
cout << "That was not a valid input. Please try again." << endl;
};
counter++;
cout << "The amount the author will make from the story will be: $" << PayOut;
cout << endl << endl;
cout << "The number of payments calculated is: " << counter << endl;
cout << "The number of A's inputted: " << numA << endl;
cout << "The number of B's inputted: " << numB << endl;
cout << "The number of C's inputted: " << numC << endl;
// FIXME
averagePayout = (averagePayout += PayOut) / counter;
// FIXME
if(highestPayout < PayOut)
{
highestPayout = PayOut;
}
else if(highestPayout > PayOut)
{
highestPayout = highestPayout;
}
cout << "The highest payout so far has been: $" << highestPayout << endl;
cout << "The average payout is: $" << averagePayout << endl << endl;
}
//===================================================
我试图找到平均支出,A, B和C的输出数量,最高支出和循环数量,但这些似乎都不起作用。我已经研究了几个小时了,我不知道我做错了什么。有人能帮忙吗?
出于某种原因,您为计数(平均值和计数)定义了两次变量—一次在main
,另一次在computePay
。你在循环中调用computePay
,每次调用它时,其中的所有变量都被重新初始化为零,因此你不能积累任何东西。有两种方法可以解决这个问题:
-
将
main
的变量作为引用传递给computePay
:int computePay(int& Length, char& AuthorLevel, int &averagePayout, int &highestPayout, <...>); //call it from main like: computePay(Length, AuthorLevel, averagePayout, highestPayout, <...>);
-
使这些变量在函数内
static
-这将使它们在函数调用之间保持其值int computePay(int& Length, char& AuthorLevel) { float PayOut; // The final payout. static int numA = 0; // Number of A's that have been printed. static int numB = 0; // Total number of B's. <...>
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何使用可变参数模板类使用模板元编程获得最大值
- 练习循环和使用循环 1 次以及如何获取数组的最大值
- 查找 for 循环数组C++中存在最大值和最小值的值
- 如何替换我的"for"循环以通过 STL mimax 算法找到最小值/最大值
- 在C++中,我需要创建一个程序,该程序在输入某个数字时循环并停止,然后显示最大值和最小值
- 如何从while循环生成的一组输出中找到最大值
- c++中查找某些变量的平均值、最大值和循环计数的问题
- 在sentinel使用条件语句结束循环后显示最大值
- C++循环查找用户输入的最小值、最大值、平均值和总和