0

我正在创建一个学校项目,但目前似乎迷路了。有人可以帮我解决我的问题吗?这是发生了什么:

我有一个程序,它使用 ofstream 在文本文件中输出随机生成的数字。它以两列的格式生成(一列是 SalePrice,第二列是 RegularPrice)。我的问题是创建一个将执行以下操作的代码:

  1. 创建一个函数来读取第一个程序生成的文本文件
  2. 只求第二列的平均值!(正常价格)然后在屏幕上输出
  3. 只求第二列的最小值和最大值!(正常价格)然后在屏幕上输出

请帮忙!我需要帮助如何编写 ifstream 部分,我是 C++ 新手,并且尝试了许多书中的所有解决方案,但似乎无法满足我的需求?:-( 任何帮助将不胜感激!提前致谢!

这只是我的代码的一部分(不是全部),它没有给我一个错误。. . 它根本没有给我任何东西:

 float SalePrice[userInput];
 float RegularPrice; 
 string cPrice;
 string readFile;
 int count = 0;
 ifstream inputFile;
 inputFile.open(fileName);
 inputFile >> RegularPrice;

 // To get you all the lines and place the line from myfile into the line variable
 while(!inputFile.eof() && getline(inputFile, readFile))
 {
      if (count < userInput)
      {
           inputFile >> readFile;
           readFile += cPrice; // Saves the line in STRING.

           //display the line we gathered:
           cout << cPrice << endl;
      }
      ++count;
 }

 avgRPrice = RegularPrice / userInput;

 cout << endl;
 cout << fixed << setprecision (2);
 cout << "The average of all the Regular Prices in the file is: " << avgRPrice << endl;
 cout << "The minimum Regular Price in the file is: " << minRPrice  << endl;
 cout << "The maximum Regular Price in the file is: " << maxRPrice  << endl;

已编辑:这是我当前用于查找最大值和最小值的代码:

int maxRPrice(float RPrice[])
{
     if (RPrice > maxRPrice)
         maxRPrice = RPrice;

     return maxRPrice;
}

int minRPrice(float RPrice[])
{
    if (RPrice < minRPrice)
        minRPrice = RPrice;

     return minRPrice;
}
4

1 回答 1

1

这是您的代码的改进版本,非常适合我:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
     const int userInput = 2;
     float SalePrice[userInput];    //Make an array
     float RegularPrice[userInput]; //Make an array

     string readFile;

     ifstream inputFile;
     inputFile.open("yourFile.txt");
     if(!inputFile){ //Check wether the file is open
         cout<<"Couldn't open file!" << endl; 
         return -1;
     }
     // To get you all the lines and place the line from myfile into the line variable
     for(int count = 0; !inputFile.eof() && (count < userInput) ; ++count) //Why are you using a while-loop if you need to count the iterations
     {    
          //
          inputFile >> SalePrice[count] >> RegularPrice[count]; //loads column SalePrice/RegularPrice into the array at position 'count'

     }

     float sumRegularPrice = 0; 
     for(int i=0; i < userInput; i++)
         sumRegularPrice += RegularPrice[i];

     float avgRPrice = sumRegularPrice / userInput;

     cout << endl;
     cout << fixed;
     cout << "The average of all the Regular Prices in the file is: " << avgRPrice << endl;
     //cout << "The minimum Regular Price in the file is: " << minRPrice  << endl;
     //cout << "The maximum Regular Price in the file is: " << maxRPrice  << endl;

     system("pause");
     return 0;
}

为什么你只加载一次RegularPrice?据我得到你对文件格式的解释(你说one column is SalePrice & the second column RegularPrice),每一行都可能有这个内容:

3.44 5.99

要获得最低和最高价格,您只需编写两个函数。

使用此输入:

3.44 5.99
5.54 8.99

我得到这个输出(在控制台中):

The average of all the Regular Prices in the file is: 7.490000

如果您有一些问题,请随时问我。

于 2013-09-08T19:01:21.447 回答