c++中查找某些变量的平均值、最大值和循环计数的问题

Problems with finding an average, max, number of certain variables, and counting number of loops, in C++

本文关键字:最大值 循环 问题 平均值 查找 变量 c++      更新时间:2023-10-16
#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,每次调用它时,其中的所有变量都被重新初始化为零,因此你不能积累任何东西。有两种方法可以解决这个问题:

  1. main的变量作为引用传递给computePay:

    int computePay(int& Length, char& AuthorLevel, int &averagePayout, int &highestPayout, <...>);
    //call it from main like:
    computePay(Length, AuthorLevel, averagePayout, highestPayout, <...>);
    
  2. 使这些变量在函数内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.
       <...>