#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的输出数量,最高支出和循环次数,但这些似乎都没有。我已经工作了几个小时,我不知道我做错了什么。有人可以帮忙吗?
答案 0 :(得分:0)
由于某种原因,您可以定义两次变量(平均值和计数值)的变量 - 一次在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.
<...>